編輯:我想出了自己。我覺得非常愚蠢,但用「結束」取代「退出小組」完美。xlDialogSaveAs - 如果選擇「取消」,結束所有代碼
背景:我有一個使用「調用」函數在一個Sub中運行多個子的Sub(請參見下面的代碼#1)。
Option Explicit
Sub MIUL_Run_All()
Dim StartTime As Double
Dim SecondsElapsed As String
'Remember time when macro starts
StartTime = Timer
Call OptimizeCode_Begin
Call Format_MIUL
Call Custom_Sort_MIUL
Call Insert_Process_List
Call Format_Process_List
Call OptimizeCode_End
'Determine how many seconds code took to run
SecondsElapsed = Format((Timer - StartTime)/86400, "ss")
'Notify user in seconds
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
End Sub
我被叫出第一個代碼,「Format_MIUL」,提示用戶保存文件,使用下面的代碼行(參見下面的代碼#2)。這段代碼有效,但問題是,如果用戶按下「取消」按鈕,主子代碼(上面代碼#1)中調用的其餘代碼將繼續運行。如果用戶按下取消按鈕,我希望所有代碼都停止。我似乎無法弄清楚如何做到這一點。
'Save file as .xlsm
MsgBox " Save as Excel Workbook (.xlsx)!"
Dim userResponse As Boolean
On Error Resume Next
userResponse = Application.Dialogs(xlDialogSaveAs).Show(, 51)
On Error GoTo 0
If userResponse = False Then
Exit Sub
Else
End If
任何幫助,非常感謝。
我覺得非常愚蠢,但用「End」替換「Exit Sub」是完美的。 – CC268
請爲你的問題寫一個答案,以便其他人可以找到類似問題的解決方案 – Maldred
Neater將Format_MIUL轉換爲保存時返回TRUE的函數,否則你可以這樣調用它: If Format_Miu Then 'Other調用 End If – jkpieterse