VBA for inserting rows

Access Beginner

Active Member
Joined
Nov 8, 2010
Messages
311
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I have a data set which in Column C has a list of postcodes, there are around 2,600 unique postcodes in this data set.

I was hoping that a macro could be written that would insert 3 rows after each postcode and then in Column D, put the following values CDAP, CRS and Total.

Data as it is now:

Column C
800
810
812
2486 etc

Desired Result:

Column C....... Column D
800 ...............CDAP
.....................CRS
.....................Total
810 ................CDAP
......................CRS
......................Total

And so on until the last postcode is down. Last row in the data set is 2603.

Please ignore the ............, this is the only why I could get it to align

Sorry but I am at work and cannot use any of the tools to paste the actual worksheet.

Any help is greatly appreciated

Using Excel 2007
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Not sure if I got the placement correct but make a copy of the worksheet, put the CRS etc values into cells A1:A3 of Sheet3 (an otherwise blank sheet) and try this out:

Code:
Sub InsertRows()
    Dim rwLast As Long, _
        Rw As Long
    
    rwLast = Range("C" & Rows.Count).End(xlUp).Row
    
    For Rw = rwLast To 2 Step -1
        Cells(Rw, 3).Resize(3, 1).EntireRow.Insert
        Cells(Rw, 4).Resize(3, 1) = Sheets("Sheet3").Range("A1:A3").Value
    Next Rw
End Sub

Denis
 
Upvote 0
This code should do what you want...
Code:
Sub InsertRowsAndData()
  Dim X As Long, LastRow As Long
  Const StartRow As Long = 2
  Const RowsToInsert As Long = 2
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  For X = LastRow To StartRow Step -1
    Rows(X + 1).Resize(RowsToInsert).Insert
    Cells(X, "D").Resize(RowsToInsert + 1).Value = WorksheetFunction.Transpose(Array("CDAP", "CRS", "Total"))
  Next
End Sub
NOTE 1: Your description said you wanted to insert 3 rows, but actually, you are only inserting 2 (new) rows.

Note 2: There are two constants (the Const) statements that you should check the values being assigned. The first is the starting row for your data... I assumed Row 2. The second is the number of new rows to be inserted which, as Note 1 indicates, is set to 2.
 
Last edited:
Upvote 0
Here's another option that places the first item on the same row as the value in column C:

Code:
Sub InsertRows()
    Dim rwLast As Long, _
        Rw As Long
    
    rwLast = Range("C" & Rows.Count).End(xlUp).Row
    
    For Rw = rwLast To 2 Step -1
        Cells(Rw, 3).Resize(2, 1).EntireRow.Insert
        Cells(Rw + 2, 4).Resize(3, 1) = Sheets("Sheet3").Range("A1:A3").Value
    Next Rw
    Range("C2:C3").EntireRow.Delete
End Sub

Denis
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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