gpt4 book ai didi

excel - 检查宏是否使用 VBA 运行

转载 作者:行者123 更新时间:2023-12-04 19:48:47 38 4
gpt4 key购买 nike

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

有没有办法确定是否运行了宏?也许在前 2 个宏的末尾放置一个调用,其中公共(public)子将 +1 添加到“计数器”变量?

例如:

宏 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

关于excel - 检查宏是否使用 VBA 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72156146/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com