Insert number of rows from specific starting row

drluke

Active Member
Joined
Apr 17, 2014
Messages
312
Office Version
  1. 365
Platform
  1. Windows
I'm attempting to insert a number of rows in a worksheet based on the number of data rows in another sheet, starting from the first empty row in my insert worksheet.
I can't find the right code to do it. Here is what I have right now
VBA Code:
Sub InsertRows()

    Dim ws As Worksheet, sh As Worksheet
    Dim strow As Long, nrow As Long
    Dim i As Long
    
    Set ws = ThisWorkbook.Worksheets("Test FAR")
    Set sh = ThisWorkbook.Worksheets("Additions")
    
    With ws
     strow = ws.Range("A2").End(xlDown).Row + 1 'finds row to start inserting from
      End With
    
    With sh
     nrow = [Counta(Additions!B:B)] - 1 'number of rows to insert
      End With

    For i = 1 To nrow
      ws.Range(strow & i).EntireRow.Insert
       Next i
    
End Sub
Any advice?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about:

VBA Code:
Sub InsertRows()
'
    Dim ws As Worksheet
    Dim sh As Worksheet
    Dim strow As Long, nrow As Long
'
    Set sh = ThisWorkbook.Worksheets("Additions")
'
    strow = ThisWorkbook.Worksheets("Test FAR").Range("A2").End(xlDown).Row + 1     'finds row to start inserting from
'
    With sh
        nrow = [Counta(Additions!B:B)] - 1      'number of rows to insert       <--- not sure why you are subtracting 1 here ???
    End With
'
    ThisWorkbook.Worksheets("Test FAR").Range("A" & strow).EntireRow.Offset(1).Resize(nrow).Insert Shift:=xlDown
    
End Sub
 
Upvote 0
Even Shorter:

VBA Code:
Sub InsertRows()
'
    Dim strow As Long, nrow As Long
'
    strow = ThisWorkbook.Worksheets("Test FAR").Range("A2").End(xlDown).Row + 1     'finds row to start inserting from
'
    nrow = WorksheetFunction.CountA(ThisWorkbook.Worksheets("Additions").Range("B:B")) - 1      'number of rows to insert       <--- not sure why you are subtracting 1 here ???
'
    ThisWorkbook.Worksheets("Test FAR").Range("A" & strow).EntireRow.Offset(1).Resize(nrow).Insert Shift:=xlDown
End Sub
 
Upvote 0
Solution
Even Shorter:

VBA Code:
Sub InsertRows()
'
    Dim strow As Long, nrow As Long
'
    strow = ThisWorkbook.Worksheets("Test FAR").Range("A2").End(xlDown).Row + 1     'finds row to start inserting from
'
    nrow = WorksheetFunction.CountA(ThisWorkbook.Worksheets("Additions").Range("B:B")) - 1      'number of rows to insert       <--- not sure why you are subtracting 1 here ???
'
    ThisWorkbook.Worksheets("Test FAR").Range("A" & strow).EntireRow.Offset(1).Resize(nrow).Insert Shift:=xlDown
End Sub
This is brilliant - thank you so much.
 
Upvote 0
Brilliant & code that I submit normally mix like oil and water, but I am happy to assist whenever possible. You are welcome @drluke.
 
Upvote 0

Forum statistics

Threads
1,213,487
Messages
6,113,943
Members
448,534
Latest member
benefuexx

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