Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 从窗体到类模块VBA的变量范围_Excel_Vba - Fatal编程技术网

Excel 从窗体到类模块VBA的变量范围

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

我有一个表单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 
    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??