“Unable to get the Countif property of the Worksheetfunction class” error

后端 未结 2 903
萌比男神i
萌比男神i 2021-01-21 14:07

I get a \"Unable to get the Countif property of the Worksheetfunction class\" error when using this code

    Windows(\"usertemp.xls\").Activate
Selection.AutoFil         


        
相关标签:
2条回答
  • 2021-01-21 14:10

    The SpecialCells(xlCellTypeVisible) will create a disjointed range and Countif does not play nice with disjointed ranges.

    You will need to use a loop to loop through each criteria and add the Countifs together.

    Try this:

    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
    
    0 讨论(0)
  • 2021-01-21 14:29

    CounIf does not accept a multi-area range. You need to loop over the 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
    
    0 讨论(0)
提交回复
热议问题