【发布时间】:2017-10-20 16:41:36
【问题描述】:
编辑:我自己想通了。我觉得很傻,但是用“End”替换“Exit Sub”效果很好。
背景:我有一个 Sub,它使用“调用”函数在一个 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”效果很好。
-
请为您的问题写一个答案,以便其他人可以找到类似问题的解决方案
-
更整洁的是将 Format_MIUL 转换为一个函数,该函数在保存时返回 TRUE,否则返回 False,因此您可以这样调用它: If Format_Miu Then 'Other calls End If
-
@jkpieterse gah,刚刚看到这条评论