2017-03-09 137 views
2

我得到一個「無法獲取WorksheetFunction類的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 
+1

#FunFacts:'WorksheetFunction.CountIf'不是一個屬性 - 該錯誤信息是混亂的! –

回答

1

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

工作 - 非常感謝! – aoswald

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