Save workbooks in variable in an addin

PhilippeMortreux

New Member
Joined
Jan 24, 2013
Messages
3
hello,

In an addin, I need to handle 2 workbooks, unfortunately their names vary, so I don't know how to refer to them so that it would work.

1) I open the first workbook and save it under the variable WB1 (workbook1)
2) then I open the second workbook using the code and save it under another variable WB2 (workbook2)

I need to navigate from the one to the other in the code but It does not work if I refer to them using WB1 and WB2
If you have any suggestions it would be really helpful.

here is a simplified version of what I am trying to do:

Code:
Sub NavigateBtweenWBks()

Dim WB1 as workbook
Dim WB2 as workbook

' Open the first workbook
Workbooks.Open Filename:="G:\path\Workbook1.xlsx"
 set WB1 = activeworkbook

' Open the second workbook
Workbooks.Open Filename:="G:\path\Workbook2.xlsx"
set WB2 = activeworkbook

WB1.activate 
' copy range("A1") from WB1 to WB2 
WB1.sheets(1).range("A1").copy destination:=WB2.sheets(1).range("A1)


WB2.activate
WB1.close

end sub

PS: I understand that I cannot use "thisworkbook" as it is a Addin and there is no workbooks visible.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top