Declare Multiple Variables for any Open Workbooks

Bravurian17

New Member
Joined
Apr 16, 2009
Messages
34
I have a bunch of Variables that I have in one workbook and I was wondering if I could declare it for the whole application rather than just the project.

Example for Main Workbook:
Code:
Option Explicit

Public Shop, Shift, Name as String

Sub Add
  Shop = "Engine"
  Shift = "Days"
  Name = "Ted"
End Sub
Then on any other Workbook:

Code:
Sub GetShop
  Msgbox Shop
End Sub

Sub GetShift
  Msgbox Shift
End Sub

Sub GetName
  Msgbox Name
End Sub
This is only a very basic example of what I want to accomplish.

The number of other Workbooks is unknown.

Thank You
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
In general it is good development practice to avoid global variables. That said, I don't believe variables in one workbook (project) are accessible in another. You could write a function/sub (or multiple functions/subs) that share/update the value of these variables. If you do that, these functions/subs will behave, at a conceptual level very much like properties of the workbook object.

You could also explictly do the same. For an article I authored for MSDN some time ago, see
How to use a class (object) from outside of the VBA project in which it is declared
http://support.microsoft.com/kb/555159
 
Upvote 0
tusharm,

That might be a lot more than what I was hoping for. What was the Global thing you wanted to avoid?

Thanks for the quick response
B-17
 
Upvote 0
Hi Bravurian17

I don't see why I'd use a public variable in one workbook to be used by another, an approach like Tushar suggests is preferable. It is possible, however, to do it. You just have to reference the workbook with the public declarations.

In your example, assuming you have saved the workbooks as main.xlsb and other.xlsb,

- select any module in the main.xlsb project
- change the name of the project in Tools->VBA Project properties to VBAProject001
- select any module in the other.xlsb project
- in Tools->References check the reference to VBAProject001

Now try running Main() and then any of the GetShop, GetShift or GetName.
 
Upvote 0
What do you mean by:

Now try running Main() and then any of the GetShop, GetShift or GetName.

I have been playing with VBA for over a year now; I'm self taught, so I learn new things that I wish I knew months ago. Could you give an example, of it used in a procedure, of the "Get" property and other ways I could use it.

Thank You,
B-17
 
Upvote 0
ohhh, it was a lot simpler. well you did give me the idea to search for global variables, so you still get credit.
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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