Excel 从窗体到类模块VBA的变量范围
我有一个表单UserForm1,它有两个命令按钮command1和command2。按下命令1时变量var=1,按下命令2时变量var=2。Var是UserForm1中的全局变量。expDate和textRecDate是UserForm1中的两个文本框 我有一个类模块clsMod,我正在尝试这样做Excel 从窗体到类模块VBA的变量范围,excel,vba,Excel,Vba,我有一个表单UserForm1,它有两个命令按钮command1和command2。按下命令1时变量var=1,按下命令2时变量var=2。Var是UserForm1中的全局变量。expDate和textRecDate是UserForm1中的两个文本框 我有一个类模块clsMod,我正在尝试这样做 If UserForm1.var= 1 Then UserForm1.expDate.Text = SelectedDate If UserForm1.var= 2 Then Us
If UserForm1.var= 1 Then
UserForm1.expDate.Text = SelectedDate
If UserForm1.var= 2 Then
UserForm1.textRecDate.Text = SelectedDate
我想将var的范围扩展到类模块。我有办法做到这一点吗
谢谢您真的不想“扩展”范围-您想将范围限制在表单级别,然后使用Public
方法访问/控制表单
在UserForm1中
Private Var As Integer
Public Function GetVar() As Integer
GetVar = Var
End Function
Public Sub SetTextRecDate(d as Date)
textRecDate.Text = SelectedDate
End Sub
在克莱斯茂德
If UserForm1.GetVar = 2 Then
UserForm1.SetTextRecDate(SelectedDate)
'The line above may actually want to be...
'UserForm1.SetTextRecDate SelectedDate
'VBA is strange about parenthesised arguments
Endif
等等
哦,如果命名不仅仅是为了演示/示例,请不要养成调用按钮
Command1
和Command2
以及变量Var
:D…Var是如何声明的?最好的方法是将其创建为窗体上的属性。如果您想使解决方案健壮,还应该为expDate和recDate添加属性。Var在UserForm1中声明为全局声明,作为integeries。它被宣布为public那么会发生什么呢?如果您看到错误,那是什么?为什么所有这些VBA问题都标记为VB6??