VBA Assign ActiveWorkBook.name to Global / Public Variable to use in other Subs

MixedUpExcel

Board Regular
Joined
Apr 7, 2015
Messages
222
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm struggling to find the answer.. probably because the way I'm wording the search.

I have a Main Sub Routine which in turn Calls various smaller Sub Routines.

in the 1st Sub I assign a name to the active 'main' workbook eg.

Code:
Dim workbooknamemain As String


    workbooknamemain = ActiveWorkbook.Name

then through the course of running the various Sub Routines, I have activated another workbook - done something to that and then need to reactivate the 'main' workbook again.

I'm now in a different Sub Routine than the one that I assigned 'workbooknamemain' to the Main Workbook.

I want 'workbooknamemain' to work in all sub routines after I've assigned the main workbook to it.

I've tried: Public workbooknamemain' As String above all Sub Routines in the Module (right at the top) but that didn't work.

What's the best way to do this please?

Thanks.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I've tried: Public workbooknamemain' As String above all Sub Routines in the Module (right at the top) but that didn't work.
Did you also remove the Dim statement from the macro?
Also is the module a standard module, or a sheet module?
 
Upvote 0
Did you also remove the Dim statement from the macro?
Also is the module a standard module, or a sheet module?

Hi Fluff,

Honestly can't tell you if I had left the DIM in the module or not when I had created the Public reference above. I have been playing around with different set ups and couldn't get any to do what I wanted.

Happy to follow some direction to get this working though :)

Regarding the Module - this is just a Standard Module that I created using Insert>Module - so not a worksheet module.

I've also found through looking online since my original post that I can supposedly pass Variables to other Sub Routines by referencing the Variable after the name of the Sub Routine eg. Sub Testing(workbooknamemain) but I've not figured out how that works just yet.

So I guess I'm looking at both options.. which ever can get the job done for me.

Thanks.

Simon
 
Upvote 0
Hi Fluff,

Based on the question you asked about whether I had the DIM in the Sub Routine made me think that it might not be needed if I'm declaring the Public at the top of the Module. I also just read that declaring a DIM in a Sub Routine limits that to just that Sub.

So I tried:

Code:
Public sheetname As String

Sub test1()


   sheetname = ActiveSheet.Name
   
Call hellotest


End Sub


Sub hellotest()


MsgBox sheetname


End Sub

That seemed to work.. so on Monday I'll try with my main workbook (when I'm back in the office) and see if I can iron out the kinks.

Thank you for your question.. looks like it might be putting me on the right track.

Simon
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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