【问题标题】:"Unable to get the Countif property of the Worksheetfunction class" error“无法获取 Worksheetfunction 类的 Countif 属性”错误
【发布时间】:2017-03-10 03:26:56
【问题描述】:

使用此代码时出现“无法获取 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

【问题讨论】:

  • #FunFacts: WorksheetFunction.CountIf 不是属性 - 该错误消息是混乱的!

标签: excel vba


【解决方案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

【讨论】:

    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2013-10-17
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多