简体   繁体   English

Excel VBA反向进度栏

[英]Excel VBA reverse progress bar

I am using a progress bar to show the user the status of a running macro, however because the macro deletes rows it is running backwards For i = lastrow To 2 Step -1 , meaning that my progress bar is running from 100% down to 2%. 我正在使用进度条向用户显示正在运行的宏的状态,但是,因为宏删除了正在向后运行的行,所以For i = lastrow To 2 Step -1 ,这意味着我的进度条从100%降为2 %。

I have only ever counted up with i , is it possible to make the progress read the info backwards when counting down, so to the user it visibly is counting up? 我只算过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

as an out of the box answer you may want to consider this sub: 作为开箱即用的答案,您可能需要考虑以下内容:

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

which shouldn't require any progress bar at all... 根本不需要任何进度条...

Replace 更换

progress pctCompl

with

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

There's no need to set i to a variable called pctCompl - just pass the value to the procedure. 无需将i设置为名为pctCompl的变量-只需将值传递给过程即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM