我的“当前”工作簿中有一个关闭“其他”工作簿的代码。然而,“其他”工作簿有一个代码,当您关闭它时,将执行一个代码。
我想要的是,当我从“当前”工作簿运行代码时,它将绕过或忽略另一个工作簿的代码。
当前工作簿代码:
NewName = "v10.0_" & Left(CurFile, Len(CurFile) - 5)
pseudo = "'"
MacroName = "Workbook_BeforeClose"
Call SaveAs(wbk_r, NewName, FolderPath) 'Save the file to destination folder
Application.Run pseudo & NewName & "'!" & MacroName & ".xlsm", Cancel
Run Code Online (Sandbox Code Playgroud)
其他工作簿代码:'''
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("All data input will be permanently saved in the file database! Are you sure for your update?", vbYesNo) = vbNo Then
Exit Sub
Else
Call SaveChangesA
ThisWorkbook.Saved = True
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
'''
用于Application.EnableEvents = False关闭事件。因此,如果您关闭工作簿,该事件Workbook_BeforeClose将不会运行。
请确保事后(或发生错误时)将其打开,Application.EnableEvents = True否则整个 Excel 中的事件将保持关闭状态。
给出错误处理的示例
\nOption Explicit\n\nPublic Sub Example()\n \n \' your inital code before closing the workbook goes here \xe2\x80\xa6\n\n \n Application.EnableEvents = False\n On Error Goto ERR_EVENTS\n \n \' close your workbook here (nothing else)\n\n On Error Goto 0 \' re-enable error reporting for the rest of your code!\n Application.EnableEvents = True\n\n\n \' your other code goes here \xe2\x80\xa6\n\n Exit Sub \' run the following only in case of an error\nERR_EVENTS:\n\n \' Enable events!\n Application.EnableEvents = True\n \n Err.Raise Err.Number \' raise an error in case there was one (otherwise you will never know that something went wrong).\nEnd Sub\nRun Code Online (Sandbox Code Playgroud)\n有关详细信息,请参阅VBA 错误处理 \xe2\x80\x93 完整指南。
\n