Excel VBA反向进度条

时间:2016-07-14 12:22:49

标签: excel vba excel-vba

我正在使用进度条向用户显示正在运行的宏的状态,但是因为宏删除了它正在向后运行的行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

2 个答案:

答案 0 :(得分:2)

作为一个开箱即用的答案你可能想要考虑这个子:

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

根本不需要任何进度条......

答案 1 :(得分:1)

替换

progress pctCompl

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

无需将i设置为名为pctCompl的变量 - 只需将值传递给过程。