VBA 在用户表单代码中使用全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28044194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA use global variable in userform code
提问by fatgecko
I'm working in Excel 2010 VBA. Is there a way of accessing values in global variables declared outside a userform, in code inside the userform? Code inside my userform returns the global variable as null - can't work out why!
我在 Excel 2010 VBA 中工作。有没有办法访问在用户表单外部声明的全局变量中的值,在用户表单内部的代码中?我的用户表单中的代码将全局变量返回为 null - 不知道为什么!
The variable is declared in the ThisWorkbook module as:
该变量在 ThisWorkbook 模块中声明为:
Public TargetCell As Range
Public TargetCellWorksheet as Worksheet
Public CurrentValue As Long
Inside the userform, I have this code on the "Update" button:
在用户表单中,我在“更新”按钮上有这个代码:
Private Sub Update_Click()
MsgBox ("Start of Update sub. TargetCellWorksheet =" & TargetCellWorksheet)
End Sub
The msgbox returns "" for the variable.
msgbox 为变量返回 ""。
Hoping someone may be able to help me understand this and how to access the variable inside the userform? Thank you in advance
希望有人能够帮助我理解这一点以及如何访问用户表单中的变量?先感谢您
回答by Matteo NNZ
As for the problem itself, you declare
至于问题本身,你声明
Public TargetCellWorksheet as Worksheet
and then try to show it into a MsgBox:
然后尝试将其显示到 MsgBox 中:
MsgBox ("Start of Update sub. TargetCellWorksheet =" & TargetCellWorksheet)
Did you maybe mean TargetCellWorksheet.Name
, or TargetCellWorksheet.Range("A1").Value
, since the MsgBox expects to receive a string?
您的意思是TargetCellWorksheet.Name
, 还是TargetCellWorksheet.Range("A1").Value
, 因为 MsgBox 期望收到一个字符串?
However, if you're sure about your code, it mightdepend on the fact that the variable is not properly declared as Public and it goes at module level only. You might want to add a property to your form, if the variable is part of the form itself (I assume that you meant to use CurrentValue
, but you can simply change the type of the property from Long
to Worksheet
and use it instead):
但是,如果您确定自己的代码,则可能取决于变量未正确声明为 Public 并且仅在模块级别进行的事实。如果变量是表单本身的一部分,您可能想要向表单添加一个属性(我假设您打算使用CurrentValue
,但您可以简单地将属性的类型从Long
to更改为Worksheet
并使用它):
This goes inside the code of your form
这在您的表单代码中
Dim pCurrentValue As Long
Public Property Get CurrentValue() As Long
CurrentValue = pCurrentValue
End Property
Public Property Let CurrentValue (value As Long)
pCurrentValue = value
End Property
Hence, passing the variable from the module to the form like this:
因此,将变量从模块传递到表单,如下所示:
This goes into your module, before you enter the code of the form
在您输入表单代码之前,这会进入您的模块
Dim myForm As New yourForm
myForm.CurrentValue = whateverYourVariableIs
and so using the variable inside your form like this:
因此在表单中使用变量,如下所示:
You can hence use your variable by calling it from the property of the form
因此,您可以通过从表单的属性调用它来使用您的变量
myVariableInTheForm = Me.CurrentValue
I must say that, however, it is strange that a public variable is not reaching the stack of the form. Are you sure you're not only declaring the variable without assigning any value before?
然而,我必须说,公共变量没有到达表单的堆栈是很奇怪的。您确定您之前不仅声明了变量而没有分配任何值吗?