如何在 VBA Excel 2010 中显示等待屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4948739/
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-11 12:38:24  来源:igfitidea点击:

How to show a wait screen in VBA Excel 2010

excel-vbavbaexcel

提问by QueueHammer

I have macros that are running on some workbooks and a status screen that shows their progress so the user does not have to stare at a blank screen or the loading screen of Excel. Or so I though. The status page works, it will update as the macros run, but excel will not show any of it until after the macros finish running. How do I show my status screen?

我有一些工作簿上运行的宏和一个显示其进度的状态屏幕,因此用户不必盯着空白屏幕或 Excel 的加载屏幕。或者我虽然如此。状态页面有效,它会随着宏的运行而更新,但 excel 直到宏完成运行后才会显示任何内容。如何显示我的状态屏幕?

status = "SheetName"

Private Sub Workbook_Open()
    'Make sure that the screen is active
    Sheets(status).Activate
    Sheets(status).Select
End Sub

回答by chris neilsen

If the purpose of your status screen is simply to give some feedback while your macros are running, a quick and easy alternative is to use the Status Bar. Here's a sample:

如果您的状态屏幕的目的只是在您的宏运行时提供一些反馈,一个快速而简单的替代方法是使用状态栏。这是一个示例:

Sub YourMacro()
    Dim StatusOld As Boolean, CalcOld As XlCalculation

    ' Capture Initial Settings
    StatusOld = Application.DisplayStatusBar

    '      Doing these will speed up your code
    CalcOld = Application.Calculation
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.EnableEvents = False

    On Error GoTo EH

    ' Your code...

    ' Every so often while your code is running
    Application.StatusBar = "Something Useful..." 

    ' After all your code is done
CleanUp:
    ' Put things back like they were
    Application.StatusBar = False
    Application.Calculation = CalcOld
    Application.DisplayStatusBar = StatusOld
    Application.ScreenUpdating = True
    Application.EnableEvents = True
Exit Sub
EH:

    ' Your error handler...

    GoTo CleanUp
End Sub

回答by Chris Rae

You have to call DoEvents after each update (VBA instruction). That will give processor time back to Excel for an instant, and it'll use it to redraw the screen. You do have to be a little careful with this as it'll also allow your user to click in the spreadsheet and mess things up.

您必须在每次更新(VBA 指令)后调用 DoEvents。这将使处理器时间立即返回给 Excel,并使用它来重绘屏幕。您必须对此小心一点,因为它还会允许您的用户单击电子表格并将事情搞砸。