Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Last Non-Zero Value in a Row.

Last Non-Zero Value in a Row

by Allen Wyatt
(last updated May 27, 2017)

5

Brian has a row of numbers with 240 cells. In this row, the numbers are steadily declining and will eventually, at some point in those 240 cells, become 0. The zeroes will continue to fill the remaining cells in the row. Brian needs to write an equation that will return the last non-zero value in the row.

There are a variety of ways that the desired value can be returned. (Doesn't that always seem to be the case with Excel? You can come up with lots of ways to get a result.) In general, you could use a regular formula or an array formula.

If you want to use a regular formula, here's one you can try:

=OFFSET(A6,0,(COUNT(A6:IF6)-COUNTIF(A6:IF6,0))-1)

The COUNTIF function counts the number of zero values and the COUNT function determines the number of cells in the range. Subtracting one from the other and adjusting by 1 gives the OFFSET value into the "array" of cells where the last non-zero value lies. This formula assumes that the values begin in column A; if they begin in a different column then you'll need to adjust the value provided by the COUNT/COUNTIF portion of the formula to represent the offset from the first column.

Here's a shorter variation of the formula, based on doing an offset from the right side of the range rather than the left side:

=OFFSET(IF6,0,-COUNTIF(A6:IF6,0))

In this instance it is important that IF6 be the actual right end of the range. The formula works by counting the number of zero values in the range (all at the right side of the range) and then computing the cell address of the last cell (IF6) minus the number of zeros.

Here is a version that uses the INDEX function, instead:

=INDEX(A6:IF6,,MATCH(0,A6:IF6,0)-1)

This version is even shorter, using the LOOKUP function:

=LOOKUP(1,1/(6:6>0),6:6)

Array formulas can also be used. (Array formulas are entered by pressing Ctrl+Shift+Enter.) This one uses the INDIRECT function:

=INDIRECT("R6C" & MAX((A6:IF6>0)*COLUMN(A6:IF6)),FALSE)

This array formula uses an interesting implementation of the LOOKUP function to find the correct result:

=LOOKUP(9.99999999999999E+307,IF(A6:IF6<>0,A6:IF6))

Here's another array formula that can be used, this time using the OFFSET function to find the last non-zero value in row 6:

=OFFSET(A6,0,MIN(IF(6:6=0,COLUMN(6:6),300))-2)

Here's an even shorter variation:

=MIN(IF(A6:IF6>0,A6:IF6))

All of these formulas presented so far depend on the fact that the numbers in the row actually do decline—they go from whatever the beginning number is and steadily go toward zero. If the numbers don't decline, then you can use a different type of array formula to determine the last non-zero value in the row:

=INDEX(6:6,MAX(IF(A6:IF6<>0,COLUMN(A6:IF6))))

The formula first determines the maximum column in the row (in this case row 6) that has a value not equal to zero, then it uses the INDEX function to get the value from that column in that row.

As you can tell, there are quite a few ways to find the last non-zero value in a row. Pick the one that strikes your fancy; there is no right or wrong in this instance.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11250) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Last Non-Zero Value in a Row.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Displaying a Hidden First Row

If you hide the first rows of a worksheet, you may have a hard time getting those rows visible again. Here's a simple way ...

Discover More

Updating Document Links

If you establish dynamic links between documents, then you can force Word to update those links whenever you want. How ...

Discover More

Protecting a Single Worksheet

Excel allows you to protect your worksheets easily—and that includes if you need to protect only a single worksheet ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Counting Employees in Classes

Excel is very good at counting things, even when those things need to meet specific criteria. This tip shows how you can ...

Discover More

Activating the Formula Bar with the Keyboard

Hate to take your hands off the keyboard while working on a worksheet? Here's one way to activate the Formula Bar without ...

Discover More

Returning a Weight and a Date

If you have two columns containing dates and weights from those dates, you may want to pick a date associated with a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 + 0?

2018-03-02 06:17:58

Steve Jez

Some.Dude,
If you enter some sample data in a sheet & then use "Evaluate Formula" from the Formulas Ribbon Tab it might help. You will have to put the formula as written in any row other than row 6 or you will get a circular reference.
When the formula evaluates you will see;
(6:6>0) you see Trues or Falses then 1 or 0 for the content of row 6
(1/1 or 0) you see 1 or #Div/0! - the last cell which is not blank or zero will evaluate to 1
(1,(1 or #Div/0!) lookup is now trying to find 1 in an array of 1's & #Div/0! & it tries to find the last one (it's the way it is programmed), so when it finds the last 1 it returns the cell value of row 6

You don't have to use whole row references, I have used it to report the latest revision in col C in the format =lookup(1,1/D6:IV6>0),D6:IV6) the only caveat is that the 2 ranges must be the same size.

It's not massively intuitive, but I hope this helps.


2018-03-01 10:59:12

Some.Dude

Re: Last Non-Zero Value in a Row

=LOOKUP(1,1/(6:6>0),6:6)

Can anyone please help me understand how/why this formula works?



2018-02-20 12:09:55

Peter Atherton

GSR

Try the following, works from the last cell in the range, in your case M17

=COUNTIF(OFFSET($M17,0,-4,,5),"<"&0.98)


2018-02-19 12:32:50

GSR

I have a 2 rows in excel table.- 1st row is for Month and 2nd row is for score. The table is dynamic that a new month is added after every month. I need a formula which counts the score less than 98% from last 5 cells. I am trying something like-

=OFFSET($D$17,0,1,COUNTIF(D17:M17, "<0.98"),-5)

but this ain't working. Can someone help me.

e.g

if I have scores of 95%, 97%, 96%, 95%, 95%, 98%, 98%, 97%, 99%

The formula should show me the outcome as 2 as 2 values out of last 5 values have score less than 98%


2017-11-07 10:44:42

Ken Schmidt

The first OFFSET formula worked great for me. Thanks! The formula will also return the second to last non-zero value in the row by changing the 1 at the end of the formula to 2, changing it to 3 the third to last etc.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.