Home » excel » excel – Cell for cell match instead of value for value match

excel – Cell for cell match instead of value for value match

Posted by: admin May 14, 2020 Leave a comment


Optimally, I would have it done with an excel formula, but if it isn’t possible, I’d also accept a User Defined Function as solution.

Consider this screenshot:

enter image description here

Though the match formulas =IF(ISERROR(MATCH(A2,$B$2:$B$12,0)),"No Match", "Match") in the Column C seem to be working perfectly, they have one caveat for me: They display “Match” even if there are two 4 in the Lookup Cells Column for only one 4 in the Lookup Range Column.

Is there, in excel, a function or a combination of them that would do a cell for cell match instead of a value for value match?

For instance, in the example above, the 4 in the 5th row should not display “Match”. Rather, it should display “No Match”.

Now, as I’ve stated at the beginning of the question, if it isn’t possible with excel functions, I’ll instead make do with UDFS.

The algorithm, that I’ve not yet finished writing in the form of vba code, is one that has 3 arguments: the lookup cell, the lookup cells and the lookup range. The function output is “Match” or “No Match”.

Basically, if I take the example above, the lookup cell could be A2, the lookup cells, $A$2:$A$5 and the lookup range, $B$2:$B$12.

With the lookup cells and the lookup range, I create two arrays, one for each.

Afterwards, compare their values in a loop. If one of their values are the same, I add their value to another array along with their relative row within the lookup range column and their absolute row number within the Lookup Cells column. This array is dynamic and has two dimensions.

Then, I would (stuck at this part) have another loop comparing the values from the lookup cells to the values within the array. If they are equal, then I’d like for the Lookup Range loop to start its loop after the relative row of this value stored in the array (+1).

Finally, after looping through it all and finding which cells do match (values in the array), if one of the absolute row numbers in the array coincide with the absolute row number of the lookup cell (1st argument of the function), then the function returns “Match”. If not, it returns “No Match”.

My thanks and my unfinished code below:

Function Rlookup(ByVal LookupCell As Range, ByVal LookupCells, ByVal LookupRange)

Dim LookupRngArray As Variant
Dim LookupCellsArray As Variant
Dim i As Long
Dim z As Long
Dim x As Long
Dim w As Long
Dim y As Long
Dim Arr() As Variant

x = 0

LookupRngArray = LookupRange.Value2
LookupCellsArray = LookupCells.Value2

For i = 1 To UBound(LookupCellsArray)

    For z = 1 To UBound(LookupRngArray)

        If LookupCellsArray(i, 1) = LookupRngArray(z, 1) Then

            For y = x To 0 Step -1

                If LookupCells(i, 1) = Arr(1, y + 1) Then
                    z > Arr(1, y + 1)


                    x = x + 1
                    ReDim Preserve Arr(1 To 3, 1 To x)
                    Arr(1, x) = LookupCellsArray(i, 1)
                    Arr(2, x) = Application.WorksheetFunction.Match(LookupCellsArray(i, 1), LookupRngArray, 0)
                    Arr(3, x) = LookupCells.Row + i - 1

                End If

            Next y

        End If

    Next z

Next i

For w = 1 To x

If LookupCell.Row = Arr(3, w) Then
Rlookup = "Match"

End If

If Rlookup = "0" Then Rlookup = "No Match"
End Function
How to&Answers:

Can be done with COUNTIF():

=IF(A2<>"",IF(COUNTIF($A$2:A2,A2)<=COUNTIF(B:B,A2),"match","No Match"),"")

enter image description here