VBA help for adding rows on a loop until a specified value is reached

jakeecirg

New Member
Joined
Mar 24, 2025
Messages
25
Office Version
  1. 365
Platform
  1. Windows
So i am new to VBA code and would very much appreciate any help that could be given:

In my column "D & F" there are the names of two separate people. The rows underneath contain the age that they are in each year. (Ascending per year). For example in Cell D14 person 1 is 63 & in D15 (symbolizing a year has passed) person 1 is now 64. I am trying to use some VBA code that will loop through my column ranges "D & F" and continue to add rows at the bottom of the columns until both people turn age 90. This is the formula of how I determine the persons age is here: [=INT((B14-INPUTS!$D$9)/365.25)].

I believe this uses some Until / Loop code but I am running into a dead end.
I would be happy to provide any additional information that could help!
 

Attachments

  • Excel help SS.png
    Excel help SS.png
    24.5 KB · Views: 11
Cell Formulas
RangeFormula
X9X9=Z9-Z8
N10,P10,R10,T10,V10,X10N10=N9/$Z$9
W13,W76,W70,W67,W64,W60:W61,W54,W51,W48,W44:W45,W38,W35,W32,W28:W29,W22,W19,W16W13=$V$7
N13N13=-FV(O13/12,12,-L13/12,N9,0)
P13,X77,X61,V61,X45,V45,T45,X29,V29,T29,R29,R13P13=-FV(Q13/12,12,0,P9,0)
T13T13=-FV(U13/12,12,0,T9,1)
V13V13=-FV($W$13/12,12,0,V9,0)
X13X13=-FV($Y$13/12,12,0,X9,0)
N16,N22,N19N16=-FV(O16/12,12,-L16/12,N14,0)
P16,X86,X83,X80,X70,V70,X67,V67,X64,V64,X54,V54,T54,X51,V51,T51,X48,V48,T48,X38,V38,T38,R38,X35,V35,T35,R35,X32,V32,T32,R32,X22,V22,R22,P22,X19,V19,R19,P19,X16,V16,R16P16=-FV(Q16/12,12,0,P13,0)
T16,T22,T19T16=-FV(U16/12,12,0,T13,1)
O13,O16,O19,O22,O28O13=$N$8
N28,V92,T76,R60,P44N28=N26
P28,X92,X76,V76,X60,V60,T60,X44,V44,T44,R44,X28,V28,T28,R28P28=P25
P29P29=-FV(Q29/12,12,-L29/12,P28,0)
P32,P38,P35P32=-FV(Q32/12,12,-L32/12,P30,0)
Q13,Q16,Q19,Q22,Q28Q13=$P$7
Q29,Q32,Q35,Q38,Q44Q29=$P$8
R45R45=-FV(S45/12,12,-L45/12,R44,0)
R48,R54,R51R48=-FV(S48/12,12,-L48/12,R46,0)
S13,S16,S19,S22,S28:S29,S32,S35,S38,S44S13=$R$7
S45,S48,S51,S54,S60S45=$R$8
T61T61=-FV(U61/12,12,-L61/12,T60,0)
T64,T70,T67T64=-FV(U64/12,12,-L64/12,T62,0)
U13,U16,U19,U22,U28:U29,U32,U35,U38,U44:U45,U48,U51,U54,U60U13=$T$7
U61,U64,U67,U70,U76U61=$T$8
W77,W92:W94,W86,W83,W80W77=$V$8
V77V77=-FV(W77/12,12,-L77/12,V76,0)
V80,V86,V83V80=-FV(W80/12,12,-L80/12,V78,0)
Y13,Y16,Y19,Y22,Y28:Y29,Y32,Y35,Y38,Y44:Y45,Y48,Y51,Y54,Y60:Y61,Y64,Y67,Y70,Y76:Y77,Y80,Y83,Y86,Y92:Y94Y13=$X$7
 
