Function for a given number of various subtractions?

amwilson97

New Member
Joined
Feb 2, 2018
Messages
6
I need to create a function that will allow me to subtract a number P1 from an initial value, followed by this P1 being subtracted from the result RP1 times. Once P1 has been subtracted RP1 times, P2 must be subtracted RP2 times, and then P3 subtracted RP3 times until 0, which it will do as the data is based on a payment scheme. The problem I am having is creating a general formula which allow me to do this for any initial value, P1,P2,P3, RP1,RP2 and RP3. I can't figure out how to perform the function using the cell above rather than the initial value every time, and I have no idea how to perform each subtraction the required number of times. I am very new to this so I'm sure this must seem very straightforward to you people! If anyone could help create me a formula that would be very appreciated, thanks
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Welcome to the board!

I'm a little unclear on your question, but I think I have enough to take a shot at it:

ABCDE
BalancePaymentCount

<colgroup><col style="width: 25pxpx"><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: right"][/TD]

[TD="align: right"][/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]1000[/TD]
[TD="align: right"][/TD]
[TD="align: right"]100[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]0[/TD]

[TD="align: center"]3[/TD]
[TD="align: right"]900[/TD]
[TD="align: right"][/TD]
[TD="align: right"]75[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]5[/TD]

[TD="align: center"]4[/TD]
[TD="align: right"]800[/TD]
[TD="align: right"][/TD]
[TD="align: right"]25[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]9[/TD]

