UserForm_Initialize() 在其他模块访问表单变量时自动调用
UserForm_Initialize() is automatically called when a form variable is accessed in other module
我正在尝试将一些公开声明的用户窗体变量的值放入其他 sheets 编写的其他模块中。
第一个myform.show函数初始化表单并设置i = 0
。但是当我使用 x 按钮关闭表单并使用 queryclosed 函数更新 i
的值并将 return 控制为 Myform.val = 10
行时 userform_initialize()
函数再次被调用,条件变为 false
因为它再次将 i
设置为 0
。我不知道为什么会这样。请任何帮助。
我的sheet模块代码如下:
Sub myModule()
Myform.Show
If Myform.val=10 then
msg "Hi"
End if
End sub
而Myform代码如下:
Public i as integer
Private Sub UserForm_Initialize()
i = 0
End sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode <> 1 Then
i = 10
End If
End Sub
如果使用 x 按钮关闭表单,我希望以上条件为真,如果使用命令按钮关闭表单,则 false
。
我想提出一些更改建议。所有这些都不是必需的,但它会帮助您保持代码整洁。
首先,使用窗体的实例,而不是窗体。其次,使用属性而不是全局变量。第三,如果卸载表单,就不能再访问它的值。
模块
Sub myModule()
Dim MyFormInstance As MyForm
Set MyFormInstance = New MyForm
MyFormInstance.Show
If MyFormInstance.Val = 10 Then
MsgBox "Hi"
End If
Unload MyFormInstance
End Sub
和表格
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode <> 1 Then
Cancel = 1
Me.Hide
i = 10
End If
End Sub
Property Get Val() As Integer
Val = i
End Property
Private Sub btnClose_Click()
i = 20
Me.Hide
End Sub
我正在尝试将一些公开声明的用户窗体变量的值放入其他 sheets 编写的其他模块中。
第一个myform.show函数初始化表单并设置i = 0
。但是当我使用 x 按钮关闭表单并使用 queryclosed 函数更新 i
的值并将 return 控制为 Myform.val = 10
行时 userform_initialize()
函数再次被调用,条件变为 false
因为它再次将 i
设置为 0
。我不知道为什么会这样。请任何帮助。
我的sheet模块代码如下:
Sub myModule()
Myform.Show
If Myform.val=10 then
msg "Hi"
End if
End sub
而Myform代码如下:
Public i as integer
Private Sub UserForm_Initialize()
i = 0
End sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode <> 1 Then
i = 10
End If
End Sub
如果使用 x 按钮关闭表单,我希望以上条件为真,如果使用命令按钮关闭表单,则 false
。
我想提出一些更改建议。所有这些都不是必需的,但它会帮助您保持代码整洁。
首先,使用窗体的实例,而不是窗体。其次,使用属性而不是全局变量。第三,如果卸载表单,就不能再访问它的值。
模块
Sub myModule()
Dim MyFormInstance As MyForm
Set MyFormInstance = New MyForm
MyFormInstance.Show
If MyFormInstance.Val = 10 Then
MsgBox "Hi"
End If
Unload MyFormInstance
End Sub
和表格
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode <> 1 Then
Cancel = 1
Me.Hide
i = 10
End If
End Sub
Property Get Val() As Integer
Val = i
End Property
Private Sub btnClose_Click()
i = 20
Me.Hide
End Sub