Find the first and last non-zero numbers in a row and subtract them

vladimiratanasiu

Active Member
Joined
Dec 17, 2010
Messages
346
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello!

I have a monthly large table of stocks with a lot of products, as the example shown in the linked document Book3.xlsx . Taking into account that each product has variations in terms of volume, some days it has zero / non-zero values. I need a formula / macro to find the first and the last non-zero numbers from a row (e.g. B3, AE3 / D4, AF4) and subtract the first from the last one (e.g. AE3-B3/ AF4-D4 - see the yellow cells).

Thank you!
 
Another possible approach too

21 12 12.xlsm
CDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
1Day/monthTotal
212345678910111213141516171819202122232425262728293031
3100701200116000049003960170180000229600543019018
400590000140001389442190000014901860001126226900190440901-589
50000000000000000000000000000000 
61234567891011121314151617181920212223242526272829303130
7000000000000000099900000000000000999
Last - First
Cell Formulas
RangeFormula
AH3:AH7AH3=IFNA(LOOKUP(9E+99,IF(C3:AG3=0,"",C3:AG3))-IF(COUNTIF(C3:AG3,">0")=1,0,INDEX(C3:AG3,MATCH(TRUE,C3:AG3>0,0))),"")
Press CTRL+SHIFT+ENTER to enter array formulas.

Thank you too for the answer !

I applied your array formula and it works well. The only issue could be the instability of formula's content. When I simply access it in the formula bar, the arrays dissapear and the final result is replaced with message #VALUE. I don't use very often this type of formula, that's why I could miss the method for overcoming the mentioned inconvenient.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,214,573
Messages
6,120,318
Members
448,956
Latest member
Adamsxl

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