满足条件时运行 VBA 宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2400621/
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
Run VBA macro when condition is met
提问by Pieter
I'm creating a spreadsheet to train my numerical skills.
我正在创建一个电子表格来训练我的数字技能。
Now, I use VBA macros to generate a new problem once the current one has been correctly solved. To do so, I still have to press a button in the worksheet, which costs time and is annoying.
现在,一旦正确解决了当前问题,我将使用 VBA 宏生成一个新问题。为此,我仍然必须按工作表中的一个按钮,这既费时又烦人。
Is there a way that I can execute a macro when a certain condition is met?
有没有办法在满足特定条件时执行宏?
for instance:
例如:
if A1 = "correct!"
then <run macro>
else <do nothing>
Also, let me know if you're interested in the spreadsheet, I'd be happy to share it with you guys.
另外,如果您对电子表格感兴趣,请告诉我,我很乐意与你们分享。
best, Pieter
最好的,彼得
回答by Peter Lang
Add this as code for your Worksheet:
将此添加为工作表的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If (Range("A1") = "correct!") Then
''# do your stuff here
End If
End Sub
Worksheet_Changeis called whenever something is changed. As it looks like A1is calculated, you can not check for Targetin that case but check for the value of the cell.
Worksheet_Change每当发生变化时都会调用。看起来A1是计算出来的,Target在这种情况下你不能检查,但检查单元格的值。
回答by Vikram Halder
=IF(A1="Vikram" ,9,8)
=IF(A1="维克拉姆",9,8)
You will write =IF(A1="Vikram" ,9,8). In plain English it means if the value of cell A1 is Vikram then the value in which this formulas resides is 9 otherwise it is 8
你会写 =IF(A1="Vikram" ,9,8)。用简单的英语来说,这意味着如果单元格 A1 的值是 Vikram 那么这个公式所在的值是 9 否则它是 8

