Macro to insert number of rows based on criteria

mtampa

Board Regular
Joined
Oct 15, 2007
Messages
61
Hi Guys,

I have a workbook that is about 20,000 rows deep. I am looking for a macro that inserts the correct number of rows based on the number of units sold so I can save space and have only the correct of rows for my countries of sale.

For example:

Date Capacity Sold
30-Oct 20 5
31-Oct 20 8

Since I have 5 sold units on 30-Oct, I want the macro to insert 5 rows after that date and 8 after 31-Oct.

I have been searching around for an idea and will continue searching, but if anyone has any ideas, please let me know. I can also email someone a sample dataset if they need.

Thanks,
-Mike
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
If sold in column C try:
Code:
Sub test()
On Error Resume Next
For r = Cells(Rows.Count, "C").End(xlUp).Row To 2 Step -1
  For rw = 2 To Cells(r, "C").Value + 1
    Cells(r + 1, "C").EntireRow.Insert
Next rw, r
End Sub
 
Upvote 0
Here's an Example - Create a New workbook and post all this in there, including the code below..

Here's your workbook BEFORE the New Days Info in Cells B2:D2
Excel Workbook
ABCDE
1DateCapacitySold
2New record30-Oct205<< Enter And then Run Macro - InsertNewRecords
3
4
5DateCapacity
629-Oct15
729-Oct15
829-Oct15
928-Oct18
1028-Oct18
1128-Oct18
1228-Oct18
Sheet1
Excel 2007

Paste this Macro into a standard module

Code:
Sub InsertNewRecords()
NumSold = Range("D2").Value
Range("A6").Select
ActiveCell.Rows("1:" & NumSold).EntireRow.Select
    Selection.Insert Shift:=xlDown
    ActiveCell.Select
Range("A6").Resize(NumSold, 2).Value = Range("B2:C2").Value
End Sub

Now, Run it!!

Your Workbook should look like this...
Excel Workbook
ABCDE
1DateCapacitySold
2New record<< Enter And then Run Macro - InsertNewRecords
3
4
5DateCapacity
630-Oct20
730-Oct20
830-Oct20
930-Oct20
1030-Oct20
1129-Oct15
1229-Oct15
1329-Oct15
1428-Oct18
1528-Oct18
1628-Oct18
1728-Oct18
Sheet1
Excel 2007
 
Upvote 0
Another idea for you.
Code:
Sub insertrow()
Dim e As Range
Set e = Range("C2")
Do
Range(e(2), e(2)(e.Value)).EntireRow.Insert
Set e = e.End(4)
Loop Until e.Row = Rows.Count
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,473
Members
452,915
Latest member
hannnahheileen

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