1

I am trying to use the =MATCH("zzzz";$A2:2) function to find the column containing the last non-empty cell in row 2 of my worksheet (containing text values).

The value returned (4) implies that the cell D2 is non-empty, even though it appears to be so, and the EQ function says its content is equal to that of the truly empty neighboring cell C2.

If I copy C2 onto D2, or if I select D2 and choose "delete value" from the menu, the problem is fixed. The correct behavior is displayed in rows 5 and 6.

Actually the problem originally occurs in a much larger worksheet, and I cannot afford to fix all "pseudo-empty" cells manually. One first step would be to figure out what is the hidden content of cell D2, which is considered empty by the EQ function but not by the MATCH function.

Is there a simple way to do that?

1 Answer 1

0

The formula is missing the search_type parameter, which makes the match() function use its default and assume that the data is sorted. This means that it uses a binary search, which will not work with unsorted data such as the data in the sample spreadsheet. This explains the unexpected result.

To make it work, add a search_type parameter of 0 and sort the data in reverse order by column number, or preferably apply this pattern:

=xmatch("?*"; arrayformula(to_text(A2:2)); 2; -1)

The formula must go to a row other than row 2.

See match() and xmatch().

3
  • I applied the XMATCH suggestion to my large spreadsheet and it works great. On the other hand, I am not sure that MATCH with the FALSE option would do the job since it would require an exact match, which will not necessarily be achieved. And finally, I am still puzzled that the result can depend on the "content" of a nominally empty cell. Anyway, thanks a lot for your help. I still have to understand properly why your solution works ! Working on it…
    – jmichel
    Commented Jan 28 at 20:30
  • match() supports wildcards. For example, to find the first non-blank cell, you can use =match("*", A2:2, 0). To search from the end, you can sort the data in reverse order by using column() as the sort key. To do the same with less steps, use xmatch(). Commented Jan 28 at 20:35
  • I did not know about XMATCH. It's very convenient. Thanks.
    – jmichel
    Commented Jan 28 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.