“无法获取Worksheet函数类的Countif属性”错误

使用此代码时出现“无法获取Worksheetfunction类的Countif属性”错误

Windows("usertemp.xls").Activate Selection.AutoFilter ActiveSheet.Range("$A$1:$AO$18695").AutoFilter Field:=14, Criteria1:=Array( _ "28", "BE", "CH", "DE", "FR", "JP", "NL"), Operator:=xlFilterValues Dim Impats As Integer Impats = Application.WorksheetFunction.CountIf(Range("AL:AL").SpecialCells(xlCellTypeVisible), "I") MsgBox Impats 

CounIf不接受多区域范围。 你需要遍历Areas

 Dim impats As Long, r As Range For Each r In Range("AL:AL").SpecialCells(xlCellTypeVisible).Areas impats = impats + WorksheetFunction.CountIf(r, "I") Next 

SpecialCells(xlCellTypeVisible)会创build一个不连贯的范围,而Countif不会和不连贯的范围玩。

您将需要使用循环来循环每个标准并将Countifs在一起。

尝试这个:

 Dim arr() as variant Dim arrPart as variant arr = Array("28", "BE", "CH", "DE", "FR", "JP", "NL") Dim Impats As Integer For Each arrPart in arr Impats = Impats + Application.WorksheetFunction.CountIfs(ActiveSheet.Range("AL:AL"), "I",ActiveSheet.Range("N:N"),arrPart) Next arrPart MsgBox Impats