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

jakeecirg

New Member
Joined
Mar 24, 2025
Messages
26
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: 13
Bucket Solver - Copy.xlsm
AUAVAW
11Excess rmdExcess rmd 2Tax
12
13$ -$ -$ -
14
15
16$ -$ -$ -
17
18
19$ -$ -$ -
20
21
22$ -$ -$ -
23
24
25$ 30,713.02$ -$ 3,071.30
26
27
28
29$ 40,162.66$ -$ 4,016.27
30
31
32$ 55,346.60$ -$ 5,534.66
33
34
35$ 77,430.23$ -$ 7,743.02
36
37
38$110,098.25$ -$ 11,009.82
39
40
41#DIV/0!#DIV/0!#DIV/0!
42
43
44
45$ -$ -$ -
46
47
48$ -$ -$ -
49
50
51$ -$ -$ -
52
53
54$ -$ -$ -
55
56
57$ -$ -$ -
58
59
60
61$ -$ -$ -
62
63
64$ -$ -$ -
65
66
67$ -$ -$ -
68
69
70$ -$ -$ -
71
72
73$ -$ -$ -
74
75
76
77$ -$ -$ -
78
79
80$ -$ -$ -
81
82
83$ -$ -$ -
84
85
86$ -$ -$ -
87
88
89$ -$ -$ -
90
91
92
CHART
Cell Formulas
RangeFormula
AU13,AU89,AU86,AU83,AU80,AU77,AU73,AU70,AU67,AU64,AU61,AU57,AU54,AU51,AU48,AU45,AU38,AU35,AU32,AU29,AU25,AU22,AU19,AU16AU13=IF(D13>=MATH!$AU$5,IF(AS13>L13,AS13-L13,0),0)
AV13,AV89,AV86,AV83,AV80,AV77,AV73,AV70,AV67,AV64,AV61,AV57,AV54,AV51,AV48,AV45,AV41,AV38,AV35,AV32,AV29,AV22,AV19,AV16AV13=IF(F13>=MATH!$AU$5,IF(AT13>$L13,AT13-$L13,0),0)
AW13,AW89,AW86,AW83,AW80,AW77,AW73,AW70,AW67,AW64,AW61,AW57,AW54,AW51,AW48,AW45,AW41,AW38,AW35,AW32,AW29,AW25,AW22,AW19,AW16AW13=(AU13+AV13)*INPUTS!$H$36
AV25AV25=IF(F25>=MATH!$AU$5,IF(AT25>L25,AT25-L25,0),0)
AU41AU41=IF(D41>MATH!$AU$5,IF(AS41>L41,AS41-L41,0),0)
 
Upvote 0
The numbers are off, but that's an issue on my end I can fix. The formulas are above for all the rows. Hope this helps!
 
Upvote 0
Cell Formulas
RangeFormula
B13B13=IF(INPUTS!E9>INPUTS!E10,INPUTS!E10,INPUTS!E9)
D13,D28,D25,D22,D19,D16D13=INT((B13-INPUTS!$D$9)/365.25)
E13,E28,E25,E22,E19,E16E13=IF(D13>INPUTS!$F$15,0,'CHART (2)'!D13)
F13,F28,F25,F22,F19,F16F13=INT((B13-INPUTS!$D$10)/365.25)
G13,G28,G25,G22,G19,G16G13=IF(F13>INPUTS!$F$20,0,'CHART (2)'!F13)
H13H13=MATH!AH9
I13I13=MATH!AL9
J13J13=MATH!AC9
K13,K25,K22,K19,K16K13=(H13+I13)-J13
L13,L28,L25,L22,L19,L16L13=IF(K13<0,0,K13)
M13,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
N13N13=-FV(O13/12,12,-L13/12,N9,0)
N14,N26,N23,N20,N17N14=N13-L14-L15
B16,B22,B19B16=EDATE(B13,12)
H16H16=MATH!AH10
I16I16=MATH!AL10
J16J16=MATH!AC10
K18K18=AW15
N16,N25,N22,N19N16=-FV(O16/12,12,-L16/12,N14,0)
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
C13:C28C13=YEAR(B13)
O13:O28O13=$N$8
B28B28=EDATE(B22,12)
H28:J28H28=H25
K28K28=(H28+I25)-J28
M28M28=IF(L28=0,AU25,L28+AU25)
N28N28=N26
 
Upvote 0
I map them underneath the green withdrawal box for that year and subtract the rmd's and taxes from the same year value. I put bold red font on where they are mapped. Thanks Sam.
 
