I have a question about variables and functions in VBA. I'm not going to include exactly what the project is in hopes that the initial question won't get to obscure. I can explain the project this will be used for if requested.
Is it possible to create universal variables in VBA so that all functions can use them?
Ex:
Dim testVariable As String
Dim newVariable As String
testVariable = "A"
Function testFunction() As String
....
newVariable = testVariable
End Function
As of now, testVariable (when it is in the function) is "Empty"
Thank you,
Jesse Smothermon
Yes, but the value assignments must occur within procedures:
Public testVariable As String
Public newVariable As String
Sub SetTestVariable()
testVariable = "A"
End Sub
Function testFunction() As String
....
newVariable = testVariable
End Function
Keep in mind that it is generally bad programming practice to use a lot of global variables. You want to limit the scope of your variables to where they will be used. For more information about scope in VBA see this Microsoft KnowledgeBase article: Scope of variables in Visual Basic for Applications
Leave the variables out of any sub/function, so they become Globals
Then, call an init function before you run any code that contains the default settings, such as the code
Sub InitGlobalVars
testVariable = "A"
End Sub
Sub MyProjectMain
InitGlobalVars
....
... rest of code
End Sub
Is it possible to create universal variables in VBA so that all functions can use them?
Yes, although you need to declare the scope as stated by mwolf02. However , what you cannot do in VBA is populate them outside of a function or procedure call like you can with VBScript.
In addition, if you really need global variables, you should derive a naming scheme that differentiates global variables from locally declared variables (eg prefix them with g_
). You can get this confusing scenario:
Public testVariable as String
Public Sub Main()
Call Foo()
Call Bar()
End Sub
Public Sub Foo()
testVariable = "Foo"
End Sub
Public Sub Bar()
Dim testVariable As String
testVariable = "Bar"
End Sub
The value of the testVariable
will be "Foo" even though Bar
was called later. In fact, there is no way to reference the global variable testVariable
from within Bar since it has declared a local variable with the same name.
Alternatively, you can use global constants rather than variables .
Public Const TESTVARIABLE = "A"
I believe it fits better your question rather than setting the value within a specific function.
Obs1.: Global constants are (usually) stored in capital letters.
Obs2.: You cannot set a global constant within a worksheet code. Use a specific module (global.bas, for instance) to do it.
Rgds
Very brief example:
Dim testVariable As String
Dim newVariable As String
Sub setVariable(ByVal x as string)
testVariable = x
End Sub
Function testFunction() As String
testFunction = testVariable
End Function
Sub test()
Call setVariable("A")
MsgBox testFunction()
Call setVariable("B")
MsgBox testFunction()
End Sub
As mentioned in other posts: globals are a bad practice, and a value has to be assigned inside sub/functions. They should also be defined at the beginning of the module.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.