Upvote 0
Bucket Solver - Copy.xlsm
ABACADAEAFAGAHAIAJAKALAMAN
7MonthlyAnnualMonthlyAnnualOne TimeTotalMonthly
8Total Guranteed IncomeReq. ExpensesIncome TaxInv. TTaxIncome gap
9$ 2,840.00$ 34,080.00$ 3,500.00$ 3,500.00$ 42,000.00$ 9,000.00$ 51,000.00$ 113.60$1,363.20$ 1,828.32$ 3,191.52$ 18,283.20$ 18,283.20
10$ 2,896.80$ 34,761.60$ 3,500.00$ 3,570.00$ 42,840.00$ -$ 42,840.00$ 115.87$1,390.46$ 946.89$ 2,337.35$ 9,468.86$ 9,468.86
11$ 2,954.74$ 35,456.83$ 3,500.00$ 3,641.40$ 43,696.80$ -$ 43,696.80$ 118.19$1,418.27$ 965.82$ 2,384.10$ 9,658.24$ 9,658.24
12$ 3,013.83$ 36,165.97$ 3,500.00$ 3,714.23$ 44,570.74$ -$ 44,570.74$ 120.55$1,446.64$ 985.14$ 2,431.78$ 9,851.41$ 9,851.41
13$ 3,074.11$ 36,889.29$ 3,500.00$ 3,788.51$ 45,462.15$ -$ 45,462.15$ 122.96$1,475.57$ 1,004.84$ 2,480.41$ 10,048.43$ 10,048.43
14$ 3,135.59$ 37,627.07$ 3,500.00$ 3,864.28$ 46,371.39$ -$ 46,371.39$ 125.42$1,505.08$ 1,024.94$ 2,530.02$ 10,249.40$ 10,249.40
15$ 3,198.30$ 38,379.62$ 3,500.00$ 3,941.57$ 47,298.82$ -$ 47,298.82$ 127.93$1,535.18$ 1,045.44$ 2,580.62$ 10,454.39$ 10,454.39
16$ 3,262.27$ 39,147.21$ 3,500.00$ 4,020.40$ 48,244.80$ -$ 48,244.80$ 130.49$1,565.89$ 1,066.35$ 2,632.24$ 10,663.48$ 10,663.48
17$ 3,327.51$ 39,930.15$ 3,500.00$ 4,100.81$ 49,209.69$ -$ 49,209.69$ 133.10$1,597.21$ 1,087.67$ 2,684.88$ 10,876.75$ 10,876.75
18$ 3,394.06$ 40,728.75$ 3,500.00$ 4,182.82$ 50,193.89$ -$ 50,193.89$ 135.76$1,629.15$ 1,109.43$ 2,738.58$ 11,094.28$ 11,094.28
19$ 3,461.94$ 41,543.33$ 3,500.00$ 4,266.48$ 51,197.77$ -$ 51,197.77$ 138.48$1,661.73$ 1,131.62$ 2,793.35$ 11,316.17$ 11,316.17
20$ 3,531.18$ 42,374.20$ 3,500.00$ 4,351.81$ 52,221.72$ -$ 52,221.72$ 141.25$1,694.97$ 1,154.25$ 2,849.22$ 11,542.49$ 11,542.49
21$ 3,601.81$ 43,221.68$ 3,500.00$ 4,438.85$ 53,266.16$ -$ 53,266.16$ 144.07$1,728.87$ 1,177.33$ 2,906.20$ 11,773.34$ 11,773.34
22$ 3,673.84$ 44,086.11$ 3,500.00$ 4,527.62$ 54,331.48$ -$ 54,331.48$ 146.95$1,763.44$ 1,200.88$ 2,964.33$ 12,008.81$ 12,008.81
23$ 3,747.32$ 44,967.84$ 3,500.00$ 4,618.18$ 55,418.11$ -$ 55,418.11$ 149.89$1,798.71$ 1,224.90$ 3,023.61$ 12,248.99$ 12,248.99
24$ 3,822.27$ 45,867.19$ 3,500.00$ 4,710.54$ 56,526.47$ -$ 56,526.47$ 152.89$1,834.69$ 1,249.40$ 3,084.08$ 12,493.96$ 12,493.96
25$ 3,898.71$ 46,784.54$ 3,500.00$ 4,804.75$ 57,657.00$ -$ 57,657.00$ 155.95$1,871.38$ 1,274.38$ 3,145.77$ 12,743.84$ 12,743.84
26$ 3,976.69$ 47,720.23$ 3,500.00$ 4,900.84$ 58,810.14$ -$ 58,810.14$ 159.07$1,908.81$ 1,299.87$ 3,208.68$ 12,998.72$ 12,998.72
27$ 4,056.22$ 48,674.63$ 3,500.00$ 4,998.86$ 59,986.34$ -$ 59,986.34$ 162.25$1,946.99$ 1,325.87$ 3,272.85$ 13,258.70$ 13,258.70
28$ 4,137.34$ 49,648.12$ 3,500.00$ 5,098.84$ 61,186.07$ -$ 61,186.07$ 165.49$1,985.92$ 1,352.39$ 3,338.31$ 13,523.87$ 13,523.87
29$ 4,220.09$ 50,641.09$ 3,500.00$ 5,200.82$ 62,409.79$ -$ 62,409.79$ 168.80$2,025.64$ 1,379.43$ 3,405.08$ 13,794.35$ 13,794.35
30$ 4,304.49$ 51,653.91$ 3,500.00$ 5,304.83$ 63,657.99$ -$ 63,657.99$ 172.18$2,066.16$ 1,407.02$ 3,473.18$ 14,070.23$ 14,070.23
31$ 4,390.58$ 52,686.99$ 3,500.00$ 5,410.93$ 64,931.15$ -$ 64,931.15$ 175.62$2,107.48$ 1,435.16$ 3,542.64$ 14,351.64$ 14,351.64
32$ 4,478.39$ 53,740.73$ 3,500.00$ 5,519.15$ 66,229.77$ -$ 66,229.77$ 179.14$2,149.63$ 1,463.87$ 3,613.50$ 14,638.67$ 14,638.67
33$ 4,567.96$ 54,815.54$ 3,500.00$ 5,629.53$ 67,554.36$ -$ 67,554.36$ 182.72$2,192.62$ 1,493.14$ 3,685.77$ 14,931.44$ 14,931.44
MATH
Cell Formulas
RangeFormula
AB9:AB33AB9=SUM(J9,L9,O9,R9,U9,W9,Y9,AA9)
AC9:AC33,AF9:AF33,AJ9:AJ33AC9=AB9*12
AD9:AD33AD9=IF(AND(D9>0,E9>0),$AC$2,$AC$3)
AE9AE9=AD9
AG9:AG33AG9=SUMIFS(INPUTS!$J$17:$J$20,INPUTS!$H$17:$H$20,$C9)
AH9:AH33,AL9:AL33AH9=AF9+AG9
AI9:AI33AI9=(G9*INPUTS!$J$26)+(I9*INPUTS!$J$27)+(L9*INPUTS!$J$28)+(O9*INPUTS!$J$29)+(R9*INPUTS!$J$30)+(U9*INPUTS!$J$31)+(W9*INPUTS!$J$32)+(Y9*INPUTS!$J$33)+(AA9*INPUTS!$J$34)
AK9:AK33AK9=IF(AM9<0,0,AM9*INPUTS!$H$36)
AM9:AM33AM9=-(AC9-(AF9+AG9+AJ9))
AN9:AN33AN9=IF(AM9<0,0,AM9)
AE10:AE33AE10=-FV($AD$3,A10,0,AD10,0)
 
