VBA question to activate workbook with variable name

icy8s111

New Member
Joined
May 6, 2014
Messages
12
I'm trying to create a vba for a workbook to switch to that workbook but the name of that workbook could change.

For example. I'm using a workbook and save that workbook with today's date added to the name.

My current VBA currently copies a range of data, opens another spreadsheet, pastes that data, Returns back to the original workbook.

The issue is that because the original name was changed the switch back to the original workbook will no longer work.

I've found a few solutions but I'm not sure exactly which to use. Any suggestions would be most helpful.


Thanks!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi,

if the code is located in the workbook you want to return to that will always be ThisWorkbook no matter what the name is. If we are talking about any other workbook you want to return to closing the opened workbook will bring you back to the workbook which was the last to hold the focus in Excel. And to make it safe you can set an object to the workbook you start copying like

VBA Code:
Set wbReturn = ActiveWorkbook
'run the copy
wbReturn.Activate 'only for demonstation here

Holger
 
Upvote 0
Solution
Thank you! I didnt realize ThisWorkbook.activate was an option. That was exactly what I needed!


Thanks!!
 
Upvote 0

Forum statistics

Threads
1,215,324
Messages
6,124,249
Members
449,149
Latest member
mwdbActuary

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