Workbook name in macro

Hanz77

Board Regular
Joined
Sep 21, 2006
Messages
87
I have a workbook (workbook1.xlsm) that has a VB script that runs as soon as it opens. This VB script opens another workbook (workbook2.xlsm). As soon as workbook2.xlsm is opened, it copies everything from sheet1 and then goes back to workbook1.xlsm and pastes everything into sheet1. The final step of the script is that it closes workbook2.xlsm.

The problem is that workbook1.xlsm needs to be renamed and I still need the macro to run each time it opens. It all runs fine up until it needs to switch back to the workbook originally called workbook1.xlsm.

Is there a way to set a workbook name in the VB script so this will run regardless of the workbook name containing the macro that runs at startup?
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
If you want to refer to the workbook that the code is in then you can use ThisWorkbook, no need to refer to it by it's name.

Mind you there should be no need to switch between workbooks if all you are doing is opening another workbook and copying some data from it, you should be able to do that without activating/selecting etc.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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