Upvote 0
@jakeecirg Please test this code and see what is happening on your workfile. Yellow box and Mapping "RMD"& "Tax" after this.
VBA Code:
Sub x()
    Dim a As Worksheet: Set a = ThisWorkbook.Sheets("Chart")
    Dim b&, c&, d#, e#, f#, g#, h As Boolean, i As Variant, j As Variant, k&, l As Variant
    b = 13
    c = a.Cells(a.Rows.Count, "B").End(xlUp).Row
    If c >= 13 Then
        With a.Range(a.Cells(13, 2), a.Cells(c, 25))
            .ClearContents
            .Interior.ColorIndex = xlNone
        End With
    End If
    i = Array(14, 16, 18, 20, 22, 24)
    
    Do
        If b = 13 Then
            a.Cells(b, 2).Formula = "=IF(Inputs!E9>Inputs!E10,Inputs!E10,Inputs!E9)"
        Else
            j = b - 1
            Do While j > 1
                If IsDate(a.Cells(j, 2).Value) Then Exit Do
                j = j - 1
            Loop
            a.Cells(b, 2).Formula = "=DATE(YEAR(B" & j & ")+1,MONTH(B" & j & "),DAY(B" & j & "))"
        End If
        
        a.Cells(b, 3).Formula = "=YEAR(B" & b & ")"
        a.Cells(b, 4).Formula = "=INT((B" & b & "-Inputs!$D$9)/365.25)"
        a.Cells(b, 5).Formula = "=IF(D" & b & ">Inputs!$F$15,0,D" & b & ")"
        a.Cells(b, 6).Formula = "=INT((B" & b & "-Inputs!$D$10)/365.25)"
        a.Cells(b, 7).Formula = "=IF(F" & b & ">Inputs!$F$20,0,F" & b & ")"
        
        Application.Calculate
        
        d = a.Cells(b, 4).Value
        e = a.Cells(b, 5).Value
        f = a.Cells(b, 6).Value
        g = a.Cells(b, 7).Value
        
        If IsNumeric(d) And d > 90 Then a.Cells(b, 4).Font.Color = vbRed
        If IsNumeric(e) And e > 90 Then a.Cells(b, 5).Font.Color = vbRed
        If IsNumeric(f) And f > 90 Then a.Cells(b, 6).Font.Color = vbRed
        If IsNumeric(g) And g > 90 Then a.Cells(b, 7).Font.Color = vbRed
        
        If IsNumeric(d) And IsNumeric(e) And IsNumeric(f) And IsNumeric(g) Then
            If (d >= 90 Or d = 0) And (e >= 90 Or e = 0) And (f >= 90 Or f = 0) And (g >= 90 Or g = 0) Then Exit Do
        End If
        
        h = False
        If (IsNumeric(d) And d >= 90) Or (IsNumeric(e) And e >= 90) Or (IsNumeric(f) And f >= 90) Or (IsNumeric(g) And g >= 90) Then h = True
        
        If Not h Then
            a.Cells(b, 8).Formula = "=Math!AH" & (b - 4)
            a.Cells(b, 9).Formula = "=Math!AL" & (b - 4)
            a.Cells(b, 10).Formula = "=Math!AC" & (b - 4)
            a.Cells(b, 12).Formula = "=IF(K" & b & "<0,0,K" & b & ")"
            a.Cells(b, 13).Formula = "=IF(L" & b & "=0,AU" & b & ",L" & b & "+AU" & b & ")"
        End If
        
        For Each l In i
            If b = 13 Then
                Select Case l
                    Case 14: a.Cells(b, l).Formula = "=-FV(O" & b & "/12,12,-L" & b & "/12,N" & b - 1 & ",0)"
                    Case 16: a.Cells(b, l).Formula = "=-FV(Q" & b & "/12,12,0,P" & b - 1 & ",0)"
                    Case 18: a.Cells(b, l).Formula = "=-FV(S" & b & "/12,12,0,R" & b - 1 & ",0)"
                    Case 20: a.Cells(b, l).Formula = "=-FV(U" & b & "/12,12,0,T" & b - 1 & ",1)"
                    Case 22: a.Cells(b, l).Formula = "=-FV($W$13/12,12,0,V" & b - 1 & ",0)"
                    Case 24: a.Cells(b, l).Formula = "=-FV($Y$13/12,12,0,X" & b - 1 & ",0)"
                End Select
            ElseIf a.Cells(b - 3, l).HasFormula Then
                If IsNumeric(a.Cells(b - 3, l).Value) Then
                    If a.Cells(b - 3, l).Value <= 0 Then
                        a.Cells(b, l).ClearContents
                    Else
                        Select Case l
                            Case 14: a.Cells(b, l).Formula = "=-FV(O" & b & "/12,12,-L" & b & "/12,N" & b - 3 & ",0)"
                            Case 16: a.Cells(b, l).Formula = "=-FV(Q" & b & "/12,12,0,P" & b - 3 & ",0)"
                            Case 18: a.Cells(b, l).Formula = "=-FV(S" & b & "/12,12,0,R" & b - 3 & ",0)"
                            Case 20: a.Cells(b, l).Formula = "=-FV(U" & b & "/12,12,0,T" & b - 3 & ",1)"
                            Case 22: a.Cells(b, l).Formula = "=-FV($W$13/12,12,0,V" & b - 3 & ",0)"
                            Case 24: a.Cells(b, l).Formula = "=-FV($Y$13/12,12,0,X" & b - 3 & ",0)"
                        End Select
                    End If
                End If
            End If
        Next l
        
        a.Cells(b, 15).Formula = "=$N$8"
        a.Cells(b, 17).Formula = "=$P$7"
        a.Cells(b, 19).Formula = "=$R$7"
        a.Cells(b, 21).Formula = "=$T$7"
        a.Cells(b, 23).Formula = "=$V$7"
        a.Cells(b, 25).Formula = "=$X$7"
        
        If d < 90 And e < 90 And f < 90 And g < 90 Then
            k = b + 1
            a.Rows(k).Insert
            a.Cells(k, 2).Value = a.Cells(b, 3).Value & " RMD"
            a.Cells(k, 3).Value = "'[PLACEHOLDER] RMD Formula"
            a.Range(a.Cells(k, 2), a.Cells(k, 25)).Interior.Color = RGB(220, 220, 220)
            
            k = k + 1
            a.Rows(k).Insert
            a.Cells(k, 2).Value = a.Cells(b, 3).Value & " Tax"
            a.Cells(k, 3).Value = "'[PLACEHOLDER] Tax Formula"
            a.Range(a.Cells(k, 2), a.Cells(k, 25)).Interior.Color = RGB(220, 220, 220)
            k = k + 1
            b = b + 3
        Else
            b = b + 1
        End If
    Loop
    
