我使用DoEvents强制更新状态栏(或工作表中的某个单元格)中的进度指示器,如下面的示例代码所示。但是屏幕不刷新,或者在某个时刻停止刷新。任务最终完成,但进度条无用。
为什么DoEvents不“做事件”?我还能做些什么来强制屏幕更新?
编辑:我在Windows XP上使用Excel2003。
这是earlier question的后续;感谢Robert Mearns的回答和下面的示例代码。
Sub ProgressMeter()
Dim booStatusBarState As Boolean
Dim iMax As Integer
Dim i As Integer
iMax = 100
Application.ScreenUpdating = False
''//Turn off screen updating
booStatusBarState = Application.DisplayStatusBar
''//Get the statusbar display setting
Application.DisplayStatusBar = True
''//Make sure that the statusbar is visible
For i = 1 To iMax ''// imax is usually 30 or so
fractionDone = CDbl(i) / CDbl(iMax)
Application.StatusBar = Format(fractionDone, "0%") & " done..."
''// or, alternatively:
''// statusRange.value = Format(fractionDone, "0%") & " done..."
''// Some code.......
DoEvents
''//Yield Control
Next i
Application.DisplayStatusBar = booStatusBarState
''//Reset Status bar display setting
Application.StatusBar = False
''//Return control of the Status bar to Excel
Application.ScreenUpdating = True
''//Turn on screen updating
End Sub发布于 2010-10-08 21:32:05
我发现DoEvents并不总是完全可靠的。我建议尝试两种不同的方式。
首先,尝试在状态栏更新之后立即发出DoEvents调用(即,在Some code ....行之前)。
如果这不起作用,我发现在某些情况下,使用睡眠API是获得处理器时间的更可靠的方法。如果DoEvents没有像我希望的那样工作,这通常是我尝试的第一件事。您需要在模块的顶部添加以下行(在函数之外):
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)然后添加下面这一行来代替或补充DoEvents
Sleep 1 'This will pause execution of your program for 1 ms如果1ms不起作用,您可以尝试使用睡眠来增加暂停程序的时间长度。
发布于 2012-02-14 14:52:27
我发现,在更新状态栏之前调用DoEvents,而不是在更新状态栏之后,会产生更多可预测的/所需的结果。
上面的代码片段如下:
fractionDone = CDbl(i) / CDbl(iMax)
DoEvents
Application.StatusBar = Format(fractionDone, "0%") & " done..."https://stackoverflow.com/questions/3863641
复制相似问题