VBA change instance variable from module (excel)

随声附和 提交于 2019-12-08 02:50:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!