End Sub
 
Upvote 0
Thank you Sam! Paste this after the code previously shared or start fresh with this code?
 
Upvote 0
Bucket Solver - Copy.xlsm
BCDEFGHIJKLMNOPQRSTUVWXY
12Plan Year (End Date)Plan Year (End Date)AgeTotal Income Annual TaxesIncome Floor GWRRP Income GWRRP Income GWRRP Income $ 60,284.08$ 50,444.90$ 61,091.62$ 50,374.29$ 33,066.33$ 60,738.79
131/1/2026202663636262$51,000$3,192$34,080$-$-$61,5012%$51,4632%$63,5814%$53,4816%$35,8118%$65,7808%
142026 RMD[PLACEHOLDER] RMD Formula**$ above - RMD - Tax
152026 Tax[PLACEHOLDER] Tax Formula
161/1/2027202764646363$44,571$2,432$36,166$-$-2%2%4%6%8%8%
172027 RMD[PLACEHOLDER] RMD Formula**$ above - RMD - Tax
182027 Tax[PLACEHOLDER] Tax Formula
191/1/2028202865656464$47,299$2,581$38,380$-$-2%2%4%6%8%8%
202028 RMD[PLACEHOLDER] RMD Formula**$ above - RMD - Tax
212028 Tax[PLACEHOLDER] Tax Formula
221/1/2029202966666565$50,194$5,019$-$-$-2%2%4%6%8%8%
232029 RMD[PLACEHOLDER] RMD Formula**$ above - RMD - Tax
242029 Tax[PLACEHOLDER] Tax Formula
251/1/2030203067676666$53,266$5,327$-$-$-2%2%4%6%8%8%
262030 RMD[PLACEHOLDER] RMD Formula**$ above - RMD - Tax
272030 Tax[PLACEHOLDER] Tax Formula
CHART
Cell Formulas
RangeFormula
N12,X12,V12,T12,R12,P12N12=N9
B13B13=IF(INPUTS!E9>INPUTS!E10,INPUTS!E10,INPUTS!E9)
C13,C25,C22,C19,C16C13=YEAR(B13)
D13,D25,D22,D19,D16D13=INT((B13-INPUTS!$D$9)/365.25)
E13,E25,E22,E19,E16E13=IF(D13>INPUTS!$F$15,0,D13)
F13,F25,F22,F19,F16F13=INT((B13-INPUTS!$D$10)/365.25)
G13,G25,G22,G19,G16G13=IF(F13>INPUTS!$F$20,0,F13)
H13,H25,H22,H19,H16H13=MATH!AH9
I13,I25,I22,I19,I16I13=MATH!AL9
J13,J25,J22,J19,J16J13=MATH!AC9
L13,L25,L22,L19,L16L13=IF(K13<0,0,K13)
M13,M25,M22,M19,M16M13=IF(L13=0,AU13,L13+AU13)
N13N13=-FV(O13/12,12,-L13/12,N12,0)
O13,O25,O22,O19,O16O13=$N$8
P13,R13P13=-FV(Q13/12,12,0,P12,0)
Q13,Q25,Q22,Q19,Q16Q13=$P$7
S13,S25,S22,S19,S16S13=$R$7
T13T13=-FV(U13/12,12,0,T12,1)
U13,U25,U22,U19,U16U13=$T$7
V13V13=-FV($W$13/12,12,0,V12,0)
W13,W25,W22,W19,W16W13=$V$7
X13X13=-FV($Y$13/12,12,0,X12,0)
Y13,Y25,Y22,Y19,Y16Y13=$X$7
B16,B25,B22,B19B16=DATE(YEAR(B13)+1,MONTH(B13),DAY(B13))
 
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