gpt4 book ai didi

vba - 在 Excel VBA 中强制屏幕更新

转载 作者:行者123 更新时间:2023-12-01 16:44:57 29 4
gpt4 key购买 nike

我的 Excel 工具执行一项较长的任务,我试图通过在状态栏或工作表中的某些单元格中提供进度报告来善待用户,如下所示。但屏幕不刷新,或在某个时刻停止刷新(例如 33%)。任务最终完成,但进度条没用。

我可以做什么来强制屏幕更新?

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.......

Next i

我使用的是 Excel 2003。

最佳答案

在循环内添加一个 DoEvents 函数,请参见下文。

您可能还想确保状态栏对用户可见,并在代码完成时重置它。

Sub ProgressMeter()

Dim booStatusBarState As Boolean
Dim iMax As Integer
Dim i As Integer

iMax = 10000

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

关于vba - 在 Excel VBA 中强制屏幕更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3735378/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com