vba 我可以在 Excel 宏中使用 if else 语句来根据单元格值决定运行哪个宏吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13865837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:36:29  来源:igfitidea点击:

Can I use if else statement in Excel macro to decide which macro to run based on a cell value?

excelvbaif-statement

提问by Oliver Ctheitroado

If Cell "DB2" equals "Hot" then run Hot_Macro If Cell "DB2" equals "Cold" then run Cold_Macro If Cell "DB2" equals "Warm" then run Warm_Macro If Cell "DB2" is blank end

如果单元格“DB2”等于“Hot”则运行 Hot_Macro 如果单元格“DB2”等于“Cold”则运行 Cold_Macro 如果单元格“DB2”等于“Warm”则运行 Warm_Macro 如果单元格“DB2”是空白端

回答by chuff

You call use a Call statement to run one macro from another. A minimalist example:

您调用使用 Call 语句从另一个运行一个宏。一个极简的例子:

   Sub main_macro()
       If Range("A1").Value = "hot" Then
           Call hot_macro
       ElseIf Range("A1").Value = "cold" Then
           Call cold_macro
       Else
           Return
       End If
   End Sub

   Sub hot_macro()
       Range("A2").Value = "It's hot!"
   End Sub

   Sub cold_macro()
       Range("A2").Value = "It's cold!"
   End Sub