Upvote 0
The first post is my Input Sheet
The second & third post are my CHART sheet (had to break it into two clips)
the fourth post is part of my MATH sheet

Please let me know if this provides you with all you would need to know. Thank you again Sam.
 
Upvote 0
CHART is the main sheet that the VBA would be used on. The other two sheets are where the data is stored that I referenced in the CHART.
 
Upvote 0
CHART is the main sheet that the VBA would be used on. The other two sheets are where the data is stored that I referenced in the CHART.
I like to know why are rows hidden in Sheet "Chart"
 
Upvote 0
I have formulas I need to run seperately.

For example for ‘GWRRP Income’ the first column formula tells me how much income to withdrawal. The next column is a IF formula saying IF that value < 0, THEN return 0. The second column is my actual value I use in the CHART. The other instance of a hidden column would be for ‘Age’. For example I ran the first formula (D.O.B. - current date). The next column also ‘Age’ with a IF formula. IF > 90, return 0. If there is any other column hidden I would be happy to explain. Thanks again
 
Upvote 0
Please go ahead and set up a new test sheet named "Chart" in your file. Leave the headers as they are, please do not change the format, but clear everything below the heading, and see how the data fills in. Please note: (unhide rows if any, not columns)
Please let me know, thanks.

1743011294662.png

