VBA Functions and Variables

I have a question about variables and functions in VBA. I am not going to include exactly what the project is in the hope that the original question will not be hidden. I can explain the project that will be used for the request.

Is it possible to create universal variables in VBA so that all functions can use them?

Example:

Dim testVariable As String
Dim newVariable As String

testVariable = "A"

Function testFunction() As String
    ....
    newVariable = testVariable
End Function

At the moment, testVariable (when it is in a function) is "Empty"

Thank,

Jesse Montermon

+3
source share
5 answers

Yes, but value assignments must be performed as part of the procedures:

Public testVariable As String
Public newVariable As String

Sub SetTestVariable()
    testVariable = "A"
End Sub

Function testFunction() As String
    ....
    newVariable = testVariable
End Function

, . , . VBA . Microsoft Knowledge Base: Visual Basic

+3

VBA, ?

, , mwolf02. , VBA, , VBScript.

, , , (, g_). :

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

testVariable "Foo", Bar . , testVariable Bar, .

+2

/,

init , ,

Sub InitGlobalVars
    testVariable = "A"
End Sub

Sub MyProjectMain
    InitGlobalVars
    ....
    ... rest of code
End Sub
+1

:

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

: - , /. .

0

, .

Public Const TESTVARIABLE = "A"

, , .

Obs1: () .

Obs2: . (, global.bas).

Rgds

0

All Articles