Excel formula/function to subtract until 0

agrych

New Member
Joined
Jul 24, 2019
Messages
4
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[TD="align: center"]D[/TD]
[TD="align: center"]E[/TD]
[TD="align: center"]AA[/TD]
[TD="align: center"]AB[/TD]
[TD="align: center"]AC[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Invoices[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Year[/TD]
[TD]Remaining[/TD]
[TD]Task[/TD]
[TD]Funding[/TD]
[TD]Invoice[/TD]
[TD]Task[/TD]
[TD]Fund[/TD]
[TD]Amount[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]2017[/TD]
[TD]3014.03[/TD]
[TD]97[/TD]
[TD]M[/TD]
[TD]0[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]2015[/TD]
[TD]6460.71[/TD]
[TD]98[/TD]
[TD]L6[/TD]
[TD]60.71[/TD]
[TD]98[/TD]
[TD]L6[/TD]
[TD]60.71[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]2018[/TD]
[TD]580.28[/TD]
[TD]99[/TD]
[TD]F[/TD]
[TD]0[/TD]
[TD]100[/TD]
[TD]A[/TD]
[TD]5600.74[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]2019[/TD]
[TD]6956.39[/TD]
[TD]99[/TD]
[TD]F[/TD]
[TD]0[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]2017[/TD]
[TD]5510.74[/TD]
[TD]100[/TD]
[TD]A[/TD]
[TD]5510.74[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]13[/TD]
[TD]2018[/TD]
[TD]8737.52[/TD]
[TD]100[/TD]
[TD]A[/TD]
[TD]90[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]14[/TD]
[TD]2019[/TD]
[TD]5000.00[/TD]
[TD]100[/TD]
[TD]A[/TD]
[TD]0[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="width: 550"]
<tbody>[TR]
[TD]From visible rows, I need to subtract the Invoice amount $5600.74 from the remaining funds for Task 100 Fund A until invoice is zero - recording the amount of the invoice used under the invoice column. Notice some rows are hidden as this is a filtered table.

Having a hard time writing a formula for cells in column E to handle subtracting the invoice amount until it has all been applied to the remaining funds (oldest year first). Perhaps a VBA function could handle this?

Any suggestions would be greatly appreciated.[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Welcome to the forums.

Why are there hidden rows? Can you show them for example's sake?
 
Upvote 0
The hidden rows have a remaining value of 0 in column B
For example:
A B C D E
Row 4: 2014 0 98 L6 0
Row 6: 2018 0 99 F 0
Row 12: 2017 0 100 A 0
 
Upvote 0
Enter with Ctrl+Shift+Enter in Cell E3:
Code:
=IF($B3=0,0,IF(INDEX($AE$3:$AE$4,MATCH($C3&$D3,$AC$3:$AC$4&$AD$3:$AD$4,0))-SUMIFS($E$2:$E2,$D$2:$D2,$D3)=0,0,MIN($B3,INDEX($AE$3:$AE$4,MATCH($C3&$D3,$AC$3:$AC$4&$AD$3:$AD$4,0))-SUMIFS($E$2:$E2,$D$2:$D2,$D3))))

Will this work for you? The errors that appear are due to the Task/Funding classes not within the Task/Fund range in columns AC/AD, so you'd either need to 1) add them, and then alter the formula, 2) ignore the errors, or 3) wrap the formula within a IFERROR function.

Reach out with any questions.
 
Upvote 0
Thank you! I had to modify it a bit to get it to work for me.
The following works as desired.

=IFERROR(IF($B3=0,0,IF(INDEX($AC$3:$AC$14,MATCH($C3&$D3,$AA$3:$AA$14&$AB$3:$AB$14,0))-SUMIFS($E$2:$E2,$C$2:$C2,$C3,$D$2:$D2,$D3)=0,0,MIN($B3,INDEX($AC$3:$AC$14,MATCH($C3&$D3,$AA$3:$AA$14&$AB$3:$AB$14,0))-SUMIFS($E$2:$E2,$D$2:$D2,$D3)))),0)
 
Upvote 0
Sorry, this is the correct formula that worked for me:
=IFERROR(IF($B5=0,0,IF(INDEX($AC$3:$AC$14,MATCH($C5&$D5,$AA$3:$AA$14&$AB$3:$AB$14,0))-SUMIFS($E$2:$E4,$C$2:$C4,$C5,$D$2:$D4,$D5)=0,0,MIN($B5,INDEX($AC$3:$AC$14,MATCH($C5&$D5,$AA$3:$AA$14&$AB$3:$AB$14,0))-SUMIFS($E$2:$E4,$C$2:$C4,$C5,$D$2:$D4,$D5)))),0)
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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