我花了最近几周编写UserForm编码.我的问题很简单,我应该在哪里加载和卸载frm1(Userform名称是frm1)以及我应该在哪里放置Me.Show和Me.Hide.问题是,UserForm中的(x)按钮不起作用.现在,我在Sheet1上写的Active-X命令按钮代码中找到了我的加载和卸载:
Private Sub cmdb1_Click()
Load frm1
Unload frm1
End Sub
Run Code Online (Sandbox Code Playgroud)
这样我的UserForm被初始化,我可以运行代码
Private Sub Userform_Initialize()
'Some other code that Works...
frm1.Show
End Sub
Run Code Online (Sandbox Code Playgroud)
这显示了我的Userform.现在,我的Userform中有一个包含代码的命令按钮
Private Sub cmdbClose_Click()
Me.Hide
End Sub
Run Code Online (Sandbox Code Playgroud)
我用来隐藏sub,在cmdb1_Click()中执行最后一行并卸载UserForm.这个作品.
但是,当我按下UserForm中的(x)按钮时,会出现以下错误
调试器说错误位于cmdb1_Click()内.我尝试添加一个名为UserForm_QueryClose()的子,但错误仍然存在.如果我不得不猜测,我会说错误是由我处理加载和卸载的方式引起的,因此是cmdb1_Click().
编辑:
这是为了未来的读者.我的问题解决了,代码如下.ShowUserform和cmdbClose_Click包含CallumDA建议的代码.我的命令按钮现在有:
Private Sub cmdb1_Click()
Load frm1
Call ShowUserform
End Sub
Run Code Online (Sandbox Code Playgroud)
Exc*_*ers 18
我建议您创建userform的实例:
Dim MyDialog As frm1
Set MyDialog = New frm1 'This fires Userform_Initialize
Run Code Online (Sandbox Code Playgroud)
然后,您可以在尝试卸载表单之前轻松检查表单是否已加载:
If Not MyDialog Is Nothing Then
Unload MyDialog
End If
Run Code Online (Sandbox Code Playgroud)
我还建议您不要从表单的Initialize事件中调用Show方法.将您的用户表单视为Excel中的另一个对象,并从主代码体管理它.
另外,我不会像CallumDA建议的那样在cmdbClose_Click事件中卸载它(尽管这可以解决当前问题).通常,您需要能够从主代码体中引用表单上的值,如果卸载它们,它们将无法使用.相反,保持它就像你在第一时间拥有它:
Private Sub cmdbClose_Click()
Me.Hide
End Sub
Run Code Online (Sandbox Code Playgroud)
所以你的主代码体(在你的activeX按钮中)看起来像这样:
Dim MyDialog as frm1
Set MyDialog = New frm1 'This fires Userform_Initialize
'Place any code you want to execute between the Initialize and Activate events of the form here
MyDialog.Show 'This fires Userform_Activate
'When the close button is clicked, execution will resume on the next line:
SomeVariable = MyDialog.SomeControl.Value
'etc.
If Not MyDialog Is Nothing Then
Unload MyDialog
End If
Run Code Online (Sandbox Code Playgroud)
您还可以捕获用户单击表单上的"X"时触发的事件,并阻止表单被卸载:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = True
Me.Hide
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
最后,通常您需要在表单上使用"取消"按钮.我处理这个的方法是在窗体的代码隐藏中创建一个"已取消"属性:
Public Cancelled as Boolean
'(Note You can create additional properties to store other values from the form.)
Run Code Online (Sandbox Code Playgroud)
在"取消"按钮的单击事件中:
Private Sub cmdbCancel_Click()
Me.Cancelled = True
Me.Hide
End Sub
Run Code Online (Sandbox Code Playgroud)
并在主代码体中:
Dim MyDialog as frm1
Set MyDialog = New frm1
MyDialog.Show
If Not MyDialog.Cancelled Then
SomeVariable = MyDialog.SomeControl.Value
SomeOtherVariable = MyDialog.SomeOtherProperty
'etc.
End If
If Not MyDialog Is Nothing Then
Unload MyDialog
End If
Run Code Online (Sandbox Code Playgroud)
(我知道上面并不是严格说明属性的正确方法,但这样可以正常工作.如果你愿意的话,你可以按常规方式将它设为只读.)
将其放在标准模块中,并将其链接到用于显示用户窗体的按钮
Sub ShowUserform
frm1.Show
End Sub
Run Code Online (Sandbox Code Playgroud)
然后在userform中
Private Sub cmdbClose_Click()
Unload Me
End Sub
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
66202 次 |
最近记录: |