Countifs 属性 运行-字符串条件的时间错误

Countifs Property Run-time error with String Criteria

我没有表达正确worksheetfunction.countifs

我得到:

Run-time error '1004':
Unable to get the CountIfs property of the WorksheetFunction Class

Sub test()
Dim Category(7 To 10) As Variant
Dim Ar As Variant
Dim Da As Range
Dim Cat As Range
Dim Br As Variant

Set Da = Range("A3:A" & Range("A" & Rows.Count).End(xlUp).Row)
Set Cat = Range("C3:C" & Range("C" & Rows.Count).End(xlUp).Row)

Ar = Range("F2").Value2
Br = Application.WorksheetFunction.EoMonth(Range("F2"), 0)

For i = 7 To 10
    Category(i) = Worksheets("Log").Cells(1, i).Value
    Cells(2, i) = Application.WorksheetFunction.CountIfs(Da, ">=" & Ar, Da, "<=" & Br, Cat, Category(i))
Next i

End Sub

问题似乎出在 sumifs 函数的最后一个范围和条件上(Cat,Category(i),其中 Category(i) 等于一个字符串)。

如果我拆分 sumif 并在 SUMIF 中单独使用 Cat,Category,它工作正常,另外两个标准单独工作也很好。

所以这实际上是由于字符串标准的声明方式,改变它修复了它。

Cells(2, i) = Application.WorksheetFunction.CountIfs(Range("C3", Range("C3").End(xlDown)), "=" & test & "*", Range("A3", Range("A3").End(xlDown)), ">=" & Ar, Range("A3", Range("A3").End(xlDown)), "<=" & Br)