2016-10-06 9 views
0

Я не могу найти пример использования CountIfs с переменной. Почему это дает ошибку «Object Requried»?С помощью VBA как использовать CountIfs с переменной

Dim recTable As ListObject 
Dim EOM As Date 
Dim Pending As Double 

For x = 1 To RecordCount 
    If Not IsNull(recTable.DataBodyRange(x, 7).Value) Then 
        Pending = Pending + WorksheetFunction.CountIfs(recTable.DataBodyRange(x, 2).Value, "<=" & EOM, recTable.DataBodyRange(x, 7).Value, ">" & EOM) 
       ElseIf IsNull(recTable.DataBodyRange(x, 7).Value) And Not IsNull(recTable.DataBodyRange(x, 6).Value) Then 
        Pending = Pending + Application.WorksheetFunction.CountIfs(recTable.DataBodyRange(x, 2).Value, "" <= "" & EOM, recTable.DataBodyRange(x, 6).Value, "" > "" & EOM) 
       Else 
        Pending = Pending + 1 
      End If 
     Debug.Print Pending 
Next x 
+0

Вам не нужны ваши две двойные кавычки должны быть одиночными двойные кавычки '«»<=«»' должен быть '" <= "' –

+0

Итак, все изменилось, но я все равно получаю сообщение об ошибке «Object Required». – Hawsidog

+0

Вы также изменили '" ">" "' на '"> "'? – YowE3K

ответ

1

На основе замечаний, я рекомендую следующее:

Dim recTable As ListObject 
Dim EOM As Date 
Dim Pending As Double ' Maybe Long or Integer? 

'recTable is not set in the posted code, but I assume it is in the actual code 
'EOM is not set in the posted code, but I assume it is in the actual code 
'RecordCount is not declared or set in the posted code, but I assume it is in the actual code 
'x is not declared in the posted code, but I assume it is in the actual code 

With recTable 
    For x = 1 To RecordCount 
     If Not IsNull(.DataBodyRange(x, 7).Value) Then 
      If .DataBodyRange(x, 2).Value <= EOM And _ 
       .DataBodyRange(x, 7).Value > EOM Then 
       Pending = Pending + 1 
      End If 
     ElseIf Not IsNull(.DataBodyRange(x, 6).Value) Then 
      If .DataBodyRange(x, 2).Value <= EOM And _ 
       .DataBodyRange(x, 6).Value > EOM The 
       Pending = Pending + 1 
      End If 
     Else 
      Pending = Pending + 1 
     End If 
     Debug.Print Pending 
    Next x 
End With