Excel VBA反向进度条

我正在使用进度条向用户显示正在运行的macros的状态,但是因为macros删除的行向后运行For i = lastrow To 2 Step -1 ,这意味着我的进度条从100%运行到2 %。

我只有跟着i计数了,倒计时才能让进度读取信息,所以对用户来说显然是在计数呢?

 Sub update() Dim lastRow As Integer, email As String, pctCompl As Single lastRow = Sheets("Sheet1").Range("C5000").End(xlUp).Row For i = lastRow To 2 Step -1 email = Trim(Cells(i, 3).Value) Set c = Sheets("Sheet3").Range("A:A").Find(email, LookIn:=xlValues) If Not c Is Nothing Then Cells(i, 1).EntireRow.Delete End If pctCompl = i progress pctCompl Next i End Sub Sub progress(pctCompl As Single) UserForm1.Text.Caption = pctCompl & "% Completed" UserForm1.Bar.Width = pctCompl * 2 DoEvents End Sub 

更换

 progress pctCompl 

 progress Abs(Round(i / lastRow * 100, 0) - 100) + 1 

不需要将i设置为名为pctCompl的variables – 只需将值传递给过程即可。

作为一个开箱即用的答案,您可能需要考虑以下内容:

 Sub update() With Worksheets("Sheet1") With .Range("C2:C" & .Cells(.Rows.Count, 3).End(xlUp).Row) With .offset(, .Parent.UsedRange.Columns.Count) .FormulaR1C1 = "=iferror(match(RC3,Sheet3!C1,0),"""")" .value = .value .SpecialCells(xlCellTypeBlanks).EntireRow.Delete .Delete End With End With End With end Sub 

根本不需要任何进度条…