在 VBA 中暂停执行并等待按键的简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11457768/
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
Easy way to pause execution and wait for key press in VBA?
提问by Jeremy
I would like to have a VBA macro pause its execution and wait for the user to press a key before continuing. Is there a quick way to accomplish this, or am I better off using a window popup?
我想让 VBA 宏暂停其执行并等待用户在继续之前按下一个键。有没有一种快速的方法来完成这个,或者我最好使用一个窗口弹出窗口?
回答by Daniel
A warning: In my experience, unless your VBA specifically includes DoEvents statements, Office applications are unresponsive while VBA is running.
警告:根据我的经验,除非您的 VBA 专门包含 DoEvents 语句,否则 Office 应用程序在 VBA 运行时无响应。
If you only want to interrupt a process temporarily until the user is ready, then using a pop-up is by far the simplest method.
如果您只想在用户准备好之前暂时中断进程,那么使用弹出窗口是迄今为止最简单的方法。
Adding something like the following would have the desired result:
添加类似以下内容将获得所需的结果:
Msgbox "Click to go on", , "Example"
The user would click to continue or could hit enter or space.
用户将单击以继续或可以按 Enter 或空格键。
If you wanted a more specific pop-up, loading a modal userform (in Excel) or a modal form (in Access) from code will halt the suspend the calling code until the form is closed.
如果您想要更具体的弹出窗口,从代码加载模式用户表单(在 Excel 中)或模式表单(在 Access 中)将暂停调用代码,直到表单关闭。
Honestly, I would resist including a pause unless it's absolutely necessary. Any unnecessary interuption is an annoyance and hinderance to efficiency. If you simply want to inform the user about what is happening you can update the screen to explain what the code is doing as it performs actions.
老实说,除非绝对必要,否则我会拒绝暂停。任何不必要的中断都是对效率的烦恼和阻碍。如果您只是想通知用户正在发生的事情,您可以更新屏幕以解释代码在执行操作时正在做什么。