Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add-in problem w/ setting workbooks

Status
Not open for further replies.

peach255

Programmer
Jan 20, 2003
29
US
I have a macro that manipulates the cells from 2 workbooks. It prompts for 2nd file to be open, do some calculations, and then copy and paste some cells from 2nd opened workbook to the 1st workbook. This procedure worked fine when I run it from the .xls file. However, when I have it as a add-in, this part did not work at all. I tried debugging it, but it did not seem to recognize the 2 workbook files. My code is as follow:

Filename = Application.GetOpenFilename(FileFilter:=Filt, Title:=Title)
'Exit if the dialog box canceled, else open file in new workbook
If Filename = False Then
MsgBox "No file selected."
Exit Sub

Else
Workbooks.Open (Filename)
End If

Set wbOne = ThisWorkbook
Set wbTwo = Workbooks.Open(Filename:=Filename)

With wbOne
.Worksheets(1).Range("X9").Value = "Sample"
.Worksheets(1).Range("Y9").Value = "Name"
End With

'Copy range L10:L89 to X10

With wbTwo
.Worksheets(1).Range("O10").Copy Destination:=wbOne.Worksheets(1).Range("AA10")
.Worksheets(1).Range("O20").Copy Destination:=wbOne.Worksheets(1).Range("AA20")
End With

Thank you!
 
ThisWorkbook means the workbook where the code is, in your case - the add-in.
So rather:
...
Else
Set wbOne=Workbooks.Open (Filename)
End If
...
Set wbTwo = Workbooks.Open(Filename:=Filename2)

combo






 
I changed the code, and it gave me a Run-time error '1004'; "could not be found.Check the spelling of the file name, and verify that the file location is correct." on the line "Set wbOne = Workbooks.Open(Filename)"

My changed code is as follow:

Filename2 = Application.GetOpenFilename(FileFilter:=Filt, Title:=Title)
'Exit if the dialog box canceled, else open file in new workbook
If Filename2 = False Then
MsgBox "No file selected."
Exit Sub

Else
Workbooks.Open (Filename2)
End If

Set wbOne = Workbooks.Open(Filename)
Set wbTwo = Workbooks.Open(Filename:=Filename2)
 
HI,

How did you set the fileName variable? (ie is it null?)

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top