用户窗体“ A”上的按钮打开用户窗体“ XYZ”。通过检查Left(ActiveWorkbook.Name,4)<>“ ABC-”,开始“ XYZ”的打开代码。
但是,如果我关闭一个打开的工作簿,然后尝试打开用户窗体“ XYZ”,则子程序崩溃。我知道原因是测试-因为没有ActiveWorkbook。我不知道如何添加“是否有任何工作簿处于活动状态?-REGARDLESS OF THE NAME”的支票。
我已经检查过Web,但是我看到的所有情况都假设您正在检查特定的工作簿名称,而不仅仅是任何工作簿都处于活动状态。
Sub XYZ_Actions_form()
'
' FIRST LINE CRASHES IF THERE IS NO ACTIVE WORKBOOK.
' That means you can't CLOSE the active workbook and then open
' the XYZ Actions form.
'
' Needs an extra If to check for "no active workbook".
If Left(ActiveWorkbook.name, 4) = "ABC-" Then
MsgBox ("Can't run XYZ Actions on an ABC workbook!")
ElseIf Has(ActiveWorkbook.name, "+PAYMENTS+") Then
MsgBox ("Can't run XYZ Actions on an ABC PAYMENTS workbook!")
Else
Load XYZ_Actions
End If
End Sub
如果没有活动的工作簿,我仍然应该能够加载XYZ_Actions表单。当前的第一行应为ElseIf,新的第一行应为
If {no active workbook} Then
Load XYZ_Actions
解决方案,来自其他帖子:
If Application.Workbooks.Count < 1 Then
' Do nothing
ElseIf {my other tests}
Goto Do_not_load_userform
End If
{code that loads the userform}
Do_not_load_userform:
End Sub