在 Excel 中自动单击“确定”的 VBA 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27360514/
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
VBA Code to auto-click OK in Excel
提问by Achintya Paradkar
I have made an Excel macro that gives results using solver. After the result is generated it displays a dialogue box to accept the result. As I have 50 solver results in the macro, I have to click OK 50 times.
我制作了一个 Excel 宏,可以使用求解器给出结果。结果生成后,它会显示一个对话框来接受结果。由于宏中有 50 个求解器结果,因此我必须单击“确定”50 次。
Is there some tweak in VBA that can be done to automatically click OK or press enter?
VBA 中是否有一些调整可以自动单击“确定”或按 Enter 键?
采纳答案by Achintya Paradkar
SendKeys "~" did the work for me. Type this code after the solversolve command and enter gets automatically typed pressing OK and closing the box.
SendKeys "~" 为我完成了这项工作。在solversolve 命令之后输入此代码,然后按OK 并关闭框自动输入回车。
回答by shir
there is an easier way. type this in your code: "solversolve(true)" True makes solver return the results without displaying the Solver Results dialog box
有一个更简单的方法。在您的代码中键入以下内容:“solversolve(true)” True 使求解器返回结果而不显示“求解器结果”对话框
回答by Will Rickards
There is a SendKeys command in vba. This allows you to simulate keystrokes.
vba 中有一个 SendKeys 命令。这允许您模拟击键。
回答by Bryan M
Not sure, but try Application.DisplayAlerts
不确定,但试试 Application.DisplayAlerts
That is, modify your macro:
也就是说,修改你的宏:
Application.DisplayAlerts = False 'turns off dialog prompts
Application.DisplayAlerts = False '关闭对话框提示
your_macro_code_here
your_macro_code_here
Application.DisplayAlerts = True 'turns back on dialog prompts for normal usage
Application.DisplayAlerts = True '重新打开正常使用的对话框提示