+ Reply to Thread
Results 1 to 9 of 9

Formula to return last but one cell in row!

  1. #1
    Forum Contributor
    Join Date
    03-22-2006
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    275

    Formula to return last but one cell in row!

    Hi all

    I currently use the following formula to find the last used cell in a row:

    =LOOKUP(2,1/($6:$6<>""),$6:$6)

    which works fine but I have been trying to amend it so that it returns the last but one cell in a row. Have tried using it with Offset but without success.

    Have found other solutions to finding to the last cell in a row albeit that a number of them do not seem to work with my project and likewise none of them seemed to allow customisation of any sort.

    Any suggestions ...spellbound
    Last edited by rylo; 03-23-2009 at 09:40 PM.

  2. #2
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Formula to return last but one cell in row!

    Assuming you don't have gaps, i.e. to get the cell immediately before

    =INDEX($6:$6,LOOKUP(2,1/($6:$6<>""),COLUMN($6:$6))-1)

    or a shorter version that needs to be "array entered"

    =INDEX($6:$6,MATCH(2,1/($6:$6<>""))-1)

    confirm with CTRL+SHIFT+ENTER

  3. #3
    Forum Contributor
    Join Date
    03-22-2006
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    275

    Re: Formula to return last but one cell in row!

    Hi daddylonglegs

    Thanks for that quick reply.

    Assuming you don't have gaps
    Do you mean by that, any blank cells within the row to be tested e.g. from A1 to BZ1. If so, yes there gaps.

    I have just tried out your formula on about 10 different rows, all of which have "gaps" and it has returned the correct result in each instance.

    I prefer the non array method, especially as I'm trying to cut down on using any more array formulas, to avoid making my workbook run even slower than it already does.

    By the way, would you have any thoughts on how to make the formula work so that it references the current row where it is located?

    Thanks again ...spellbound

  4. #4
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Formula to return last but one cell in row!

    What I mean about gaps is.....

    Your original formula returns the last value in the row. So if that value is in Z6 my suggested formulas will give you the value in Y6, even if Y6 is blank, so you might want to return the previous populated cell in row 6, be that X6, W6 or whatever.......for that you can use this formula

    =INDEX($6:$6,LARGE(IF($6:$6<>"",COLUMN($6:$6)),2))

    which needs to be confirmed with CTRL+SHIFT+ENTER

  5. #5
    Forum Contributor
    Join Date
    03-22-2006
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    275

    Re: Formula to return last but one cell in row!

    Having problems for some reason but just to confirm:

    Your original formula returns the last value in the row. So if that value is in Z6 my suggested formulas will give you the value in Y6, even if Y6 is blank
    this is exactly what I need but since my first tests, the results have gone out the window so to speak.

    I removed the absolute references, so that it could be pasted into other rows.

    e.g. =INDEX(6:6,LOOKUP(2,1/(6:6<>""),COLUMN(6:6))-1)

    In one column, the formula in row 6 is now returning the result from row 10 but if I go back to using the formula with absolute references, it works fine.

    Likewise when I tried to use the above formula in Column B, I started getting 'Circular References' for some reason and I found that when the formula is entered in Column B on any row, either with or without Absolute references, it results in a circular reference.

    Any suggestions because I can't see how using the formula in different columns would produce such conflicting results.

    spellbound
    Last edited by Spellbound; 03-21-2009 at 08:50 AM. Reason: Amended due to results of testing formula.

  6. #6
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Formula to return last but one cell in row!

    You don't want to put the formula in the referenced row if it refers to the whole row.

    If your data range ends at column BZ then use the formula in CA6, tweaked to make it more "robust"

    =INDEX(A6:BZ6,LOOKUP(2,1/(A6:BZ6<>""),COLUMN(A6:BZ6)-COLUMN(A6)))

  7. #7
    Forum Contributor
    Join Date
    03-22-2006
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    275

    Re: Formula to return last but one cell in row!

    Unfortunately, I could not use your last solution because the formula has to reside in Column B of the same row and the data range expands weekly by 6 columns per week.

    I have tried out numerous solutions from the web but they were all producing a circular reference which turned out to be a conflict with a specific type of formula that I use throughout this particular worksheet.

    Out of interest the conflicting formula was:

    =SUMIF($B$5:$B$2191,INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN($B$1),4),"1","")&(ROW()-1)),CE$5:CE$2191)

    By virtue of its reference to Column B. So I will have to change all of these formulas to:

    =SUM(OFFSET(CE86,-COUNTIF($B$5:$B85,$B85),,):OFFSET(CE86,-1,0))

    Which sum the relevant blocks of data as required.

    This enabled me to make use of daddylonglegs formula:

    =INDEX($6:$6,LOOKUP(2,1/($6:$6<>""),COLUMN($6:$6))-1)

    Which I have tested in about 100 rows and no obvious problems have arisen at this time.

    I also came across this formula on XLDynamic.com which is supposed to be the fastest solution for finding the last numeric cell but I was unable to make it work in returning the last but one numeric cell.

    =INDEX(1:1,MATCH(9.99999999999999E307,1:1))

    I would be interested to learn if there is a way of adapting this formula to return the last but one numeric cell.

    It is surprising how long this has taken to sort out for what I hoped would be a minor change.

    spellbound

  8. #8
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Formula to return last but one cell in row!

    You didn't say you wanted the last numeric value

    Simplest formula for that (not necessarily the most efficient but it won't make much difference over a relatively short range)

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

    but to get the second last you'd use INDEX/MATCH, i.e.

    =INDEX(1:1,MATCH(9.99999999999999E+307,1:1)-1)

    Note: as with some of the previous formulas suggested this gives the cell before the last numeric value, regardless of what's in that cell so it wouldn't necessarily be the second last numeric if that cell was blank or contained text......

  9. #9
    Forum Contributor
    Join Date
    03-22-2006
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    275

    Re: Formula to return last but one cell in row!

    That works fine in this instance as the last cell and the last cell but one will always be numeric.

    Mind you, I could have sworn that I tried out this formula with -1 at the end but sometimes it is quite easy to miss the obvious.

    Thanks for your help …spellbound

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1