How to Reference another workbook in a Macro

thomachr

Board Regular
Joined
Mar 13, 2007
Messages
53
I have been working a macro that pulls information of an different sheet within the same workbook.

I would now like to move this macro into a separate workbook, but direct the macro to reference the original workbook, still.

The relevant part of the macro is
Code:
Set FromSheet = ThisWorkbook.Worksheets("Sheet1")
Set ToSheet = ThisWorkbook.Worksheets("Summary")

How can I change "ThisWorkbook." so that it references a different file?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Code:
Set FromSheet = Workbooks("ThisWorkbookname.xls").Worksheets("Sheet1") 
Set ToSheet = Workbooks("ThisWorkbookname.xls").Worksheets("Summary")

Keep in mind, that workbook will need to be OPEN at the time code runs.
 

thomachr

Board Regular
Joined
Mar 13, 2007
Messages
53
Code:
Set FromSheet = Workbooks("ThisWorkbookname.xls").Worksheets("Sheet1") 
Set ToSheet = Workbooks("ThisWorkbookname.xls").Worksheets("Summary")

Keep in mind, that workbook will need to be OPEN at the time code runs.

Thanks a lot! Works great.
 

Forum statistics

Threads
1,181,056
Messages
5,927,864
Members
436,573
Latest member
CMR237

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
Top