Excel VBA:确认按下CommandButton
当按下我的CommandButton时,我想要一个popup窗口,询问“这些更改不能撤消,build议在继续之前保存一份副本,你想继续吗?
我想有三个select:
- 是的 – popup窗口closures并执行CommandButtonmacros
- 否 – 这将closurespopup窗口并且不会改变任何内容
- 保存 – closurespopup窗口并打开“另存为”(不执行macros)
我真的不知道从哪里开始。 你能帮我一下吗?
非常感谢你。
您可以使用消息框,但这是有限的。 您可以稍微改变问题以使用vbYesNoCancel
button,因为“ Save As
为”不是消息框上的可选button。
然后您可以使用消息框的结果button单击:
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
我build议你把代码放在你的macros的顶部来问这个问题并回答答案。
这看起来像这样:
Sub YourMacro() if MsgBox("These changes cannot be undone. It is advised to save a copy before proceeding. Do you wish to proceed?", vbYesNo + vbQuestion) = vbNo then exit sub end if ... the rest of your macro.
请注意,这不会给用户保存选项。 你不能用标准的MsgBox来做到这一点。 如果你想这样做,你将需要创build自己的用户窗体,显示而不是MsgBox,并响应用户推送的用户窗体中的button。 对于你来说,这比使用MsgBox要多得多,但是如果你想让你的用户界面变得有趣的话,这可能是值得的。