检查宏是否为 运行 和 VBA

Check if a macro was run with VBA

我有一个带有 3 个分步按钮的交互式工作簿,因此用户可以生成报告。我想让第二个宏检查第一个宏是否是 运行,如果不是,则用 Masgbox 警告用户先 运行 它。

有没有办法确定宏是否为 运行?也许在前 2 个宏的末尾放置一个 Call,其中 public sub 将 +1 添加到 'counter' 变量?

例如:

宏 2 检查计数器是否为 1, 宏 3 检查 counter = 2

提前致谢。

运行 宏顺序

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