Search code examples
excelvbauser-experience

Check if a macro was run with VBA


I have a interactive workbook with 3 step-by-step buttons so the user can generate a report. I want 2nd macro to check if 1st macro was run and if not, warn the user with a Masgbox to run it first.

Is there a way to determine if a macro was run? Maybe putting a Call at the end of the first 2 macros where a public sub add +1 to a 'counter' variable?

For example:

Macro 2 check if counter = 1, Macro 3 check if counter = 2

Thanks in advance.


Solution

  • Run Macros Sequentially

    Public RunChecker As Long
    
    Sub Macro1()
        Select Case RunChecker
        Case 1
            MsgBox "You already ran Macro1. To continue, run Macro2.", vbExclamation
            Exit Sub
        Case 2
            MsgBox "To continue, run Macro3.", vbCritical
            Exit Sub
        End Select
        
        ' Your code, e.g.:
        MsgBox "Running1", vbInformation
        
        RunChecker = 1
    End Sub
    
    Sub Macro2()
        Select Case RunChecker
        Case 0
            MsgBox "You need to run Macro1 first.", vbCritical
            Exit Sub
        Case 2
            MsgBox "You already ran Macro2. To continue, run Macro3.", vbExclamation
            Exit Sub
        End Select
        
        ' Your code, e.g.:
        MsgBox "Running2", vbInformation
        
        RunChecker = 2
    End Sub
    
    Sub Macro3()
        Select Case RunChecker
        Case 0
            MsgBox "You already finished. To start again, run Macro1.", vbExclamation
            Exit Sub
        Case 1
            MsgBox "You need to run Macro2 first.", vbCritical
            Exit Sub
        End Select
        
        ' Your code, e.g.:
        MsgBox "Running3", vbInformation
        
        RunChecker = 0
    End Sub