VBA Code:
Sub a()
    Dim s As Worksheet: Set s = Sheets("Chart")
    Dim r&, a1#, a2#

    r = 13

    Do
        With s
            If r = 13 Then
                .Cells(r, 2).Formula = "=IF(Inputs!E9>Inputs!E10,Inputs!E10,Inputs!E9)"
            Else
                .Cells(r, 2).Formula = "=DATE(YEAR(B" & r - 1 & ")+1,MONTH(B" & r - 1 & "),DAY(B" & r - 1 & "))"
            End If

            .Cells(r, 3).Formula = "=YEAR(B" & r & ")"
            .Cells(r, 4).Formula = "=INT((B" & r & "-Inputs!$D$9)/365.25)"
            .Cells(r, 5).Formula = "=IF(D" & r & ">Inputs!$F$15,0,D" & r & ")"
            .Cells(r, 6).Formula = "=INT((B" & r & "-Inputs!$D$10)/365.25)"
            .Cells(r, 7).Formula = "=IF(F" & r & ">Inputs!$F$20,0,F" & r & ")"
        End With

        Application.Calculate
        a1 = s.Cells(r, 4).Value: a2 = s.Cells(r, 6).Value

        If a1 > 90 Then s.Cells(r, 4).Font.Color = vbRed
        If a2 > 90 Then s.Cells(r, 6).Font.Color = vbRed

        If a1 >= 90 And a2 >= 90 Then Exit Do

        Call z(s, r)
        r = r + 1
    Loop

    Call z(s, r)
    
End Sub

Sub z(s As Worksheet, r&)
    With s
        .Cells(r, 8).Formula = "=Math!AH" & r - 4: _
        .Cells(r, 9).Formula = "=Math!AL" & r - 4: _
        .Cells(r, 10).Formula = "=Math!AC" & r - 4: _
        .Cells(r, 12).Formula = "=IF(K" & r & "<0,0,K" & r & ")": _
        .Cells(r, 13).Formula = "=IF(L" & r & "=0,AU" & r & ",L" & r & "+AU" & r & ")": _
        .Cells(r, 14).Formula = "=-FV(O" & r & "/12,12,-L" & r & "/12,N" & r - 1 & ",0)": _
        .Cells(r, 15).Formula = "=$N$8": _
        .Cells(r, 16).Formula = "=-FV(Q" & r & "/12,12,0,P" & r - 1 & ",0)": _
        .Cells(r, 17).Formula = "=$P$7": _
        .Cells(r, 18).Formula = "=-FV(S" & r & "/12,12,0,R" & r - 1 & ",0)": _
        .Cells(r, 19).Formula = "=$R$7": _
        .Cells(r, 20).Formula = "=-FV(U" & r & "/12,12,0,T" & r - 1 & ",1)": _
        .Cells(r, 21).Formula = "=$T$7": _
        .Cells(r, 22).Formula = "=-FV($W$13/12,12,0,V" & r - 1 & ",0)": _
        .Cells(r, 23).Formula = "=$V$7": _
        .Cells(r, 24).Formula = "=-FV($Y$13/12,12,0,X" & r - 1 & ",0)": _
        .Cells(r, 25).Formula = "=$X$7"
    End With
End Sub
 
Upvote 0
Thank you so much Sam!!!! It looks great & works so well! Two things that I ran into are below:

1. Under the 'Segment' Columns (N,P,R,T,V,X) the FV formula w. withdrawals (illustrated by light green rectangles) continues into the white rectangles underneath even after the withdrawal period is over. The withdrawal period length is set by the value in the yellow boxes above. When I change the value in the yellow cells it does not seem to change the amount of green withdrawal rectangles underneath. To further explain, the yellow values will tell the VBA how many green boxes (w. the FV formula that takes withdrawals from 'GWRRP Income') there will be in that particular segment.

