Excel 用户表单 运行-尝试在初始 'Cancel' 后第二次启动或关闭时出现红色 'X' 时出现时间错误
Excel Userform Run-Time Error when trying to launch a 2nd time after initial 'Cancel' or close on red 'X'
问题:我正在构建一个具有 'Submit' 和 'Cancel' 按钮的用户表单。如果用户点击 'Cancel' 按钮,我希望整个表单清除所有输入的数据并关闭表单,但如果用户点击顶部的红色 'X',我也试图构建相同的功能右上角。我不清楚我需要在哪里卸载表格。我目前将它放在 btnCancel_Click()
方法中,我可以启动表单,输入一些数据并点击取消,它会关闭表单。
但是,当我第二次尝试重新启动表单时,我收到一条错误消息(我附上了该消息的图片),内容为
"Run-Time error '-2177418105 (80010007): Automation Error - The Callee (server [not server application]) is not available and disappeared; all connections are invalid. The call may have executed.
如果我从 btnCancel_Click()
中删除 Unload Me
那么表单可以关闭并重新打开就好了,但是我第一次输入的任何数据仍然会在表单上并且不会被清除适当地。我想知道这是 Unload Me
错误还是我在初始化表单时需要重置所有表单控件?
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
' how was the form closed?
' vbFormControlMenu = X in corner of title bar
If CloseMode = vbFormControlMenu Then
' cancel normal X button behavior
Cancel = True
' run code for click of Cancel button
btnCancel_Click
End If
End Sub
'******************************************************************
Private Sub btnCancel_Click()
mbCancel = True
Me.Hide
Unload Me
End Sub
'*********************************************************************
Private Sub UserForm_Initialize()
'Populate values for 2 combo boxes
lastEmp = Sheets("Form_Ref").Cells(Rows.Count, 1).End(xlUp).Row
Me.cmbBoxEmpName.List = Sheets("Form_Ref").Range("A2:A" & lastEmp).Value
lastBld = Sheets("Form_Ref").Cells(Rows.Count, 2).End(xlUp).Row
Me.cmbBoxBuildingName.List = Sheets("Form_Ref").Range("B2:B" & lastBld).Value
End Sub
'******************************************************************
Public form As New CheckOutForm
Sub testFormOptions()
'Button pressed within Excel will start program and show the userform
form.Show
End Sub
这是最简单快速但又脏的解决方案:
从代码中删除 Public form As New CheckOutForm
。然后在testFormOptions()
:
中添加
Sub testFormOptions()
Dim form As New CheckOutForm
form.Show
End Sub
一些不太好的VBA books/tutorials甚至会有点像这样,但这很残酷:
Sub testFormOptions()
CheckOutForm.Show
End Sub
无论如何,现在表单中预定义值的问题已经解决了。
对于干净且不那么简单的解决方案,考虑围绕以下形式编写一个 MVC 框架:
https://codereview.stackexchange.com/questions/154401/handling-dialog-closure-in-a-vba-user-form
this blogpost(免责声明 - 我的!),这几乎说明了上面 link 的建议,但它没有问题中的错误。
如果你执行Unload
,你会销毁表单对象。换句话说,你的(全局)变量 form
变得无效,如果你发出一个新的 form.show
,你会得到运行时错误。
另一方面,当您只是 unhide
表单时,表单对象保持有效(只是当前不可见)并且所有控件都保持其值。
要么通过在显示表单时重置所有控件(使用 UserForm_Activate
-事件)进行一些内务处理,要么每次要显示它时都必须创建一个新的表单对象(我强烈建议不要使用名称 form
作为变量名以避免混淆。
Sub testFormOptions()
dim myForm as CheckOutForm
myForm.Show
End Sub
问题:我正在构建一个具有 'Submit' 和 'Cancel' 按钮的用户表单。如果用户点击 'Cancel' 按钮,我希望整个表单清除所有输入的数据并关闭表单,但如果用户点击顶部的红色 'X',我也试图构建相同的功能右上角。我不清楚我需要在哪里卸载表格。我目前将它放在 btnCancel_Click()
方法中,我可以启动表单,输入一些数据并点击取消,它会关闭表单。
但是,当我第二次尝试重新启动表单时,我收到一条错误消息(我附上了该消息的图片),内容为
"Run-Time error '-2177418105 (80010007): Automation Error - The Callee (server [not server application]) is not available and disappeared; all connections are invalid. The call may have executed.
如果我从 btnCancel_Click()
中删除 Unload Me
那么表单可以关闭并重新打开就好了,但是我第一次输入的任何数据仍然会在表单上并且不会被清除适当地。我想知道这是 Unload Me
错误还是我在初始化表单时需要重置所有表单控件?
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
' how was the form closed?
' vbFormControlMenu = X in corner of title bar
If CloseMode = vbFormControlMenu Then
' cancel normal X button behavior
Cancel = True
' run code for click of Cancel button
btnCancel_Click
End If
End Sub
'******************************************************************
Private Sub btnCancel_Click()
mbCancel = True
Me.Hide
Unload Me
End Sub
'*********************************************************************
Private Sub UserForm_Initialize()
'Populate values for 2 combo boxes
lastEmp = Sheets("Form_Ref").Cells(Rows.Count, 1).End(xlUp).Row
Me.cmbBoxEmpName.List = Sheets("Form_Ref").Range("A2:A" & lastEmp).Value
lastBld = Sheets("Form_Ref").Cells(Rows.Count, 2).End(xlUp).Row
Me.cmbBoxBuildingName.List = Sheets("Form_Ref").Range("B2:B" & lastBld).Value
End Sub
'******************************************************************
Public form As New CheckOutForm
Sub testFormOptions()
'Button pressed within Excel will start program and show the userform
form.Show
End Sub
这是最简单快速但又脏的解决方案:
从代码中删除 Public form As New CheckOutForm
。然后在testFormOptions()
:
Sub testFormOptions()
Dim form As New CheckOutForm
form.Show
End Sub
一些不太好的VBA books/tutorials甚至会有点像这样,但这很残酷:
Sub testFormOptions()
CheckOutForm.Show
End Sub
无论如何,现在表单中预定义值的问题已经解决了。
对于干净且不那么简单的解决方案,考虑围绕以下形式编写一个 MVC 框架:
https://codereview.stackexchange.com/questions/154401/handling-dialog-closure-in-a-vba-user-form
this blogpost(免责声明 - 我的!),这几乎说明了上面 link 的建议,但它没有问题中的错误。
如果你执行Unload
,你会销毁表单对象。换句话说,你的(全局)变量 form
变得无效,如果你发出一个新的 form.show
,你会得到运行时错误。
另一方面,当您只是 unhide
表单时,表单对象保持有效(只是当前不可见)并且所有控件都保持其值。
要么通过在显示表单时重置所有控件(使用 UserForm_Activate
-事件)进行一些内务处理,要么每次要显示它时都必须创建一个新的表单对象(我强烈建议不要使用名称 form
作为变量名以避免混淆。
Sub testFormOptions()
dim myForm as CheckOutForm
myForm.Show
End Sub