关闭UserForm时未设置对象变量

时间:2014-11-22 11:30:13

标签: vba excel-vba userform excel

当我的Excel 2007项目中的用户表单(Object variable or With block variable not set)通过单击红色X或在UF_Main语句后卸载时,我遇到错误Unload Me错误。

我设法将错误隔离到模块中的以下代码:

Public Sub fProgressExit()

intProVol = 0
intProStep = 0
Unload UF_Progress
UF_Main.Show 'This line is causing the error'

End Sub

删除该行可防止发生错误。

当卸载userform UF_Main时,没有对上述过程的引用,并且已经卸载了与该过程相关的用户表单(UF_Progress)。

为什么在用户表单关闭时fProgressExit未执行时会收到错误?

1 个答案:

答案 0 :(得分:0)

如果您不希望表单关闭,可以使用UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)的属性:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 'before unloaded
Select Case CloseMode
    Case 0: 'Close initiated by the little [X] button top right of the UserForm
        'To prevent closing this way : cancel=true
    Case 1: 'close initiated by Unload UserForm
    Case 2, 3: 'close initiated by windows(2) / Task manager(3)
End Select
End Sub

请注意:UserForm_QueryClose' is activated before the Form closes, UserForm_Terminate`在Form已经关闭+ Unloaded后被激活。