VBA从模块(excel)更改实例变量 [英] VBA change instance variable from module (excel)

查看:158
本文介绍了VBA从模块(excel)更改实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在VBA中,我需要一个模块子句来告诉一个实例来设置一些变量。在模块1中,我有:

  Sub Load()
ThisWorkbook.SetupVariables
ThisWorkbook.TestVariables
End Sub

在ThisWorkbook中我有:

  Private Variable1 As整数
私有变量2作为字符串
私有变量3作为MyUserDefinedObjectType

Public Sub SetupVariables()
Variable1 = 5
Variable2 =Five
Set Variable3 = New MyUserDefinedObjectType()
End Sub

Sub TestVariables()
MsgBox Variable1& 拼写& Variable2
Variable3.SomeFunction
End Sub

调用TestVariables内部Load )产生正确的结果,但后续调用TestVariables失败。如何使Variable1和Variable2保持其值? (在我的现实情况下,这些变量是我定义的对象,不能被作为公共变量。)



事件顺序:



加载存储在 Module1 中,并与<$ c上的表单按钮相关联$ C> Worksheet1 。这是第一个。



随后, Worksheet2 中的ActiveX控件告诉 ThisWorkbook 调用 TestVariables

解决方案

它适用于我拥有它为Variable1创建一个Watch(查看 - 观察窗口),并选择Break When Value Changes。您应该能够看到它何时更改为零。


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.

解决方案

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.

这篇关于VBA从模块(excel)更改实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