Auto add/delete rows based on specific cell value.

Sean15

Well-known Member
Joined
Jun 25, 2005
Messages
719
Office Version
  1. 2010
Platform
  1. Windows
Hi:
I want to add rows to table below based on valued in G2. So, if G2 has 61, table should have 61 rows starting from E10, if 41 in G2, table should have 41 rows, etc.

I also want to keep the subtotals as shown in F61 and G61 but subtotal should adjust up/down based on value in G2. I have searched online but can’t find codes that does that. Also, I read that AI tools have just as much VB skills as I do so I better ask the professionals.

Can someone help?

PV of lease pymts.xlsm
EFG
1
2Number of Periods51
3
4
5
6Annual Rate9.00%
7
8
9PeriodCashPresent Value
101$237,651.19$235,882.07
112$237,651.19$234,126.12
123$237,651.19$232,383.25
134$237,651.19$230,653.35
145$237,651.19$228,936.33
156$237,651.19$227,232.09
167$237,651.19$225,540.53
178$237,651.19$223,861.57
189$244,780.72$228,860.96
1910$244,780.72$227,157.28
2011$244,780.72$225,466.28
2112$244,780.72$223,787.87
2213$244,780.72$222,121.96
2314$244,780.72$220,468.45
2415$244,780.72$218,827.24
2516$244,780.72$217,198.26
2617$244,780.72$215,581.39
2718$244,780.72$213,976.57
2819$244,780.72$212,383.69
2920$244,780.72$210,802.67
3021$252,124.14$215,510.42
3122$252,124.14$213,906.13
3223$252,124.14$212,313.78
3324$252,124.14$210,733.28
3425$252,124.14$209,164.54
3526$252,124.14$207,607.49
3627$252,124.14$206,062.02
3728$252,124.14$204,528.06
3829$252,124.14$203,005.52
3930$252,124.14$201,494.31
4031$252,124.14$199,994.35
4132$252,124.14$198,505.56
4233$259,687.87$202,938.69
4334$259,687.87$201,427.98
4435$259,687.87$199,928.52
4536$259,687.87$198,440.21
4637$259,687.87$196,962.99
4738$259,687.87$195,496.77
4839$259,687.87$194,041.45
4940$259,687.87$192,596.98
5041$259,687.87$191,163.25
5142$259,687.87$189,740.20
5243$259,687.87$188,327.74
5344$259,687.87$186,925.80
5445$267,478.50$191,100.32
5546$267,478.50$189,677.74
5647$267,478.50$188,265.74
5748$267,478.50$186,864.26
5849$267,478.50$185,473.21
5950$267,478.50$184,092.52
6051$267,478.50$182,722.10
61Total$12,852,671.76$10,604,259.89
PV_Calc
Cell Formulas
RangeFormula
E11:E59E11=IF($G$2>E10,E10+1,"")
E60E60=IF($G$2>=E59,E59+1,"")
G10:G60G10=IF(OR(E10="",F10=""),"",PV($G$6/12,E10,0,-F10,0))
F61F61=SUBTOTAL(109,[Cash])
G61G61=SUBTOTAL(109,[Present Value])
Cells with Data Validation
CellAllowCriteria
G2Whole numberbetween 1 and 1200
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi:
Sorry to get back to you on this.
I wanted to adjust code in post #4, so it won't do any calculation or add number sequence in column E.
I delete line:

VBA Code:
.DataBodyRange.Cells(1, 1).AutoFill .DataBodyRange.Columns(1), xlFillSeries
but code is still adding 1 in column E. What else do I have to delete so code in post #4 won't do any calculation or add number in column E.
 
Upvote 0
but code is still adding 1 in column E. What else do I have to delete so code in post #4 won't do any calculation or add number in column E.
What is the initial value in E10?
Do you have formula in E11 downward (as shown in your original example)? And do you want to keep that formula?
 
Upvote 0
Hi:

I changed formula slight. Please see below.

PV of lease pymts-Updated VBA codes - Copy.xlsm
E
11Period
120
131
142
153
PV_Calc
Cell Formulas
RangeFormula
E12E12=IF($G$8="Beginning",0,1)
E13:E15E13=E12+1


I thought I could just add formula in E12 (row changed because I added two new rows to worksheet), then E13 downwards but there is something in your code that replaces the formulas I just added.
 
Upvote 0
Hi:
I thought I could just add formula in E12 (row changed because I added two new rows to worksheet), then E13 downwards but there is something in your code that replaces the formulas I just added.
Replace this part:
VBA Code:
                .DataBodyRange.Cells(2, 1).AutoFill .DataBodyRange.Columns(1), xlFillSeries