Ex. Seg 1 yellow box -> "5" ( five green boxes w. withdrawal FV formula) / Seg 2 box: "7" (seven green boxes w. withdrawal FV formula) / etc. Each period length would be variable to what value is chosen.

**After the withdrawal period, the rows after will have no formula / stop.


light green rectangle's FORMULA (taking withdrawal) --> =-fv( O13/12<-(growth rates in tiny boxes to right),12,-L13/12<-(w.drawal),N9<-(prev. yr value),0) <--example; real formulas are attached in sheet above
white rectangle's FORMULA (no withdrawal, only growth) --> =-fv(Q13/12,12,0,P9,0) <--example

the white rectangle's symbolize the time before we start the withdrawal period, hence no income being taken above. The length of these would start from the beginning (row 13) - previous columns last green rectangle.

I will attach the image below to help visualize.


2. Lastly, I unhid my rows because under each year there are two more rows (1. 'rmd' & 2. tax). These formulas are just pulling in other values.

Is there a way that the vba code can run in this order going down the rows?

1. 2025: Age, Total Income, Annual Taxes, etc (row 13) <--currently what we have
2. 2025 rmd <--add (row 14)
3. 2025 tax <--add (row 15)
4. 2026: Age, Total Income, Annual Taxes, etc (row 16) <--currently what we have
5. 2026 rmd <-- add (row 17)
6. 2026 tax: <-- add (row 18)

7. continue pattern to age 90!

**I will share the mini sheet to show formulas of RMD and tax (they are in red)


It is so so close! Please let me know if I could help further or if you need me to send the mini sheet again. I am beyond impressed with what you have done so far. I'd seriously want to get you a free dinner or something for all the work you've been doing haha !
 

Attachments

  • SEND SAM FINAL.png
    SEND SAM FINAL.png
    176.8 KB · Views: 5
Upvote 0
Cell Formulas
RangeFormula
B13B13=IF(INPUTS!E9>INPUTS!E10,INPUTS!E10,INPUTS!E9)
D13,D28:D29,D25,D22,D19,D16D13=INT((B13-INPUTS!$D$9)/365.25)
E13,E28:E29,E25,E22,E19,E16E13=IF(D13>INPUTS!$F$15,0,'CHART (real)'!D13)
F13,F28:F29,F25,F22,F19,F16F13=INT((B13-INPUTS!$D$10)/365.25)
G13,G28:G29,G25,G22,G19,G16G13=IF(F13>INPUTS!$F$20,0,'CHART (real)'!F13)
H13H13=MATH!AH9
I13I13=MATH!AL9
J13J13=MATH!AC9
K13,K29,K25,K22,K19,K16K13=(H13+I13)-J13
M13,M29,M22,M19,M16M13=IF(L13=0,AU13,L13+AU13)
K14,K23,K26,K20,K17K14=AU13+AV13
L14,L23:L24,L26:L27,L20:L21,L17:L18L14=K14
K15:L15,K24,K27,K21K15=AW13
B16,B22,B19B16=EDATE(B13,12)
H16H16=MATH!AH10
I16I16=MATH!AL10
J16J16=MATH!AC10
L16,L28:L29,L25,L22,L19L16=IF(K16<0,0,K16)
K18K18=AW15
H19H19=MATH!AH11
I19I19=MATH!AL11
J19J19=MATH!AC11
H22H22=MATH!AH12
I22I22=MATH!AL12
J22J22=MATH!AC12
H25H25=MATH!AH13
I25I25=MATH!AL13
J25J25=MATH!AC13
B28B28=EDATE(B22,12)
H28:J28H28=H25
K28K28=(H28+I25)-J28
M28M28=IF(L28=0,AU25,L28+AU25)
B29B29=EDATE(B28,12)
H29H29=MATH!AH14
I29I29=MATH!AL14
J29J29=MATH!AC14
 
Upvote 0

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