Excel VBA:按下命令按钮时的确认 [英] Excel VBA: Confirmation on Pressing CommandButton

查看:764
本文介绍了Excel VBA:按下命令按钮时的确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下CommandButton时,我会弹出一个对话框,询问这些更改无法撤消.建议在继续操作之前保存副本.您要继续吗?"

Upon pressing my CommandButton, I would like to have a pop-up that asks "These changes cannot be undone. It is advised to save a copy before proceeding. Do you wish to proceed?"

我想有三个选择:

  1. 是-关闭弹出窗口并执行CommandButton Macro
  2. 否-这将关闭弹出窗口,并且不做任何更改
  3. 保存-关闭弹出窗口并打开另存为"(不执行宏)

我真的不知道从哪里开始.你能帮我个忙吗?

I don't really know where to start with this. Could you please give me a hand?

真的非常感谢你.

推荐答案

您可以使用消息框,但这有些限制.您可以稍微改一下该问题以使用vbYesNoCancel按钮,因为Save As不是消息框上的可选按钮.

You can use a message box, but that is somewhat limited. You can rephrase the question slightly to use the vbYesNoCancel buttons, since Save As is not an optional button on Message Box.

然后您可以使用消息框的结果单击按钮:

Then you can work with the result of the message box button-click:

Dim mbResult as Integer
mbResult = MsgBox("These changes cannot be undone. Would you like to save a copy before proceeding?", _
 vbYesNoCancel)

Select Case mbResult
    Case vbYes
        'Modify as needed, this is a simple example with no error handling:
        With ActiveWorkbook
            If Not .Saved Then .SaveAs Application.GetSaveAsFilename()
        End With
    Case vbNo
        ' Do nothing and allow the macro to run

    Case vbCancel
        ' Do NOT allow the macro to run
        Exit Sub

End Select

这篇关于Excel VBA:按下命令按钮时的确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