Publically set Object Variable

ilya2004

Board Regular
Joined
Mar 17, 2011
Messages
135
Hello,
I am trying to store some worksheet names to variables so that I can easily refer to them in all my sub routines in different modules.

It seems to me that there is not "official" way to permanently store an object variable name to be used by different sub routines, but does anyone have any quick-fix way for me to do this?
-Ilya
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Define the varailable outside of the Subs inside of a module... eg this one for a path name



Public Const filepaths As String = "\\Wpltsz31\fvcimport\Settlements\Options\"

Sub testing
'a bit of code
End sub

Then you can call "Filepaths" in any sub
 
Upvote 0
so in this case, lets say I have a worksheet called "User Data" that I would like to be able to reference as "wUD" in every routine, how would that look like in your code?
 
Upvote 0
At the top of a code module, just below Option Explicit,

Code:
Public wksUD as Worksheet

You can reference that in any module in the Project.

I'd suggest that you have one module that initializes all public variables.
 
Upvote 0
I'm a little unclear. Doing this would only declare the variable type globally, I want to permanently tie the variable to a specific worksheet.
 
Upvote 0
In the procedure where you initialize variables,

Code:
Set wksUD = Worksheets("User Data")
 
Upvote 0
I tried to do it that way, but for some reason as soon as I take the declaration out of the running module and put it into a separate module (that I run in the current one), I get an object error. That's what prompted me to post the question.
 
Upvote 0
as soon as I take the declaration out of the running module and put it into a separate module (that I run in the current one)

Can you explain exactly what you mean by that?
 
Upvote 0
It has to be in the same Project (i.e., in the same workbook) as other code that uses it, and it must be in a code module (not a sheet module or ThisWorkbook), and you must not declare it in any other procedure, but other than that, it should work fine.

If it doesn't, post your code with an explanation of where it appears.

Howdy, Rory.
 
Upvote 0

Forum statistics

Threads
1,224,559
Messages
6,179,517
Members
452,921
Latest member
BBQKING

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