"无法获取Worksheetfunction类的Countif属性"错误

时间:2017-03-09 18:54:50

标签: excel vba

我得到一个"无法获得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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

SpecialCells(xlCellTypeVisible)会创建一个脱节的范围,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