Adding cells until they are greater than or equal to another number

Raley

New Member
Joined
Jan 11, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
1610382156722.png

I can't seem to figure out how to write the formula/script to add each cell in "Money Made" until it is greater than or equal to Money Owed and then have it related the years in the "How many Years to Pay off" column.
The answer I would be looking for here is 3 Years to pay off.
Thank you in advance
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
This should work, note that it will also show 4, 5 etc for all subsequent years. Should you extend the loan say in year 3 then that too will be taken into account.

Book1
ABCD
1YearMoney MadeMoney OwedHow Many Years to Pay Off
21$6,150.00($16,050.00 ) 
32$7,150.00 
43$8,150.003
54$9,150.004
Sheet1
Cell Formulas
RangeFormula
D2:D5D2=IF(SUM($B$2:$B2)+SUM($C$2:$C2)>0,[@Year],"")


HTH
 
Upvote 0
Hi Raley,

use below code:

VBA Code:
Sub CalculateYears()
    Dim totalRows As Integer, totalAmount As Long, rowno As Integer
    totalRows = WorksheetFunction.CountA(ActiveSheet.Range("B:B"))
    totalAmount = 0
    
    For rowno = 2 To totalRows
        totalAmount = totalAmount + ActiveSheet.Range("B" & rowno)
        If totalAmount >= ActiveSheet.Range("C2") Then Exit For
    Next
    ActiveSheet.Range("D2") = rowno - 1
End Sub

Thanks,
Saurabh
 
Upvote 0
Solution
That’s why different answers are encouraged. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,994
Messages
6,122,633
Members
449,092
Latest member
bsb1122

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