[TD="align: center"]5[/TD]
[TD="align: right"]700[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]6[/TD]
[TD="align: right"]600[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]7[/TD]
[TD="align: right"]500[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]8[/TD]
[TD="align: right"]425[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]9[/TD]
[TD="align: right"]350[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]10[/TD]
[TD="align: right"]275[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]11[/TD]
[TD="align: right"]200[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]12[/TD]
[TD="align: right"]175[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]13[/TD]
[TD="align: right"]150[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]14[/TD]
[TD="align: right"]125[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]15[/TD]
[TD="align: right"]100[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]16[/TD]
[TD="align: right"]75[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]17[/TD]
[TD="align: right"]50[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]18[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]19[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]20[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Sheet5

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<thead>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH="width: 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]A3[/TH]
[TD="align: left"]=MAX(0,A2-INDEX($C$2:$C$4,MATCH(ROW()-3,$E$2:$E$4)))[/TD]
[/TR]
[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]E3[/TH]
[TD="align: left"]=E2+D2[/TD]
[/TR]
[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]E4[/TH]
[TD="align: left"]=E3+D3[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]



Put your balance in A2, the payment amounts in C2:C4, and the number of times for each payment in D2:D4. The numbers in E2:E4 are running totals of the payments. Now put the formula in A3 and drag down. Change the references in the formulas to match your sheet. The E column can in fact be incorporated into the formula, but it would make it a lot more complicated. We also may need to adjust the formula based on what row you start in.

Let us know if this helps.
 
Upvote 0
Hi, thanks for the quick reply. This works perfectly for one set of data, but i need to create a template that can be extended to any variety of starting amount, payments and repetitions of each payment. ideally, if I could put the data for this in A1:A7 for example, so each column represents one set of data, so column B could then be separate and so on that would help. I don't see a way to incorporate your column E into this though, unless there is a way to hide this?

Column A
Initial amount
Payment 1
repetitions of P1
Payment 2
Repetitions of P2
Payment 3
Repetitions of P3

Initial Value
Balance after Payment 1
etc
etc
etc
Until 0

This kind of represents what I need, so the formula can be extended across to new columns and the payment scheme be automatically shown for new sets of data. Is this possible?
Thanks
 
Upvote 0
Oh also, is there a way to allow the first payment to occur at any point in column A rather than only being able to use A2? Each payment scheme may start on a different month so one might start in A2 but another might need to start in B6, sorry if this overcomplicates things, this would just be so uselful if I could make the template work.
 
Upvote 0
Column E could theoretically be incorporated into the formula, but there are bigger issues with your proposed layout. If you want everything in one column, I think a formula solution is out. I could write a macro to do that. You'd enter the data in the format you describe, select the cell with the balance, then call the macro, and the macro would put the running balances lower down in the same column.

If you want formulas, I'd strongly recommend 3 columns. Something like:

KLM
StartBalances
PaymentsCounts

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: right"][/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]1000[/TD]
[TD="align: right"][/TD]
[TD="align: right"]1000[/TD]

[TD="align: center"]3[/TD]

[TD="align: right"]900[/TD]

[TD="align: center"]4[/TD]
[TD="align: right"]100[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]800[/TD]

[TD="align: center"]5[/TD]
[TD="align: right"]75[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]700[/TD]

[TD="align: center"]6[/TD]
[TD="align: right"]25[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]600[/TD]

[TD="align: center"]7[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]500[/TD]

[TD="align: center"]8[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]425[/TD]

[TD="align: center"]9[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]350[/TD]

[TD="align: center"]10[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]275[/TD]

[TD="align: center"]11[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]200[/TD]

[TD="align: center"]12[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]175[/TD]

[TD="align: center"]13[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]150[/TD]

[TD="align: center"]14[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]125[/TD]

[TD="align: center"]15[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]100[/TD]

[TD="align: center"]16[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]75[/TD]

[TD="align: center"]17[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]50[/TD]

[TD="align: center"]18[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]25[/TD]

[TD="align: center"]19[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]0[/TD]

[TD="align: center"]20[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]0[/TD]

[TD="align: center"]21[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]0[/TD]

[TD="align: center"]22[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]0[/TD]

</tbody>
Sheet5



You'd put your parameters in K:L (or whatever), and then put the formula in M and drag down. Not too friendly though. I'd probably try the macro. Let me know what you think, and which way you want to go, and I'll write something up.
 
Upvote 0
Ok, I have no clue how to use macros so if you think that's the best way to go, I would be eternally grateful if you could that. Its up to you
Just one thing, I've just realised ill need two more payment types at the end that will always be taken off once each, and this will always result in the balance reaching 0 at the end.
So, if i can put months in column A, starting at Jan-2012 so each row represents a new month, then each column represents a payment scheme, with 5 types of payments with varying repetitions except for the final two which each only have one rep, that would be awesome. No rush though man honestly I'm extremely grateful
 
Upvote 0
Try this.

Open a COPY of your workbook. Press Alt-F11 to open the VBA editor. From the menu, click Insert > Module. On the sheet that opens, paste this code:

Code:
Sub PaymentMaker()
Dim MyData As Variant, MyDict As Object, i As Long, ctr As Long, j As Long

    If MsgBox("Are you sure that the selected cell is on the starting balance?", vbOKCancel, _
               "Payment calculator") = vbCancel Then Exit Sub
    
    MyData = Range(ActiveCell, ActiveCell.End(xlDown)).Value
    Set MyDict = CreateObject("Scripting.Dictionary")
    MyDict.Add 1, MyData(1, 1)
    ctr = 1
    For i = 2 To UBound(MyData) Step 2
        For j = 1 To MyData(i + 1, 1)
            MyData(1, 1) = MyData(1, 1) - MyData(i, 1)
            ctr = ctr + 1
            MyDict.Add ctr, MyData(1, 1)
        Next j
    Next i
    
    ActiveCell.Offset(UBound(MyData) + 3).Resize(MyDict.Count) = WorksheetFunction.Transpose(MyDict.items)
    
End Sub
Go back to your workbook. Somewhere on your sheet, select a cell, and enter the parameters like you said, for example:

G

<tbody>
[TD="align: center"]8[/TD]
[TD="align: right"]1200[/TD]

[TD="align: center"]9[/TD]
[TD="align: right"]100[/TD]

[TD="align: center"]10[/TD]
[TD="align: right"]5[/TD]

[TD="align: center"]11[/TD]
[TD="align: right"]75[/TD]

[TD="align: center"]12[/TD]
[TD="align: right"]4[/TD]

[TD="align: center"]13[/TD]
[TD="align: right"]25[/TD]

[TD="align: center"]14[/TD]
[TD="align: right"]8[/TD]

[TD="align: center"]15[/TD]
[TD="align: right"]150[/TD]

[TD="align: center"]16[/TD]
[TD="align: right"]1[/TD]

[TD="align: center"]17[/TD]
[TD="align: right"]50[/TD]

[TD="align: center"]18[/TD]
[TD="align: right"]1[/TD]

</tbody>
Sheet5



Initial amount, payment 1, # of times of payment 1, payment 2, # of times of payment 2, etc. You can make this list as long as you want, as long as it's in this format. If you have single payments, just put 1 for the number of times.

Now select the cell with the Initial amount, G8 in this example. Press Alt-F8, select PaymentMaker, and click Run. You should get this:

G

<tbody>
[TD="align: center"]8[/TD]
[TD="align: right"]1200[/TD]

[TD="align: center"]9[/TD]
[TD="align: right"]100[/TD]

[TD="align: center"]10[/TD]
[TD="align: right"]5[/TD]

[TD="align: center"]11[/TD]
[TD="align: right"]75[/TD]

[TD="align: center"]12[/TD]
[TD="align: right"]4[/TD]

[TD="align: center"]13[/TD]
[TD="align: right"]25[/TD]

[TD="align: center"]14[/TD]
[TD="align: right"]8[/TD]

[TD="align: center"]15[/TD]
[TD="align: right"]150[/TD]

[TD="align: center"]16[/TD]
[TD="align: right"]1[/TD]

[TD="align: center"]17[/TD]
[TD="align: right"]50[/TD]

[TD="align: center"]18[/TD]
[TD="align: right"]1[/TD]

[TD="align: center"]19[/TD]
[TD="align: right"][/TD]

[TD="align: center"]20[/TD]
[TD="align: right"][/TD]

[TD="align: center"]21[/TD]
[TD="align: right"][/TD]

[TD="align: center"]22[/TD]
[TD="align: right"]1200[/TD]

[TD="align: center"]23[/TD]
[TD="align: right"]1100[/TD]

[TD="align: center"]24[/TD]
[TD="align: right"]1000[/TD]

[TD="align: center"]25[/TD]
[TD="align: right"]900[/TD]

[TD="align: center"]26[/TD]
[TD="align: right"]800[/TD]

[TD="align: center"]27[/TD]
[TD="align: right"]700[/TD]

[TD="align: center"]28[/TD]
[TD="align: right"]625[/TD]

[TD="align: center"]29[/TD]
[TD="align: right"]550[/TD]

[TD="align: center"]30[/TD]
[TD="align: right"]475[/TD]

[TD="align: center"]31[/TD]
[TD="align: right"]400[/TD]

[TD="align: center"]32[/TD]
[TD="align: right"]375[/TD]

[TD="align: center"]33[/TD]
[TD="align: right"]350[/TD]

[TD="align: center"]34[/TD]
[TD="align: right"]325[/TD]

[TD="align: center"]35[/TD]
[TD="align: right"]300[/TD]

[TD="align: center"]36[/TD]
[TD="align: right"]275[/TD]

[TD="align: center"]37[/TD]
[TD="align: right"]250[/TD]

[TD="align: center"]38[/TD]
[TD="align: right"]225[/TD]

[TD="align: center"]39[/TD]
[TD="align: right"]200[/TD]

[TD="align: center"]40[/TD]
[TD="align: right"]50[/TD]

[TD="align: center"]41[/TD]
[TD="align: right"]0[/TD]

</tbody>
Sheet5



Also, press Alt-F8 again. Select PaymentMaker, but this time click the Options... button. Where it says Shortcut key, you can put a letter in the box after Ctrl+. If you put a in that box, and click OK, you can now run this macro by just clicking Ctrl+a, you don't need to go through the Alt-F8 menu anymore.

Let me know how this works.
 
Last edited:
Upvote 0
When I try to press alt-f8 and then choose the macro, i get an error saying Run Time error '9': Subscript out of range??
 
Upvote 0
Wait no it has worked now because I'd put headers in the leftmost column which seemed to be screwing around with the code. IS there a way I can have these in, and so I can choose where to start the payments from, for example in August 2013, rather than Jan 2012?
 
Upvote 0
This is designed to do one column at a time. You put your parameters in a column, starting at B2, or G8. Then SELECT the cell with the starting balance in it, like G8. Then run the macro.

If you want to do multiple columns at a time, I'm not sure how to choose them. I could look at the column headers, and if it's non-empty, look for the lowest block of numbers in that column and run the routine based on that. But that seems pretty risky. You'd need a better way to identify the starting parameters in each column.
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,713
Members
452,667
Latest member
vanessavalentino83

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