Insert new rows with increments from existing rows...

joeallenfm

New Member
Joined
Feb 10, 2023
Messages
5
Office Version
  1. 365
Platform
  1. MacOS
Hi MrExcel board, first time poster in need...

I have 600k rows of data like this;

CODEProductQuantityCustomerID
WMB0000025Medium Thing25x35904930
WMX0000240Small Thing25x49509600
WMB0000391Medium Thing25x39409510

I would like to run a script to insert rows after each existing row where:
  1. The number of rows inserted is taken from the quantity cell of the row triggering the insert minus 1.
  2. The Code of the inserted rows is the same as the code cell from the triggering row + 1 added the the numerical part of the code... e.g. the first two rows after the first example above would be WMB0000026 and WMB0000027.
  3. The Product and Customer ID are the same as the cells from the triggering row.
  4. The rows are inserted and do not overwrite any existing rows - so in example above the second row of data (WMX0000240) would still be there after the script had executed the first row of data's insert.
 
Last edited by a moderator:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Are your codes always like 3 characters [A-Z] and 7 characters [0-9]?

And what about the quantitiy row? Is it gonna stay the same also?
 
Upvote 0
Ah, sorry I should have used a real code.

They are always four characters [A-Z] and 9 charachters [0-9] Here's a real one:

RZMa000406717
 
Upvote 0
I hope it will be fast enough for 600k rows :)
VBA Code:
Sub test()
  Dim lRow As Long, r As Long
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
  Application.ScreenUpdating = False
  For i = lRow To 2 Step -1
    r = Cells(i, 3).Value
    Rows(i + 1).Resize(r).EntireRow.Insert
    With Range("A" & i & ":B" & i)
    .AutoFill Destination:=.Resize(r + 1)
    End With
    Range("C" & i + 1 & ":D" & i + 1).Resize(r).Value = Range("C" & i & ":D" & i).Value
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
That is amazing. Thank you. I just ran on a test range. The only issue was it added 25 rows (e.g. the quantity field) but I wanted 25 minus 1. I've tried to work out where the code needs updating...but failed miserably! If you could help with that, thanks again. :)
 
Last edited by a moderator:
Upvote 0
Hi Flasbond,

what about the number of rows available in an Excel sheet as 600K rows + 1200 new rows is a lot more than 2^20 rows available

Holger
 
Upvote 0
This should work :) You don't have to send me anything. Just send your best hopes for my country 🙏

VBA Code:
Sub test()
  Dim lRow As Long, r As Long
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
  Application.ScreenUpdating = False
  For i = lRow To 2 Step -1
    r = Cells(i, 3).Value - 1
    Rows(i + 1).Resize(r).EntireRow.Insert
    With Range("A" & i & ":B" & i)
    .AutoFill Destination:=.Resize(r + 1)
    End With
    Range("C" & i + 1 & ":D" & i + 1).Resize(r).Value = Range("C" & i & ":D" & i).Value
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hi Flasbond,

what about the number of rows available in an Excel sheet as 600K rows + 1200 new rows is a lot more than 2^20 rows available

Holger
That's a very good point. It will be at least 15.000.000 additional rows if the quantities are 25 in average.
@joeallenfm you are already close to the row limit.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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