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
 
Thanks that works well except the copy for some reason is not like a normal copy paste as it does not keep the flow (if that makes sense) it's like a cut paste instead.

Example below of what I mean.

Line spacer macro.xlsm
ABCDEFG
1P&L ListVBA Inputs
2RevenueRowSpacing
3Cost of Sales86
4Overheads
5Profit1Company 1
62Revenue
73Cost of Sales
83Overheads
94Overheads
105Profit
116
127Company 2
138Revenue
148Cost of Sales
159Cost of Sales
1610Overheads
1711Profit
Sheet1
Cell Formulas
RangeFormula
E3E3=F12-F5
G6:G8G6=A2
G9:G10G9=A4
G13:G14,G16:G17G13=G6
G15G15=G7
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I believe it may need some sort of Autofill tweak as the copy is correct it's just the cells below are then out of order.
 
Upvote 0
I'm not really sure if I understand your issue 100% but check the below to what I believe your issue is. I have added 1 more line in my code to highlight the inserted row in yellow to see things clearly. If you run the below code in the attached sample, it should insert mse330 before the word overheads correctly since I have already updated it in column A where the formulas are linked to. However, if you delete it from column A then re-run the code again, you'll notice that the new inserted row copied correctly from the above formula but the word overheads is still reading form the same cell, hence it appears to you as it is cut paste. Give it a try & let me know if my assumption is correct


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)
    Cells(x, "G").Interior.Color = vbYellow
    x = x + Spacing
    lRow = lRow + 1
Loop

End Sub

Book1
ABCDEFG
1P&L ListVBA Inputs
2RevenueRowSpacing
3Cost of Sales87
4mse330
5Overheads1Company 1
6Profit2Revenue
73Cost of Sales
84Overheads
95Profit
106
117Company 2
128Revenue
139Cost of Sales
1410Overheads
1511Profit
1612
1713Company 3
1814Revenue
1915Cost of Sales
2016Overheads
2117Profit
Sheet5
Cell Formulas
RangeFormula
G6:G7G6=A2
G8:G9G8=A5
G12:G15,G18:G21G12=G6
 
Upvote 0
This is what I get when I run it.

Yes you're right exactly as the rows below the inserted row are static so they don't realise to also reference the next cell (hence the duplicate references).

Book2
ABCDEFG
1P&L ListVBA Inputs
2RevenueRowSpacing
3Cost of Sales86
4mse330
5Overheads1Company 1
6Profit2Revenue
73Cost of Sales
83mse330
94Overheads
105Profit
116
127Company 2
138Revenue
148Cost of Sales
159Cost of Sales
1610Overheads
1711Profit
1812
1913Company 3
2013Company 3
2114Revenue
2215Cost of Sales
2316Overheads
2417Profit
Sheet2
Cell Formulas
RangeFormula
E3E3=F12-F5
G6:G10G6=A2
G13:G14,G16:G17,G22:G24G13=G6
G15,G21G15=G7
 
Upvote 0
Sorry my mistake that does work and that's very helpful, ideally I meant for the spacing to be 6 in this example not 7 if the code could be tweaked for that please.

Book2
ABCDEFG
1P&L ListVBA Inputs
2RevenueRowSpacing
3Cost of Sales87
4mse330
5Overheads1Company 1
6Profit2Revenue
73Cost of Sales
83mse330
94Overheads
105Profit
116
127Company 2
138Revenue
149Cost of Sales
159mse330
1610Overheads
1711Profit
1812
1913Company 3
2014Revenue
2115Cost of Sales
2215mse330
2316Overheads
2417Profit
Sheet2
Cell Formulas
RangeFormula
G6:G10G6=A2
G13:G17,G20:G24G13=G6
 
Upvote 0
That does everything I've asked for you've been a huge help (can just change Spacing to E3+1).

I can try to do this bit myself but thought I'd ask, say I would like to add multiple rows so D3 to say D24 were row inputs (not all of them would be necessarily used) would that be possible as part of the loop.
 
Upvote 0
I've made something like this, but it's an inelegant way of adding multiple row inputs.

VBA Code:
Sub InsertRows()

Dim RowStart As Integer, RowN As Integer, Spacing As Integer, lRow As Long
RowStart = [D3]
RowN = [D4]
Spacing = [E3+1]
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

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


End Sub
 
Upvote 0
That does everything I've asked for you've been a huge help (can just change Spacing to E3+1).

Glad I was able to help to get what you initially needed. For your last requirement, it would be tricky since you've already inserted rows the first round/loop …
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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