使用 VBA 如何将 CountIfs 与变量一起使用

With VBA how to use CountIfs with a variable

我在任何地方都找不到将 CountIfs 与变量一起使用的示例。为什么会出现 "Object Requried" 错误?

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

根据评论,我推荐如下:

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