Dynamic Workbook Name

alexaronson

Active Member
Joined
Sep 30, 2005
Messages
314
Hello and thank you for looking at my question.

I have code in my macro that decleares the workbook and sheets which makes the code run much much faster.

One of the problems I run into is that my users sometimes saves the workbook under a different name and that cause my code problems, because the code is looking for a specific workbook name.

Code:
Dim Wb as WorkBook
Dim S1 as WorkSheet

Set Wb = Workbooks("ExampleFile.xlsm")
Set S1 as WorkSheets("Sheet1")

Typically when I or my users execute the file with this naming process the macro takes less than 1 second to process.

However, when trying to make the Workbook name flexible by changing to this:
Code:
Set Wb = Workbooks(ActiveWorkbook.Name)

The code is taking 5 times longer.

Any suggestions on how to make the Workbook name flexible without really slowing down my macro?

Thanks
 

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
Personally, don't see how that alone can make it go 5 times slower...

But, perhaps this is better

Set Wb = ThisWorkbook


ThisWorkbook refers to the book the code resides in.
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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