with this:
VBA Code:
               .DataBodyRange.Cells(2, 1).Copy .DataBodyRange.Cells(2, 1).Resize(n - 1)
 
Upvote 0
Thank you.
Your help is greatly appreciated.

Sean
 
Upvote 0
Try this one, it is faster than the previous one:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$G$2" Then
   
    Dim n As Long, i As Long, h As Long
    Dim tb As ListObject
    Dim c As Range
    
    n = Target.Value
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlManual
    Set tb = ActiveSheet.ListObjects(1)
    With tb
           h = .DataBodyRange.Rows.Count
            If h < n Then  'add rows
                Set c = .DataBodyRange.Cells(1).Offset(h).Resize(n - h, 3)
                c.Insert shift:=xlShiftDown
                .Resize .Range.Cells(1).Resize(n + 1, 3)
                .DataBodyRange.Cells(2, 1).Copy .DataBodyRange.Cells(2, 1).Resize(n - 1)
            ElseIf h > n Then  'delete rows
               .Resize .Range.Cells(1).Resize(n + 1, 3)
               .DataBodyRange.Cells(1).Offset(n).Resize(h - n, 3).Delete shift:=xlShiftUp
            End If
    End With

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlAutomatic
    
End If

Exit Sub
skip:
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlAutomatic
MsgBox "Error number " & Err.Number & " : " & Err.Description
End Sub
 
Upvote 0
Hi:

Code in post #17 is really fast.
But something is off with column E.

Here is schedule when user enter 2 in G2. It's perfect.

PV of lease pymts-Updated VBA codes - V3.xlsm
DEFGH
2Number of Periods2
3
4Periods in Months
5
6Annual Rate9.00%
7
8Payment made atEndof period
9
10
11PeriodCashPresent Value
121$237,651.19$235,882.07
132$237,651.19$234,126.12
14
PV_Calc
Cell Formulas
RangeFormula
E12E12=IF($G$8="Beginning",0,1)
E13E13=E12+1
G12:G13G12=PV(IF($G$4="Months",$G$6/12,IF($G$4="Quarters",$G$6/4,IF($G$4="bi-annual",$G$6/2,$G$6))),E12,0,-F12,IF($G$8="Beginning",0,1))
Cells with Data Validation
CellAllowCriteria
G2Whole numberbetween 1 and 1200
G8List=Sheet1!$A$1:$A$2
G4List=Sheet1!$A$4:$A$7


But if user enter 10 in G2, schedule is:

PV of lease pymts-Updated VBA codes - V3.xlsm
EFGH
2Number of Periods10
3
4Periods in Months
5
6Annual Rate9.00%
7
8Payment made atEndof period
9
10
11PeriodCashPresent Value
12#VALUE!$237,651.19#VALUE!
13#VALUE!$237,651.19#VALUE!
14#VALUE!$237,651.19#VALUE!
15#VALUE!$237,651.19#VALUE!
16#VALUE!$237,651.19#VALUE!
17#VALUE!$237,651.19#VALUE!
18#VALUE!$237,651.19#VALUE!
19#VALUE!$237,651.19#VALUE!
20#VALUE!$244,780.72#VALUE!
21#VALUE!$244,780.72#VALUE!
PV_Calc
Cell Formulas
RangeFormula
E12:E21E12=E11+1
G12:G21G12=PV(IF($G$4="Months",$G$6/12,IF($G$4="Quarters",$G$6/4,IF($G$4="bi-annual",$G$6/2,$G$6))),E12,0,-F12,IF($G$8="Beginning",0,1))
Cells with Data Validation
CellAllowCriteria
G2Whole numberbetween 1 and 1200
G8List=Sheet1!$A$1:$A$2
G4List=Sheet1!$A$4:$A$7

The formula in E12 is off. It should be: IF($G$8="Beginning",0,1) as shown in 1st screenshot of post #18. Formula in E12 must stay as is, it should not be changed or copied. Something in the VBA code is replacing formula.
Formula in E13 is E12+1
E14 is E13+1
E15 is E14+1 etc.,

I believe that's the confusion.
I sincerely apologise.
 
Upvote 0
The formula in E12 is off. It should be: IF($G$8="Beginning",0,1) as shown in 1st screenshot of post #18. Formula in E12 must stay as is, it should not be changed or copied. Something in the VBA code is replacing formula.
I can't reproduce the error, I entered any number in G2 & formula in E12 stayed the same.
Did you sort the table before running the code?
Could you please upload a sample workbook (without sensitive data) to a file-sharing site like Dropbox.com or Google Drive, and then share the link here? Also, ensure that the link is accessible to anyone.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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