A Cell Function to return the last value in a column, including Blanks

  • Thread starter Thread starter Mark Worthington
  • Start date Start date
M

Mark Worthington

I picked up the following from the Excel forum (ref Jason Morin) that
caters for data with blanks :

=ROW(INDEX(Data,MAX(IF(LEN(Data)>0,ROW(Data)))))

However, this does not work with Data of the form C:C, ie, C1:C65536
but does if Data is defined as C1:C65535,and sometime it is more
convenient to use the whole column. It gives the #NUM! error.

Any help would be much appreciated!

Mark
 
To fetch the last numerical value...

=LOOKUP(9.99999999999999E+307,C:C)

To determine the position of the last numerical value within column C...

=MATCH(9.99999999999999E+307,C:C)

To fetch the last text value...

=LOOKUP(REPT("z",255),C:C)

To determine the position of the last text value within column C...

=MATCH(REPT("z",255),C:C)
 
There is no way of doing that and covering all bases, Aladin showed you how
to do it
for either text and blanks or numbers and blanks but for a function that
will work for text, numbers and blanks
there is no such beast. I also fail to see any advantage using all 65536
rows



--

Regards,

Peo Sjoblom

(No private emails please, for everyone's
benefit keep the discussion in the newsgroup/forum)
 
Aladin,

Thanks for the input, very clever, forcing a failed lookup to return
the last value…

I have spent some time looking at this, and while I know the formulae
you gave were specific to the question, the following may be helpful
to others like me who browse these Q&A :

These give the #N/A error if no number or text exist, depending on the
formula used. Also, the position will be incorrect if the list does
not start on Row 1 (for example, a selection other than C:C, say
C4:C100).

The Index/Match gives a reference (unlike Lookup) so can use Row(….)
etc to find the location/address. Therefore, an alternative method to
find the last numerical or text value, the following can be used :

=IF(COUNT(Data)=0,INDEX(Data,MATCH(REPT("z",255),Data)),
IF(COUNTA(Data)-COUNT(Data)=0,INDEX(Data,MATCH(9.99999999999999E+307,Data)),
INDEX(Data,MAX(MATCH(REPT("z",255),Data),MATCH(9.99999999999999E+307,Data)))))

With regard to Jason Morin's formula, it only works if Data starts in
Row 1. To enable this to work with any selection (except an entire
column), use the following array formulae :

=INDEX(Data,MAX(IF(LEN(Data)>0,ROW(Data)-MIN(ROW(Data)-1))))

To find the position (row number) for instance, use :

=ROW(INDEX(Data,MAX(IF(LEN(Data)>0,ROW(Data)-MIN(ROW(Data)-1)))))

To find the address, use :

=ADDRESS(ROW(INDEX(Data,MAX(IF(LEN(Data)>0,ROW(Data)-MIN(ROW(Data)-1))))),COLUMN(Data))


Peo,

I am surprised at your last comment. While I appreciate that using all
rows is hardly likely, it is much easier to select a single column
(knowing all data will be accounted for) than to worry about defining
a particular range.
 
Mark Worthington said:
Aladin,

Thanks for the input, very clever, forcing a failed lookup to return
the last value.

I have spent some time looking at this, and while I know the formulae
you gave were specific to the question, the following may be helpful
to others like me who browse these Q&A :

No, it's not. The questions these formulas tackle have the following form:

[1]

Last numerical value in a given ref, where ref can be definite like C4:C100
or indefinite like A:A...

The formula is:

=LOOKUP(9.99999999999999E+307,Ref)

Position of the last numerical value within a given ref, where ref can be
definite like C4:C100 or indefinite like A:A...

The formula is:

=MATCH(9.99999999999999E+307,Ref)

[2]

Last text value in a given ref, where ref can be definite like C4:C100 or
indefinite like A:A...

The formula is:

=LOOKUP(REPT("z",255),Ref)

Position of the last text value within a given ref, where ref can be
definite like C4:C100 or indefinite like A:A...

The formula is:

=MATCH(REPT("z",255),Ref)
These give the #N/A error if no number or text exist, depending on the
formula used.

Exactly what we should like to have... A diagnostic value that tells us:
There is no text/numerical value in the ref of interest.
Also, the position will be incorrect if the list does
not start on Row 1 (for example, a selection other than C:C, say
C4:C100).

Definitely not true. All depends on what one wants: the native position or
within ref position.

If the ref of interest is C4:C100...

=MATCH(9.99999999999999E+307,C4:C100)

will give the position of the last numerical value within C4:C100, that is,
a position relative to C4:C100. If you want it to be expressed relative to
column C, which I refer to as the native position, you need to expand...

=MATCH(9.99999999999999E+307,C4:C100)+CELL("Row",C4)
The Index/Match gives a reference (unlike Lookup) so can use Row(..)
etc to find the location/address. Therefore, an alternative method to
find the last numerical or text value, the following can be used :

=IF(COUNT(Data)=0,INDEX(Data,MATCH(REPT("z",255),Data)),
IF(COUNTA(Data)-COUNT(Data)=0,INDEX(Data,MATCH(9.99999999999999E+307,Data)),
INDEX(Data,MAX(MATCH(REPT("z",255),Data),MATCH(9.99999999999999E+307,Data)))
))

I'd use since you have a definite ref (Data does not refer to whole column)
for the last value either text or numerical (not: last logical or error
value) something like...

=LOOKUP(9.99999999999999E+307,1/(1-(ISLOGICAL(Data)+ISERROR(Data)+ISBLANK(Da
ta))),Data)

Note that the last formula is restricted to definite refs and expensive,
although less expensive than one you suggest, while [1] and [2], addressing
the last numerical or the last text value are efficient and insensitive to
irrelevant type of data points plus empty cells.

If Data would be a whole column type of ref, say A:A, one could use multiple
cells...

F1:

=MATCH(9.99999999999999E+307,A:A)

F2:

=MATCH(REPT("z",255),A:A)

F3:

=CHOOSE(COUNT(F1:F2)+1,"",SUMIF(F1:F2,"<>#N/A"),MAX(F1:F2))

In F4 you could then fetch the value of interest:

=IF(N(F3),INDEX(A:A,F3),"")
 
Aladin Akyurek said:
I'd use since you have a definite ref (Data does not refer to
whole column) for the last value either text or numerical (not:
last logical or error value) something like...

=LOOKUP(9.99999999999999E+307,1/(1-(ISLOGICAL(Data)+ISERROR(Data)
+ISBLANK(Data))),Data)
....

Save yourself some typing.

=LOOKUP(2,1/(ISNUMBER(Data)+ISTEXT(Data)),Data)
If Data would be a whole column type of ref, say A:A, one could use
multiple cells...
....

Or in a single cell,

=IF(OR(ISNUMBER(A65536),ISTEXT(A65536)),A65536,
LOOKUP(2,1/(ISNUMBER(A1:A65535)+ISTEXT(A1:A65535)),A1:A65535)
 
Harlan Grove said:
...

Save yourself some typing.

=LOOKUP(2,1/(ISNUMBER(Data)+ISTEXT(Data)),Data)

Quite. I should have chosen a setup in positive mood...
 
Back
Top