Excel: lookup the last non-zero, non-empty cell in filtered range

microsoft excelmicrosoft-excel-2010worksheet-function

The following formula gives the last visible data in a filtered range:

=LOOKUP(2,1/SUBTOTAL(2,OFFSET(R5,ROW(R5:R200)-ROW(R5),0)),R5:R200)

However, it'd return zero if the last visible cell contains zero. Anyway to filter off zero and return the last greater than zero cell?

The following is a simplified example. Both date and sport are filtered. The filters could be altered so June and basket ball are displayed, for example.

The next two columns are the balance of two currencies. I need a formula at the bottom of the currencies that displays 193 and 525 respectively. The lookup formula I found on the net displays 193 and 0, where 0 is the last number in the filtered data of a currency. What I want to display is 525, not 0. By the way, zero is not zero per se. Zero means no transaction. It actually should be blank. However, I can't make them blank because they are involved in calculation. Addition of a number to a blank gives an error.

This is for a monthly balance sheet report. I need to report 193 and 525 as the ending balances for July.

2019/07/01  tennis  775           0
2019/07/11  tennis  925           0
2019/07/11  tennis  0           125
2019/07/13  tennis  0           105
2019/07/13  tennis  0           525
2019/07/25  tennis  193           0

Another formula:

LOOKUP(2,1/(R5:R200<>0),R5:R200)

will display the last non-zero number, whether visible or not. It's not what I want either.

Best Answer

One was of accomplish this is adding your formula above into an index match formula. It would look something like this

=INDEX(R5:R200,(LOOKUP(2,1/SUBTOTAL(2,OFFSET(R5,ROW(R5:R200)-ROW(R5),0)),R5:R200),1)

I use a similar formula; INDEX(Entry20!D2:D367,(LOOKUP(2,1/(Entry20!BG2:BG367<>0),ROW(Entry20!BG2:BG367))),1) in my case column D contains the date and I'm looking for the date that the last non-zero value came up. I tried changing this to BG2:BG367 and it provided the last non-zero value.

I hope this helps, Brad

Related Question