Excel VBA:按下命令按钮时的确认 [英] Excel VBA: Confirmation on Pressing CommandButton
问题描述
按下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?"
我想有三个选择:
- 是-关闭弹出窗口并执行CommandButton Macro
- 否-这将关闭弹出窗口,并且不做任何更改
- 保存-关闭弹出窗口并打开另存为"(不执行宏)
我真的不知道从哪里开始.你能帮我个忙吗?
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屋!