0

I want to get last non empty cell from every row for further operations. Searching Net I came across a LOOKUP formula which can do this job fine. I have alpha as well as numeric data in the rows. Sample Data is shown below.

ID1 ID2 ID3 ID4 ID5     ID6 ID7 ID8 ID9 ID10    ID11
a1  a2  a3  a4  a5      23  24  25      25  

23  NN  67  99  200001  ss  p1  23  rq          3

I am using the following formula in L4 which gives correct results that is 3 in L4. Similar formula for row 2 gives result of 25 in L2. To understand the formula I carried out Formula Evaluation but I am not getting the logic of last step as how it translates to final result.

=LOOKUP(2,1/(A4:K4<>""),A4:K4)

LOOKUP(2,{1,1,1,1,1,1,1,1,1,#DIV/0!,1},A4:K4) After this step result that is the last non empty value of Row 4 comes i.e. 3 Can someone explain how this line is finally translating into the result step by step.

Snapshot

2 Answers 2

3

Basically taking the below formula as an example

=LOOKUP(2,1/(A1:D1<>""),A1:D1)

With data in A1:D1

A1  B1  C1  D1
1    2   3  

The formula will populate as follows

=LOOKUP(2,{1,1,1,#DIV/0!},{1,2,3,0})

The way this works is quite simple.

1/(A1:D1<>"") creates an array of 1's and #DIV\0!

            1  where the cell is not blank
            #DIV/0  where the cell is blank

Then the lookup formula looks for a 2 in this array , which it cant find, but it will always return the position of then last NON ERROR value in the context of an array filled with 1's and #DIV/0!, which would be the 3rd position in the array, The LOOKUP formula then returns the 3rd position of the last argument array in the formula A1:D1 , which is 3

8
  • "but it will always return the last NON ERROR value": No, not as it is documented. Please provide a link where this rule is documented. Jun 25, 2016 at 10:58
  • in the context of an array filled with 1's and #DIV/0! Jun 25, 2016 at 10:59
  • Lets agree its has badly written documentation . "If the LOOKUP function can't find the lookup_value, the function matches the largest value in lookup_vector that is less than or equal to lookup_value." but if multiple identical values are in the array it returns the position of the last one. Jun 25, 2016 at 11:10
  • That is not the point. The point is that it seems that {1,1,#DIV/0!,1,#DIV/0!,1,#DIV/0!,#DIV/0!} is accepted as an array in ascending order in which the last 1 is the largest value that is less than or equal to 2. That is documented nowhere. But this approach is used sucessfully so often, that we can be very sure, that it is exactly so. Jun 25, 2016 at 11:18
  • my point is that its not finding the largest value in the array, its finding the last in the array when it cant find a match, =LOOKUP(4,{3,2,1,0},{3,2,1,0}) hence why this returns 0 , its easy to say its needs to be sorted, but sorting just puts the largest to the end, when in fact its really just finding the last one anyway! Jun 25, 2016 at 11:23
2

This approach finding the last filled cell uses the vector form of LOOKUP. In

=LOOKUP(2,{1,1,1,1,1,1,1,1,1,#DIV/0!,1},A4:K4) the 2 is the lookup_value, the array {1,1,1,1,1,1,1,1,1,#DIV/0!,1} is the lookup_vector and the A4:K4 is the result_vector.

The vector form of LOOKUP looks in a lookup_vector for the lookup_value and returns the value from the same position in the result_vector.

If the LOOKUP function can't find the lookup_value directly in the lookup_vector, the function matches the largest value in lookup_vector that is less than or equal to lookup_value. If multiple equal values are in the array, which are the largest which are less than or equal to lookup_value, the position of the last is taken. In this case it matches the last 1 since this is the largest value in lookup_vector that is less than or equal to 2.

But for this to do, there is the rule, that the values in lookup_vector must be placed in ascending order. And this is the not documented feature with this approach. It seams as if the #DIV/0! errors within lookup_vector does not contradict that rule. But since the source is not open, we can't be absolutely sure about this. But this approach is used sucessfully so often, that we can be very sure.

3
  • Sorry for delay in responding, I was away from my computer due to some unavoidable emergent situation. I convey my thanks for helping me in understanding the logic. I need some clarity in the third para which starts from If the LOOKUP function....less than or equal to 2. When you say largest value does it mean largest positional value, since these are all 1's and #DIV/0! If I change data to be all 1's except last three cells 0 then it returns 0 but if change last one to say 4 it returns 4. . How it co-relates to result vector in this situation is still not very clear.
    – skkakkar
    Jun 25, 2016 at 11:53
  • LOOKUP looks in the lookup_vector for the lookup_value, 2 in the example, and returns the value from the same position in the result_vector. If multiple equal values are in the lookup_vector, which are the largest which are less than or equal to lookup_value, the position of the last is taken. But the lookup_vector must be in ascending order. If you contradict that rule, then LOOKUP will not work correctly. The result_vector needs not to be sorted. Jun 25, 2016 at 12:11
  • Heartfelt thanks for a lucid step by step explanation on formula logic. Now my concepts are fully clear in this matter.
    – skkakkar
    Jun 25, 2016 at 12:21

Your Answer

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

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