在vba中添加IF AND THEN语句

时间:2013-10-07 19:17:07

标签: excel vba excel-vba

我需要在我的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$5").Value = "Company 1" Then
  Set Rng = ThisWorkbook.Names("Company1").RefersToRange
 ElseIf Range("$A$5").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$5" > "0" Then
       Wks.Range("$B$5").Value = BrokerCell.Text
       Wks.PrintOut
    End If
  Next BrokerCell

End Sub

宏没有

 AND "$Q$5" > "0"
很明显我在这里做错了。

1 个答案:

答案 0 :(得分:2)

你可能意味着:

If BrokerCell <> "" And Range("$Q$5").Value > 0

您将文字字符串“$ Q $ 5”与文字字符串“0”进行比较。