Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

progressbar without loop

Tags:

excel

vba

I think I've searched the whole internet for a simple progressbar, but I can't find one that makes progress after a function/sub is finished.

I will demonstrate in the code below what I want. My knowledge in VBA is to basic too make something like this.

I have a form, with a button, when I click on the button he should execute this:

Sub program()

progressbarform.show

Call function1
"set progressbar to 20%"

Call funcion2
"set progressbar to 40%"

Call function3
"set progressbar to 100%"

"Unload progressbar form"

End Sub
like image 386
user2156096 Avatar asked Dec 01 '25 08:12

user2156096


1 Answers

You can use delegation to make this work. Your progress bar is in a separate form to your execution of function 1, 2 and 3 so you should try and execute within the progress bar form. After you call form.Show the form has focus and function 1,2 and 3 won't run until the form closes.

In your progress bar form (assuming it has a label and a progress bar control) place a function that will update your progress bar:

Public Sub UpdateProgress(intProgress As Integer, Optional strMessage As String)

    If Not IsMissing(strMessage) Then
        lbl_Progress.Caption = strMessage
    End If
    pb_Progress.Value = intProgress
    Call Me.Repaint

End Sub

Then you can execute your functions from within your progressbar form. You can do this by setting the form that called it as an object to expose your functions or just store function 1,2 and 3 in your progress bar form.

Sub RunFunctions()

    UpdateProgress 0, "Starting functions"    

    UpdateProgress 10, "Begin function1"    
    Call function1
    UpdateProgress 30, "Finished function1"  

    UpdateProgress 50, "Begin function2"    
    Call function2
    UpdateProgress 70, "Finished function2"  

    UpdateProgress 90, "Begin function3"    
    Call function3
    UpdateProgress 100, "Finished function3"

    UpdateProgress 100, "Finished All Functions"

End Sub

Ask me any questions if you need help setting this up.

like image 197
Tom 'Blue' Piddock Avatar answered Dec 03 '25 04:12

Tom 'Blue' Piddock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!