Global Variable?

megaxoom

New Member
Joined
May 11, 2002
Messages
11
Hello

I wonder is there a way to create a variable in a Workbook, and then access this variable in the Module? Thanks for any help.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Here is what I plan to do:

Whenever a user opens my excel program, the program will ask user for some information. These information will be stored in the Workbook. In the Module, I have written many functions that will use these information to do some work. So what do I to access these information stored in the Workbook.
 
Upvote 0
If you have several Visual Basic modules in your workbook and want to use the same variable in all the modules in the workbook, declare it with the word Public or Global instead of Dim. This declaration must be at the very top of your module, above any macros or functions you have, but below option statements like Option Explicit. For example:

Public GLB_TestVariable As Integer

This allows GLB_TestVariable to be used in other modules. Note that using GLB as part of the variable name helps to identify it as a global variable. Although not required, such a naming approach will insure that you know when you are working with a global variable. Public variables will retain their values after a macro runs, just like a Static variable. However, they will be reset if the Visual Basic code is edited, an End statement is used to halt a macro, or the workbook is closed.

Please note that if a variable in a macro is declared with the same name as a global variable, references to this variable in that macro will refer to the local or macro declared variable instead of the global variable. Such a situation may cause errors that are difficult to debug.
 
Upvote 0
Hello,
Because the transfer of variables from one module/user form causes me so many
problems I put the variable value on a worksheet. This has never failed me yet.

Example..
Sub Macro1() ' Whatever module you want
Dim a As Double
' whatever code generates "a" here, for example I generate "a" by (a = 10.86)
a = 10.86
Cells(1, 1).Value = a
'Code
'Code
End Sub

Rem the following code can be in a user form or any other module
Sub Macro2()
Dim b As Double
'Code code code
b = Cells(1, 1).value
'Code code code
End Sub

' Hope this is of use
 
Upvote 0

Forum statistics

Threads
1,214,541
Messages
6,120,110
Members
448,945
Latest member
Vmanchoppy

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