在 vba 中添加 IF AND THEN 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19232769/
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
Adding an IF AND THEN Statement in vba
提问by user2162384
I need to create an IF AND THEN statement in my vba. I've tried this code below and although I get no errors it doesn't work (ie: nothing happens when I run the macro):
我需要在我的 vba 中创建一个 IF AND THEN 语句。我已经尝试了下面的这段代码,虽然我没有得到任何错误,但它不起作用(即:运行宏时没有任何反应):
Sub PrintAll()
Dim BrokerCell As Range
Dim Rng As Range
Dim Wks As Worksheet
Set Wks = Worksheets("PRINT PAGE")
If Range("$A").Value = "Company 1" Then
Set Rng = ThisWorkbook.Names("Company1").RefersToRange
ElseIf Range("$A").Value = "Company2" Then
Set Rng = ThisWorkbook.Names("Company2").RefersToRange
Else: Set Rng = ThisWorkbook.Names("Company3").RefersToRange
End If
For Each BrokerCell In Rng
If BrokerCell <> "" AND "$Q" > "0" Then
Wks.Range("$B").Value = BrokerCell.Text
Wks.PrintOut
End If
Next BrokerCell
End Sub
The macro works without the
宏在没有
AND "$Q" > "0"
so clearly I'm doing something wrong here.
很明显我在这里做错了。
回答by Alex Godofsky
You probably mean:
你可能的意思是:
If BrokerCell <> "" And Range("$Q").Value > 0
You're comparing the literal string "$Q$5" to the literal string "0".
您将文字字符串“$Q$5”与文字字符串“0”进行比较。