vba Excel 2013 中的状态栏和屏幕更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28076343/
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
StatusBar and ScreenUpdate in Excel 2013
提问by JMichael
I've just gotten into Excel 2013 and am noticing some differences in how macros work (because Microsoft can't leave a good thing alone).
我刚刚进入 Excel 2013 并且注意到宏的工作方式存在一些差异(因为 Microsoft 不能单独留下一件好事)。
I've always used Application.ScreenUdpdating = False
at the start of long macros to speed run time. Generally in those instances I've also used Application.StatusBar = "random text based on macro"
to give myself (or other users) some sense of what's going on and/or how much work is left for the macro to complete.
我总是Application.ScreenUdpdating = False
在长宏的开头使用以加快运行时间。通常在这些情况下,我也习惯于Application.StatusBar = "random text based on macro"
让自己(或其他用户)了解正在发生的事情和/或宏需要完成多少工作。
In 2013, I've learned (from Application.Statusbar not working as expected in Excel 2013) that DoEvents
is now required. However, when I added in DoEvents
after my StatusBar updates, it seems to turn screen updating back on, unless a window outside Excel (ex. an open File Explorer window) is given the focus.
在 2013 年,我了解到(从Application.Statusbar 没有按预期在 Excel 2013 中工作)DoEvents
现在需要。但是,当我在DoEvents
StatusBar 更新后添加时,它似乎重新打开屏幕更新,除非 Excel 外部的窗口(例如打开的文件资源管理器窗口)获得焦点。
Another issue I've noticed is that with DoEvents in the code I can't seem to break the macro manually (i.e. holding ESC doesn't stop the code).
我注意到的另一个问题是,在代码中使用 DoEvents 时,我似乎无法手动破坏宏(即按住 ESC 不会停止代码)。
So a few questions: A) Is DoEvents really required? B) Is there a way to prevent ScreenUpdates from getting turned back "on" by DoEvents? C) How do you manually break code running with DoEvents in it?
所以有几个问题:A) 真的需要 DoEvents 吗?B) 有没有办法防止屏幕更新被 DoEvents 重新“打开”?C) 您如何手动中断带有 DoEvents 的代码?
I can provide the current macro I'm working on if it will help, but since I'm more interested in the general concepts I didn't want to provide specifics for any readers to get bogged down in.
如果有帮助,我可以提供我正在处理的当前宏,但由于我对一般概念更感兴趣,我不想为任何读者提供细节而陷入困境。
Thanks in advance!
提前致谢!
回答by Ian
I ran into a similar problem today so I thought I would share what I found while debugging the issue:
我今天遇到了类似的问题,所以我想我会分享我在调试问题时的发现:
For me I noticed that the status bar was reverting to a previous status bar method while my macro was running. I found that the old message was the last status bar message printed before I disable screen updating. However, new status bar messages were printing temporarily so screen updating wasn't preventing new messages from displaying. I traced the issue to a spot in the macro where I run an executable using:
对我来说,我注意到状态栏在我的宏运行时恢复到以前的状态栏方法。我发现旧消息是在禁用屏幕更新之前打印的最后一条状态栏消息。但是,新的状态栏消息正在临时打印,因此屏幕更新不会阻止显示新消息。我将问题追溯到宏中的某个位置,在该位置我使用以下命令运行可执行文件:
Set wsh = VBA.CreateObject("WScript.Shell")
errorCode = wsh.Run(exeCMD, windowStyle, waitOnReturn)
It appears that calling the executable disrupted the status bar display and excel reverted to displaying whatever status bar message was showing before screen updating was disabled. To solve this I reactivated screen updating before running the executable and then disabled screen updating once the executable was run:
似乎调用可执行文件会破坏状态栏显示,并且 excel 恢复显示在禁用屏幕更新之前显示的任何状态栏消息。为了解决这个问题,我在运行可执行文件之前重新激活了屏幕更新,然后在运行可执行文件后禁用了屏幕更新:
Set wsh = VBA.CreateObject("WScript.Shell")
Application.ScreenUpdating = True
errorCode = wsh.Run(exeCMD, windowStyle, waitOnReturn)
Application.ScreenUpdating = False
Hope this helps someone out there.
希望这可以帮助那里的人。