First/Last Non-Zero Value in a list

Syer

New Member
Joined
Dec 8, 2019
Messages
6
Office Version
  1. 2013
Platform
  1. Windows
Heyy

I'm looking for formula to deduct 2 from the first non-zero cell and from the last non-zero value cell only.
So far I use IF formula (with reference to the table below):

=IF(B1=(INDEX($B1:$F1,MATCH(TRUE,INDEX($B1:$F1<>0,),0))), (B1-2), IF(B1=(LOOKUP(2,1/($B1:$F1<>0),$B1:$F1)), (B1-2), B1))

Data02441
Result0044-1

Problem 1
The problem here is that if there are similar number for the first non-zero (INDEX($B2:$F2,MATCH(TRUE,INDEX($B2:$F2<>0,),0)) or the last non-zero (LOOKUP(2,1/($B2:$F2<>0),$B2:$F2) within the list, so the formula will result in minus 2 for the duplicate numbers as well instead of just the first/last non-zero value.

Example:
Data02411
Result004-1-1

Problem 2
The second issue is that we don't want -1 result. Therefore, if the last/first non-zero value is 1 (which will result in -1) we need it to minus 1 only and bring the other 1 (from the original minus 2) to the second first/last non-zero value.

Example:
Data11045
Result00043

Any ideas how to solve these problems? Or at least one of them?

Please and thank you!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi
Welcome to the board

Try in B2:
=IF(AND(B1<>0,COUNTIF($B$1:B1,"<>0")=1),MAX(B1-2,0),IF(AND(B1<>0,SUM($B$1:B1)-B1=1),B1-1,IF(AND(B1<>0,SUM(B1:$F$1)-B1=1),B1-1,IF(AND(B1<>0,COUNTIF(B1:$F$1,"<>0")=1),MAX(B1-2,0),B1))))

Copy to the right till F2

Remark: I did not consider the overlap of the first and last values

Ex.:

Book1
ABCDEF
1Data12401
2Result01300
Sheet1
 
Upvote 0
Thank you so much! The formula works like a charm :)

However, I noticed that some of my data is like this:

Data05100
Result0300

How do it take into consideration this overlap?
 
Upvote 0
Hi

I'm glad it helped so far.

You did not post what you want to do in the case of overlap.

Please explain the logic for the case of overlap and also post the result you expect in these 2 cases:

Book1
ABCDEF
1Data05100
2Result
Sheet1


and

Book1
ABCDEF
1Data00300
2Result
Sheet1
 
Upvote 0
Hi,

Apologies for that.

Here's the result that I need for each of the cases:

Case 1:
Data05100
Result02000

*basically the sum of these 2 numbers has to minus 4 since it is the only data available

Case 2:
Data00300
Result0000

*still have to deduct 4 and since it'll be a -1 then it should just become 0

Thank you.
 
Upvote 0
Hi

Try in B2:

=MAX(0,B1-(IF(AND(B1<>0,COUNTIF($B$1:B1,"<>0")=1),2,IF(AND(B1<>0,SUM($B$1:B1)-B1=1),1))+IF(AND(B1<>0,SUM(B1:$F$1)-B1=1),1,IF(AND(B1<>0,COUNTIF(B1:$F$1,"<>0")=1),2))))

Copy to the right
 
Upvote 0

Forum statistics

Threads
1,215,467
Messages
6,124,984
Members
449,201
Latest member
Lunzwe73

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top