Need help, Excel Formula, Count number of cells in a range that match the content of a different range

StackOverflow https://stackoverflow.com/questions/23339960

Pregunta

I am pretty good on Excel but this has got me.

I have a range of entries - $D7:$AH7

I want to know how many times these entries appear in this range - $K$7:$V$10

I have tried countif array but I think it is trying to match row with row rather than each entry in my range against the entire lookup range. I also tried transposing the lookup range onto the same line but then I think it tries to match column by column in the same fashion.

If sumproduct works I can't use that is too calculation extensive with this document.

I am happy to use a VBA solution if you have that suggestion.

¿Fue útil?

Solución

Non VBA solution:

=SUMPRODUCT(COUNTIF($K$7:$V$10,$D7:$AH7))

VBA solution:

Founds how many times values from rng1 appears in rng2:

Can it ignore blanks?

UPD:

Function countEntries(rng1 As Range, rng2 As Range) As Long
    Dim arr1, arr2, a1, a2

    arr1 = rng1.Value
    arr2 = rng2.Value

    For Each a1 In arr1
        If Trim(a1) <> "" Then
            For Each a2 In arr2
                If a2 = a1 Then
                    countEntries = countEntries + 1
                    Exit For
                End If
            Next
        End If
    Next
End Function

call it like this: =countEntries($D7:$AH7,$K$7:$V$10).


P.S. I use loop for determining whether array contains value or not rather than Application.Match/Application.CountIf because it's up to 10 times faster. See this link for details: Matching values in string array

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top