更新VBA循环中的Excel状态栏

我有一个macros代码来执行数据修剪。 以下是我的代码:

Sub StatusBarExample() Application.ScreenUpdating = False Application.DisplayStatusBar = True Application.StatusBar = "Trimming..." Dim x As Long x = 1 Do While Cells(x, 1) <> "" Cells(x, 2).Select ActiveCell.FormulaR1C1 = "=trim(RC[-1])" x = x + 1 Loop Application.Wait Now + TimeValue("00:00:02") Application.StatusBar = False End Sub 

数据的数量总是在变化,这就是为什么我使用Do While循环的原因。 有了这个代码,我只能看到一个消息“修剪”,而不知道百分比的进展。 任何人都可以帮助我改进这个代码?

我会采取这种方法。 我更喜欢for循环,这会告诉你在状态栏中%完成。

 Sub StatusBarExample() Application.ScreenUpdating = False Application.DisplayStatusBar = True Application.StatusBar = "Trimming..." Dim x As Long Dim lRow as Long Dim prePct as Single, newPct as Single newPct = 0 prePct = 0 With ThisWorkbook.Sheets("Sheet Name Goes Here") lRow = .Range("A" & .Rows.Count).End(xlUp).Row For x = 1 to lRow .Cells(x,2).FormulaR1C1 = "=trim(RC[-1])" newPct = x/lRow If newPct > prePct + .01 DoEvents prePct = newPct End If Application.StatusBar = "Trimming..." & Format(x/lRow,"0.00%") & " Complete" Next x Application.StatusBar = False Application.ScreeUpdating = True End Sub 

感谢您的build议编辑。 我做了一些改变。 在行中If newPct > prePct + .01 ,则可能需要根据数据量将.01更改为其他值。