How To Insert A Row In Excel Vba

Inserting a Row in Excel Using VBA

VBA (Visual Basic for Applications) allows you to automate tasks in Microsoft Excel, including inserting rows. Here is an example of inserting a row using VBA:

Sub InsertRowExample()
    Dim rowNum As Integer
    rowNum = 2 ' Specify the row number where you want to insert the new row

    ' Shift existing rows down to make room for the new row
    Rows(rowNum & ":" & rowNum).Insert Shift:=xlDown

    ' Modify the contents of the new row as needed
    Range("A" & rowNum).Value = "New Value1"
    Range("B" & rowNum).Value = "New Value2"
    ' Insert more code to update other cells in the new row if required
End Sub
  

In the above code:

  • Change the value of the rowNum variable to the desired row number where you want to insert the new row. In this example, the new row will be inserted at row number 2.
  • The Rows(rowNum & ":" & rowNum).Insert Shift:=xlDown line of code inserts a new row and shifts the existing rows down.
  • Modify the Range("A" & rowNum).Value = "New Value1" and Range("B" & rowNum).Value = "New Value2" lines of code to update the contents of the new row as needed. Replace “New Value1” and “New Value2” with the actual values you want to insert into the respective cells.

You can add more code to update other cells in the new row if required.

Leave a comment