VBA - Insert Row based on Cell Value

smitpau

Board Regular
Joined
Apr 2, 2020
Messages
167
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'd like to be able to create VBA which will insert a row based on a cell reference (for that row number) and will use spacing to insert the same row at say a spacing of 5.

Basically the issue is if a line is added to say a P&L (in this case Overheads), then for n number of companies each of there P&L's need a new line inserted so believe a VBA should be able to make this quicker.

Hopefully the example below is clear, I'm not sure if a Row Limit cell would have to be required so the spacing doesn't just continue to the end of the sheet which wouldn't be required.

Thanks for reading!

Book2
ABCDEFG
1VBA Inputs
2RowSpacing
395
4
5P&L ListBeforeAfter
6Revenue 1Company 11Company 1
7Cost of Sales2Revenue 2Revenue
8Overheads3Cost of Sales3Cost of Sales
9Profit 4Profit 4Overheads
1055Profit
116Company 26
127Revenue 7Company 2
138Cost of Sales8Revenue
149Profit 9Cost of Sales
1510Overheads
1611Profit
Sheet1
Cell Formulas
RangeFormula
E3E3=C11-C6
D7:D8D7=A6
D9D9=A9
D12:D14D12=D7
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi smitpau ... How about this ?

VBA Code:
Sub InsertRows()

Dim RowStart As Integer, Spacing As Integer, lRow As Long
RowStart = [D3]
Spacing = [E3]
lRow = Range("D" & Rows.Count).End(xlUp).Row 'Change column D to the column where your data is located

For x = lRow To RowStart Step -Spacing
    Rows(x).Insert
Next

End Sub
 
Upvote 0
Thanks mse330, when I try running it the following message comes up.

1589630012409.png
 
Upvote 0
Do you have other macro in Module 1 called Rows ?
 
Upvote 0
Haha sorry yes good spot, works well two things and then it should be perfect, one is that I believe for some reason it adds three to the inserted rows number input (pretty minor thing maybe able to fix) final and more important thing would it be possible to copy the row directly above to the newly insert rows below, so it keeps the flows of the formulas and formatting.

Appreciate the help.
 
Upvote 0
it adds three to the inserted rows number input (pretty minor thing maybe able to fix)

So once the new row is inserted it has a value of "3" ? :unsure:

Anyway, try the below which copies the above line

VBA Code:
Sub InsertRows()

Dim RowStart As Integer, Spacing As Integer, lRow As Long
RowStart = [D3]
Spacing = [E3]
lRow = Range("D" & Rows.Count).End(xlUp).Row 'Change column D to the column where your data is located

For x = lRow To RowStart Step -Spacing
    Rows(x).Insert
    Rows(x - 1).Copy Cells(x, 1)
Next

End Sub
 
Upvote 0
Okay that's getting quite close now.

For the three bit added that was wrong sorry, what happens for some reason is that it will insert in row 10, then the spacing works well so the other row is 17 (10+6+1). If I change the row input to either 7 or 8 it will still start in row 10, not sure why.

Example below showing what happens using 8 as the row input.


Line spacer macro.xlsm
ABCDEFG
1VBA Inputs
2RowSpacing
3P&L List86
4Revenue
5Cost of Sales
6Overheads1Company 1
7Profit2Revenue
83Cost of Sales
94Overheads
104Profit
115Profit
126
137Company 2
148Revenue
159Cost of Sales
1610Overheads
1710Profit
1810Profit
Sheet1
Cell Formulas
RangeFormula
E3E3=F13-F6
G7:G10G7=A4
G11G11=A7
G14:G16,G18G14=G7
G17G17=G11
 
Upvote 0
This seems to solve the row start bit actually, you've already done the hard part to be fair.

For x = RowStart To lRow Step Spacing
 
Upvote 0
This really is the final thing now not the end of the world if it can't be done.

For some reason the copy is like an exact copy with the same formula rather than a relative copy so would reference in the same order, so instead of two Profits as above it would reference A7 then A8.
 
Upvote 0
How about the revised code below

VBA Code:
Sub InsertRows()

Dim RowStart As Integer, Spacing As Integer, lRow As Long
RowStart = [D3]
Spacing = [E3]
lRow = Range("G" & Rows.Count).End(xlUp).Row 'Change column D to the column where your data is located

x = RowStart
Do While x <= lRow
    Rows(x).Insert
    Rows(x - 1).Copy Cells(x, 1)
    x = x + Spacing
    lRow = lRow + 1
Loop

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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