How do I declare a global variable in VBA?
Solution 1
You need to declare the variables outside the function:
Public iRaw As Integer
Public iColumn As Integer
Function find_results_idle()
iRaw = 1
iColumn = 1
Solution 2
This is a question about scope.
If you only want the variables to last the lifetime of the function, use Dim
(short for Dimension) inside the function or sub to declare the variables:
Function AddSomeNumbers() As Integer
Dim intA As Integer
Dim intB As Integer
intA = 2
intB = 3
AddSomeNumbers = intA + intB
End Function
'intA and intB are no longer available since the function ended
A global variable (as SLaks pointed out) is declared outside of the function using the Public
keyword. This variable will be available during the life of your running application. In the case of Excel, this means the variables will be available as long as that particular Excel workbook is open.
Public intA As Integer
Private intB As Integer
Function AddSomeNumbers() As Integer
intA = 2
intB = 3
AddSomeNumbers = intA + intB
End Function
'intA and intB are still both available. However, because intA is public, '
'it can also be referenced from code in other modules. Because intB is private,'
'it will be hidden from other modules.
You can also have variables that are only accessible within a particular module (or class) by declaring them with the Private
keyword.
If you're building a big application and feel a need to use global variables, I would recommend creating a separate module just for your global variables. This should help you keep track of them in one place.
Solution 3
To use global variables, Insert New Module from VBA Project UI and declare variables using Global
Global iRaw As Integer
Global iColumn As Integer
Solution 4
The question is really about scope, as the other guy put it.
In short, consider this "module":
Public Var1 As variant 'Var1 can be used in all
'modules, class modules and userforms of
'thisworkbook and will preserve any values
'assigned to it until either the workbook
'is closed or the project is reset.
Dim Var2 As Variant 'Var2 and Var3 can be used anywhere on the
Private Var3 As Variant ''current module and will preserve any values
''they're assigned until either the workbook
''is closed or the project is reset.
Sub MySub() 'Var4 can only be used within the procedure MySub
Dim Var4 as Variant ''and will only store values until the procedure
End Sub ''ends.
Sub MyOtherSub() 'You can even declare another Var4 within a
Dim Var4 as Variant ''different procedure without generating an
End Sub ''error (only possible confusion).
You can check out this MSDN reference for more on variable declaration and this other Stack Overflow Question for more on how variables go out of scope.
Two other quick things:
- Be organized when using workbook level variables, so your code doesn't get confusing. Prefer Functions (with proper data types) or passing arguments ByRef.
- If you want a variable to preserve its value between calls, you can use the Static statement.
Solution 5
If this function is in a module/class, you could just write them outside of the function, so it has Global Scope
. Global Scope means the variable can be accessed by another function in the same module/class (if you use dim
as declaration statement, use public
if you want the variables can be accessed by all function in all modules) :
Dim iRaw As Integer
Dim iColumn As Integer
Function find_results_idle()
iRaw = 1
iColumn = 1
End Function
Function this_can_access_global()
iRaw = 2
iColumn = 2
End Function
Admin
Updated on September 04, 2021Comments
-
Admin over 2 years
I wrote the following code:
Function find_results_idle() Public iRaw As Integer Public iColumn As Integer iRaw = 1 iColumn = 1
And I get the error message:
"invalid attribute in Sub or Function"
Do you know what I did wrong?
I tried to use
Global
instead ofPublic
, but got the same problem.I tried to declare the function itself as `Public, but that also did no good.
What do I need to do to create the global variable?
-
Seb about 7 yearsAre you sure about that a Global variable can be used in different workbooks ? Doesn't work for me
-
FCastro about 7 yearsGood point! I noticed I didn't add a reference for where I got that information... Nor did I find it again. Better to edit the answer... :/ Oh, and thanks Seb.
-
Egalth over 6 years+1 Still useful 7 years later. But is there some additional nuance regarding object variables versus data variables? I ran into an issue related to scoping object variables that prompted a new question. Much appreciated if you would have time to have a look. stackoverflow.com/q/46058096/5457466
-
Przemyslaw Remin over 5 yearsWhat is the difference between declaring with
Public
vs withGlobal
keyword? -
Fandango68 over 4 yearsI tried all the other suggestions about "scope" and none worked. The only thing that worked was a new Module specifically for global vars and it works!
-
kapilddit over 4 yearsWhen I try to declare an array as public it says: array and user-defined data types can't be declared as public.
-
Solomon Duskis over 4 years
-
Solomon Duskis over 4 yearsAbove the first
Function
/Sub
, not just outside: "Module-level variables can be declared with a Dim or Private statement at the top of the module above the first procedure definition." (from Scope of variables in Visual Basic for Applications)