加入VBA CountA / countif

时间:2014-11-20 18:53:08

标签: excel vba function loops excel-vba

嗨,大家只是快速提问。我想在VBA excel中做一个 Counta / Countif公式。我正在使用的代码如下。

Sub To_open
  Dim x as Range 
  x  =COUNTA('Tier 2'!C2:C1000)+COUNTA('Tier 3'!C2:C1000)+COUNTA('Tier 4'!C2:C1000)+COUNTA('Tier 5'!C2:C1000)

  If x = 0 then 
    msgbox " No impact "
  End If   
End sub

代码假设计算并添加coloumn C中的行,其值为4张,如果等于零,则显示消息框。

2 个答案:

答案 0 :(得分:2)

一种方法是使用评估功能:

Sub To_open()
  Dim n             As Long

  n = Evaluate("COUNTA('Tier 2'!C2:C1000) + " & _
               "COUNTA('Tier 3'!C2:C1000) + " & _
               "COUNTA('Tier 4'!C2:C1000) + " & _
               "COUNTA('Tier 5'!C2:C1000)")
  MsgBox n
End Sub

答案 1 :(得分:1)

尝试

 x =Application.WorksheetFunction.COUNTA('Tier 2'!C2:C1000)+COUNTA('Tier 3'!C2:C1000)+COUNTA('Tier 4'!C2:C1000)+COUNTA('Tier 5'!C2:C1000))