【问题标题】:xlDialogSaveAs - End ALL code if "cancel" is selectedxlDialogSaveAs - 如果选择“取消”,则结束所有代码
【发布时间】: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,刚刚看到这条评论

标签: vba excel


【解决方案1】:

Call 关键字已废弃 20 年,您可以将其删除。

End 关键字将有效地结束执行,但它几乎是一个红色的“自毁”大按钮,如果代码结构正确,您实际上永远不需要使用它。

看起来Format_MIUL 是一个Sub 过程。将其设为Function返回一个Boolean 值,告诉调用者是否可以继续,或者是否应该取消其余操作:

Private Function Format_MUIL() As Boolean
    '...
    '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

    'return False if userResponse isn't a filename, True otherwise:
    Format_MUIL = Not VarType(userResponse) = vbBoolean
End Function

现在代替这个:

Call Format_MIUL

调用者可以这样做:

If Not Format_MIUL Then Exit Sub

然后就可以了,无需按下任何自毁按钮即可优雅退出。

【讨论】:

  • 有趣...我不会做太多 VBA,这里的其他用户并不真正需要这个代码,只是一个很好的工具,所以我对正确结构化代码的知识非常有限。不过,我不太清楚如何用函数替换所有“调用”关键字。不是每个 Sub 都有这样的对话框。另外,在我的 Format MIUL 代码中,我应该将“Format MIUL = Not VarType...”放在哪里?
  • 我没有说“用函数替换Call 关键字”,我说的是删除Call 关键字 - 它们是多余的。 Call DoSomethingDoSomething 完全相同。是的,Format_MIUL = ... 进入Format_MIUL 函数 - 这就是你在 VBA 中设置函数返回值的方式,通过分配给它的标识符。
  • 好吧,但我是要留下我的原始代码'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 并在最后添加Format_MUIL = Not VarType(userResponse) = vbBoolean 吗?无论如何,我会看看听起来不错的 Rubberduck。
  • @CC268 我已经编辑澄清,希望它有所帮助。 Format_MUIL = Not VarType(userResponse) = vbBoolean 部分实际上替换了整个 If userResponse = False 块。
  • 太好了,谢谢!我可以为我“呼叫”的所有其他潜艇做到这一点吗?即使我没有在其他人中做任何 xlDialogSaveAs 吗?在大多数情况下,它们只是进行一般格式化等的 Subs。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 2018-06-26
  • 2016-09-28
  • 1970-01-01
  • 2018-07-27
  • 2011-10-09
相关资源
最近更新 更多