问题
In VBA I need a module sub to tell an instance to set up some variables.
In Module 1 I have:
Sub Load()
ThisWorkbook.SetupVariables
ThisWorkbook.TestVariables
End Sub
In ThisWorkbook I have:
Private Variable1 As Integer
Private Variable2 As String
Private Variable3 As MyUserDefinedObjectType
Public Sub SetupVariables()
Variable1 = 5
Variable2 = "Five"
Set Variable3 = New MyUserDefinedObjectType()
End Sub
Sub TestVariables()
MsgBox Variable1 & " is spelled " & Variable2
Variable3.SomeFunction
End Sub
The call to TestVariables inside Load() yields the correct result, but subsequent calls to TestVariables fail. How can I make Variable1 and Variable2 hold their values? (In my real-world situation, these variables are objects I've defined and cannot be made public variables.)
Sequence of events:
Load
is stored in Module1
and is associated with a form button on Worksheet1
. This is pressed first.
Subsequently, an ActiveX control in Worksheet2
tells ThisWorkbook
to call TestVariables
.
回答1:
It works for me as you have it. Create a Watch (View - Watch Window) for Variable1 and choose Break When Value Changes. You should be able to see when it changes to zero.
来源:https://stackoverflow.com/questions/13365758/vba-change-instance-variable-from-module-excel