Search code examples
vbaformsrefreshprogress

VBA refresh form


I have a simple progress bar. I want to fill it when I make iteration through my loop, but I only see my form with 0 and 100 progress value, so it doesn't refresh during the loop. How can I achieve the runtime refreshment? Thank you!

Sub test()
UserForm1.Show (False)
DoEvents

For i = 1 To 700
    For j = 1 To 5000
        UserForm1.Label1.Caption = 100 * i / 700 & "% Completed"
        UserForm1.Bar.Width = 200 * i / 700 '200 - width of the bar
    Next j
Next i

End Sub

Solution

  • You just have the DoEvents in the wrong place. Try it like this.

    Sub test()
    UserForm1.Show vbModeless
    
    For i = 1 To 700
        For j = 1 To 5000
            UserForm1.Label1.Caption = 100 * i / 700 & "% Completed"
            UserForm1.Bar.Width = 200 * i / 700 '200 - width of the bar
            DoEvents
        Next
    Next
    
    End Sub