D3merzel
D3merzel

Reputation: 63

Calculating running time for several macros

I'm currently using the following code to determine the time which it takes for a certain macro to run.

    Sub Timer ()

        Dim StartTime As Double
        Dim SecondsElapsed As Double

        StartTime = Timer


        'Some macro executed


        SecondsElapsed = Round(Timer - StartTime, 2)

        MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation

    End Sub

Now, I want to use this timer for several macros which are executed in a row using a button. I'm looking for something like:

    <Button>

        'Dim StartTime As Double
        'Dim SecondsElapsed As Double

        StartTime = Timer


        Macro1
        Macro2
        Macro3


        SecondsElapsed = Round(Timer - StartTime, 2)

        MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation

    <Button>

Upvotes: 0

Views: 349

Answers (1)

Earl_of_Duke
Earl_of_Duke

Reputation: 358

You can just call your macros from within your main timer code

Sub Timer ()

    Dim StartTime As Double
    Dim SecondsElapsed As Double

    StartTime = Timer


    call Macro1()
    call Macro2()
    call Macro3()


    SecondsElapsed = Round(Timer - StartTime, 2)

    MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation

End Sub

If you wanted to know how long each one took individually just declare a SecondsElapsed variable for each Macro and populate it after each Call statement.

Upvotes: 2

Related Questions