简体   繁体   中英

Excel array count frequencies with multiple conditions

example data is shown in the link

https://docs.google.com/spreadsheets/d/1HXhT76CwDLs7XVPoU8uqx4RVrKOb6OOcox65l3i7LRY/edit#gid=0

desired outcome is "vcount" I want to use sumproduct function to count frequences , if the conditions is satisfied:

① vyr>pyr

② both v1 and v2 appears p1,p2,p3 columns(order does not matter)

=SUMPRODUCT(--($G2:$I5=A2),--($G2:$I5=B2),--(C2>$J2:$J5)) then Ctrl + Shift + Enter

in fact, the real data contain about p1 to p20 columns , so i use CSE But doesn't seem to work.

As stated I think VBA is the only solution. Put this in a module attached to the workbook.

Function vcount(v1 As Range, v2 As Range, vyr As Range, pRng As Range, pyr As Range) As Long

    vcount = -1

    Dim pArr As Variant
    pArr = pRng.Value

    Dim pyrArr As Variant
    pyrArr = pyr.Value

    If UBound(pArr, 1) <> UBound(pyrArr, 1) Then Exit Function

    Dim temp As Long
    temp = 0

    Dim i As Long
    For i = LBound(pArr, 1) To UBound(pArr, 1)
        Dim v1B As Boolean
        v1B = False

        Dim v2B As Boolean
        v2B = False

        Dim j As Long
        For j = LBound(pArr, 2) To UBound(pArr, 2)
            If pArr(i, j) = v1 Then v1B = True
            If pArr(i, j) = v2 Then v2B = True
        Next j
        If v1B And v2B And pyrArr(i, 1) < vyr Then temp = temp + 1
    Next i

    vcount = temp
End Function

Then you would call it like a normal formula:

=vcount(A2,B2,C2,$G$2:$I$5,$J$2:$J$5)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM