VBA поиск по дополнительному столбцу - PullRequest
1 голос
/ 06 ноября 2019

Мне нужно добавить PW к приведенному ниже коду: RDG1, PW, Sewer Works, Pass. Дополнительным столбцом будет столбец D, ура.

With ThisWorkbook.Sheets(1)
    .Range("D5").Value = Counts("RDG1", "Sewer Works")
    .Range("E5").Value = Counts("RDG1", "Sewer Works", "PASS")
    .Range("F5").Value = Counts("RDG1", "Sewer Works", "FAIL")

    'other counts here

End With



'Get row counts from sheet2
'  Optional 3rd parameter for PASS/FAIL
Function Counts(v1 As String, v2 As String, Optional v3 As String = "") As Long
    Dim rng1 As Range, rng2 As Range, rng3 As Range
    With ThisWorkbook.Sheets(2)
        Set rng1 = .Range("C7:C10000")
        Set rng2 = .Range("G7:G10000")
        Set rng3 = .Range("I7:I10000")
        If Len(v3) > 0 Then
            Counts = Application.CountIfs(rng1, v1, rng2, v2, rng3, v3)
        Else
            Counts = Application.CountIfs(rng3, v1, rng2, v2)
        End If
    End With
End Function

1 Ответ

0 голосов
/ 06 ноября 2019

Не проверено - вы должны получить общий шаблон ...

With ThisWorkbook.Sheets(1)
    .Range("D5").Value = Counts("RDG1", "PW", "Sewer Works")
    .Range("E5").Value = Counts("RDG1", "PW", "Sewer Works", "PASS")
    .Range("F5").Value = Counts("RDG1", "PW", "Sewer Works", "FAIL")

    'other counts here

End With



'Get row counts from sheet2
'  Optional 4rd parameter for PASS/FAIL
Function Counts(v1 As String, v2 As String, v3 As String, Optional v4 As String = "") As Long
    Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range
    With ThisWorkbook.Sheets(2)
        Set rng1 = .Range("C7:C10000")
        Set rng2 = .Range("D7:D10000")
        Set rng3 = .Range("G7:G10000")
        Set rng4 = .Range("I7:I10000")
        If Len(v4) > 0 Then
            Counts = Application.CountIfs(rng1, v1, rng2, v2, rng3, v3, rng4, v4)
        Else
            Counts = Application.CountIfs(rng1, v1, rng2, v2, rng3, v3)
        End If
    End With
End Function
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...