In this post, let’s have a look at examples in Excel VBA demonstrating how to insert row or column in your Microsoft Excel spreadsheet.
How to Insert Row or Column in Excel VBA?
To insert rows or columns we can use the Insert Method in Excel.
Insert a Single Row
Insert New Row
To insert a single row, use this VBA code.
Code:
Rows(4).Insert
Insert Entire Row
To insert an entire row, use the below VBA code.
Code:
Range("b4").EntireRow.Insert
Insert New Column
To insert new column, use the below code snippet
Code:
Columns(4).Insert
Insert an Entire Column
To insert an EntireColumn in your excel worksheet, use the below code snippet.
Code:
Range("b4").EntireColumn.Insert
Insert Multiple Rows or Columns
Insert Multiple Rows
To insert multiple rows
Code:
Rows("4:6").Insert
Insert Multiple Columns
To insert Multiple Columns
Code:
Columns("B:D").Insert
Insert Rows Based on Cell Value
This will loop through a range, inserting rows based on cell values:
Sub InsertRowswithSpecificValue() Dim cell As Range For Each cell In Range("b2:b20") If cell.Value = "insert" Then cell.EntireRow.Insert End If Next cell End Sub