How To Insert Multiple Rows In Excel Vba

How to Insert Multiple Rows in Excel using VBA

To insert multiple rows in Excel using VBA, you can use the ‘Insert’ method of the ‘Range’ object. This method allows you to insert a specified number of rows at a specific location within the sheet.

Here’s an example:

Sub InsertMultipleRows()
    Dim numRows As Integer
    Dim insertRange As Range
    
    ' Set the number of rows to insert
    numRows = 3
    
    ' Set the range where the rows should be inserted
    Set insertRange = Range("A2")
    
    ' Insert the rows
    insertRange.Resize(numRows).EntireRow.Insert
End Sub

In this example, we first declare two variables: ‘numRows’ to specify the number of rows to insert and ‘insertRange’ to specify the location where the rows should be inserted. We set ‘numRows’ to 3 and ‘insertRange’ to cell “A2”.

Next, we use the ‘Insert’ method on the ‘insertRange’ range object. We resize the range to match the specified number of rows (‘numRows’) and then use ‘EntireRow’ property to insert entire rows at the specified location.

When you run this macro, it will insert 3 rows above the existing row 2 in the active sheet. The inserted rows will shift the existing content down.

Notes:

  • You can adjust the ‘numRows’ variable to specify the number of rows you want to insert.
  • You can change the ‘insertRange’ to a different range to insert rows at a different location.

Same cateogry post

Leave a comment