Alan wrote:
Hi all,
I have a list of 1 and 0 values, and I'm using Match to find the first instance of 1 from bottom up.
[....]
The match function is in the second column, and the array that it searches would grow accordingly as the formula moves down. An example of the formula in cell B3 is "=MATCH(1,$A$1:A3,1)".
As you can see from the above example, it kept working until row 13, where it simply stopped looking and grabbed the number right next to it, thinking that's the closest number it can find. I cannot find this issue documented anywhere
The MATCH help page for XL2003, it states:
"If match_type is 1, [....] Lookup_array must be placed in
ascending order".
Obviously, the values in column A ("random" 1s and 0s) are not in ascending order.
When the lookup column is not in ascending order, the results are somewhat unpredictable, owing to the nature of a binary search. Sometimes you get lucky and MATCH returns what you expect. Sometimes MATCH returns an unexpected number because match type 1
"finds the largest value that is less than or equal to lookup_value". Sometimes MATCH returns a #N/A error.
As you may know, MATCH(1,$A$1:A3,0) -- note the zero in the 3rd parameter -- would find the first 1 from
the top.
AFAIK, there is no reliable MATCH formula to find the first 1 from
the bottom. You could write a VBA UDF to do it. The following is bare-bones.
Option Explicit
Function matchRev(lookupVal, lookupTable As Range)
Dim n As Long, i As Long
n = lookupTable.Count
If lookupTable.Rows.Count = 1 Then
For i = n To 1 Step -1
If lookupVal = lookupTable.Cells(1, i) Then matchRev = i: Exit Function
Next
ElseIf lookupTable.Columns.Count = 1 Then
For i = n To 1 Step -1
If lookupVal = lookupTable.Cells(i, 1) Then matchRev = i: Exit Function
Next
End If
matchRev = CVErr(xlErrNA)
End Function