vba 添加“你确定吗?” 到我的 excel 按钮,我该怎么办?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8569389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:18:57  来源:igfitidea点击:

Add "Are you sure?" to my excel button, how can I?

excelvbaexcel-vbadialog

提问by aslum

I have a button on my form that clears the entire 8 sheet workbook. I do want to clear it occasionally, but I would hate to do it by accident. I've tried googling it, but every result I've found assumes I have a much firmer grasp of VBA than I do. How can I make it so when the button is clicked a Dialog box pops up saying "This will erase everything! Are you sure? [Continue] [Cancel]"? Thanks.

我的表单上有一个按钮,可以清除整个 8 页工作簿。我确实想偶尔清除它,但我不想不小心这样做。我试过用谷歌搜索它,但我发现的每一个结果都假设我对 VBA 的掌握比我更牢固。我怎样才能做到当点击按钮时弹出一个对话框说“这将清除所有内容!你确定吗?[继续][取消]”?谢谢。

回答by Matt Donnan

On your existing button code, simply insert this line before the procedure:

在您现有的按钮代码上,只需在程序之前插入这一行:

If MsgBox("This will erase everything! Are you sure?", vbYesNo) = vbNo Then Exit Sub

This will force it to quit if the user presses no.

如果用户按 no,这将强制它退出。

回答by Jesse

Create a new sub with the following code and assign it to your button. Change the "DeleteProcess" to the name of your code to do the deletion. This will pop up a box with OK or Cancel and will call your delete sub if you hit ok and not if you hit cancel.

使用以下代码创建一个新子并将其分配给您的按钮。将“DeleteProcess”更改为您的代码名称以进行删除。这将弹出一个带有 OK 或 Cancel 的框,如果您点击 OK 而不是如果您点击取消,它将调用您的删除子。

Sub AreYouSure()

Dim Sure As Integer

Sure = MsgBox("Are you sure?", vbOKCancel)
If Sure = 1 Then Call DeleteProcess

End Sub

Jesse

杰西

回答by alyon2002

Just make a custom userform that is shown when the "delete" button is pressed, then link the continue button to the actual code that does the deleting. Make the cancel button hide the userform.

只需制作一个在按下“删除”按钮时显示的自定义用户表单,然后将继续按钮链接到执行删除的实际代码。使取消按钮隐藏用户窗体。