为什么即使代码运行良好,VBA 也会说“(无响应)”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28487969/
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
Why does VBA say "(Not Responding)" even when code is running fine?
提问by Kevin Presley
I've run into this issue with many different VBA programs, so I'm thinking I may have some incorrect settings in Excel or the VBA editor. Honestly, I'm pretty clueless after a lot of web searching.
我在许多不同的 VBA 程序中都遇到过这个问题,所以我想我在 Excel 或 VBA 编辑器中可能有一些不正确的设置。老实说,经过大量网络搜索后,我非常无能为力。
Basically, I'll be running a normal piece of code and the application (both Excel and the VBA Editor) will go into (Not Responding) mode. However the code is running fine. Once the code is finished, the application will no longer be in non-responsive mode and everything returns to normal
基本上,我将运行一段普通代码,应用程序(Excel 和 VBA 编辑器)将进入(无响应)模式。但是代码运行良好。代码完成后,应用程序将不再处于无响应模式,一切恢复正常
Really kinda frustrating on users who think something is wrong when really it's just taking a while to run the code.
对于那些认为有问题的用户来说真的有点令人沮丧,而实际上运行代码只需要一段时间。
Any help would be great! Thanks!
任何帮助都会很棒!谢谢!
回答by mti2935
Try putting DoEvents
statements in your code, so that DoEvents
is executed every so often during a lengthy process. See http://support.microsoft.com/kb/118468/en-us
尝试将DoEvents
语句放入您的代码中,以便DoEvents
在漫长的过程中每隔一段时间执行一次。请参阅http://support.microsoft.com/kb/118468/en-us
This will return control to the OS, so that the OS can do what it needs to do while your program is running, and this should prevent the OS from thinking that your program is not responding.
这会将控制权返回给操作系统,以便操作系统可以在您的程序运行时做它需要做的事情,这应该可以防止操作系统认为您的程序没有响应。
回答by Eric J.
Basically, I'll be running a normal piece of code and the application (both Excel and the VBA Editor) will go into (Not Responding) mode. However the code is running fine. Once the code is finished, the application will no longer be in non-responsive mode and everything returns to normal
基本上,我将运行一段普通代码,应用程序(Excel 和 VBA 编辑器)将进入(无响应)模式。但是代码运行良好。代码完成后,应用程序将不再处于无响应模式,一切恢复正常
The User Interface is not responding because you are doing a bunch of processing in response to a UI event.
用户界面没有响应,因为您正在执行大量处理以响应 UI 事件。
If this is acceptable to you and your users, all's well enough. If not, consider doing the work on a background thread.
如果这对您和您的用户来说是可以接受的,那就足够了。如果没有,请考虑在后台线程上进行工作。