Excel - Using COUNTIF/COUNTIFS across multiple sheets/same column

后端 未结 4 1917
甜味超标
甜味超标 2020-12-03 12:28

I am trying to \"COUNT\" the number of a certain object in column I (in this instance) across multiple sheets. That value in column I is the result of a formula (if it matte

4条回答
  •  死守一世寂寞
    2020-12-03 13:30

    I am trying to avoid using VBA. But if has to be, then it has to be:)

    There is quite simple UDF for you:

    Function myCountIf(rng As Range, criteria) As Long
        Dim ws As Worksheet
    
        For Each ws In ThisWorkbook.Worksheets
            myCountIf = myCountIf + WorksheetFunction.CountIf(ws.Range(rng.Address), criteria)
        Next ws
    End Function
    

    and call it like this: =myCountIf(I:I,A13)


    P.S. if you'd like to exclude some sheets, you can add If statement:

    Function myCountIf(rng As Range, criteria) As Long
        Dim ws As Worksheet
    
        For Each ws In ThisWorkbook.Worksheets
            If ws.name <> "Sheet1" And ws.name <> "Sheet2" Then
                myCountIf = myCountIf + WorksheetFunction.CountIf(ws.Range(rng.Address), criteria)
            End If
        Next ws
    End Function
    

    UPD:

    I have four "reference" sheets that I need to exclude from being scanned/searched. They are currently the last four in the workbook

    Function myCountIf(rng As Range, criteria) As Long
        Dim i As Integer
    
        For i = 1 To ThisWorkbook.Worksheets.Count - 4
            myCountIf = myCountIf + WorksheetFunction.CountIf(ThisWorkbook.Worksheets(i).Range(rng.Address), criteria)
        Next i
    End Function
    

提交回复
热议问题