To add a row with Excel VBA, use the worksheet’s Rows.Insert method, such as Worksheets("Sheet1").Rows(5).Insert to insert a blank row above row 5. In Microsoft 365 for Windows, qualify the worksheet explicitly so the macro changes the intended sheet, and save a copy before running it because macros cannot be undone with Ctrl+Z.
What inserting a row does
Inserting a worksheet row shifts the existing row and the rows below it downward. It is different from clearing cells or writing values into an unused row: insertion changes the sheet’s layout and can affect formulas, references, formatting, tables, and other objects. The row number in Rows(5) identifies the position before which Excel inserts the new row.
A complete, reusable example is:
Sub AddRowToSheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
' Insert a blank row above row 5.
ws.Rows(5).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
Change Sheet1 to the worksheet’s tab name and change 5 to the desired position. ThisWorkbook means the workbook containing the macro; use ActiveWorkbook only when you deliberately want the workbook currently active in Excel. The optional arguments make the shift direction and formatting source explicit.
How to run the macro safely
- Save a copy of the workbook. VBA changes are not reversed by Excel’s Undo command.
- Save the working file in a macro-enabled format, such as Excel Macro-Enabled Workbook (
.xlsm), if it contains VBA you need to keep. - Press Alt+F11 on Windows to open the Visual Basic Editor. Choose Insert > Module, then paste the procedure into the module.
- Return to Excel, press Alt+F8, select
AddRowToSheet, and choose Run. If Excel displays a macro security warning, run the code only if you trust its source.
On a Mac, open the Visual Basic Editor using the Developer tab or the applicable Visual Basic Editor command in Excel, then add the code to a standard module. Keyboard shortcuts can differ by keyboard and macOS settings. Excel for the web can open and edit a workbook containing VBA, but it cannot create, edit, or run VBA macros; use the desktop Excel app.
Choose a row position and formatting
To insert at the top, use ws.Rows(1).Insert. To insert above the current last used row, first identify which column reliably contains data; a sheet’s last used row can vary by column. For example, if column A is populated for every record, this macro inserts above the last occupied cell in that column:
Sub AddRowBeforeLastRecord()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Insert above the last occupied row in column A.
ws.Rows(lastRow).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
This example inserts before the last record, not after it. To add beneath the last record, insert at lastRow + 1. If column A might contain gaps or be blank while other columns have data, choose a more reliable column or use a table instead. A worksheet also always has a row 1,048,576; trying to insert a row when the sheet is already full can fail.
The CopyOrigin argument controls where inserted formatting comes from: xlFormatFromLeftOrAbove copies formatting from above, while xlFormatFromRightOrBelow uses the other direction. Excel does not provide an Insert dialog’s “Clear Formatting” option through this argument. If formatting matters, check the new row after insertion or apply the intended format explicitly.
Insert several rows at once
For a block of blank rows, insert a range rather than repeating a single-row insertion. This adds three rows above row 5:
Sub AddThreeRows()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
' Insert three blank rows above row 5.
ws.Rows("5:7").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
Inserting the range at once is generally clearer than looping through individual rows. If you do insert rows in a loop at changing positions, earlier insertions can shift later row numbers, so the result may not be where you expect. Plan whether the range denotes the insertion position before or after existing records.
Worksheet rows versus Excel table rows
If the data is an Excel Table, inserting an entire worksheet row may not be the best way to add a record. Tables expand as data is entered in the row directly beneath them, and VBA can add a table row directly with ListRows.Add. For example:
Sub AddTableRow()
Dim ws As Worksheet
Dim tbl As ListObject
Dim newRow As ListRow
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set tbl = ws.ListObjects("SalesTable")
' Add a row to the end of the table.
Set newRow = tbl.ListRows.Add
End Sub
Replace SalesTable with the table’s actual name. A table row is usually preferable for structured records because table formulas, formatting, filters, and references are designed to expand with the table. Use tbl.ListRows.Add Position:=1 to add at the beginning of the table’s data rows; this is not the same as inserting worksheet row 1. The default adds at the end.
Common problems and checks
- The wrong sheet changes:
Rows(5)without a worksheet qualifier acts on the active sheet. Usews.Rows(5)orWorksheets("Sheet1").Rows(5). - The sheet name is not found: The text must match the worksheet tab name exactly. A renamed or deleted sheet causes a runtime error.
- Protected worksheet: Excel may prevent insertion while the sheet is protected. Unprotect it first, or deliberately configure protection to allow row insertion.
- Merged cells or unusual layouts: Merged ranges, array formulas, and other layout constraints can block or complicate insertion. Review the affected area if Excel reports an error.
- Formulas or references change: Excel adjusts many cell references when rows are inserted, but the result depends on the formula and where the insertion occurs. Check dependent formulas, charts, named ranges, and external links.
- Macro does not run: Confirm the file is macro-enabled, the procedure is in a standard module, and macros are permitted by the organization’s security settings. Do not enable macros in an untrusted file.
Frequently Asked Questions
Does VBA insert a row above or below the row number?
With ws.Rows(5).Insert, Excel inserts a blank row above the existing row 5 and shifts the old row 5 downward. To insert beneath an existing row 5, insert at row 6.
How do I add a row after the last row with data?
Find the last occupied cell in a dependable column, then insert at the next row. If column A always contains a value for each record, calculate lastRow with ws.Cells(ws.Rows.Count, "A").End(xlUp).Row and insert at lastRow + 1. Account for headers and an otherwise empty sheet.
Can I add a row without shifting the entire worksheet?
Not when inserting a whole worksheet row: cells below it shift down by design. If you only want to add a record to a table, use ListRows.Add. If you mean filling an already blank row, write values into that row instead of inserting.
Can I undo a VBA row insertion with Ctrl+Z?
No. Running a VBA macro clears Excel’s normal Undo history, so Ctrl+Z will not reverse the insertion. Save a separate copy before running code that changes workbook structure.
Will a new table row copy formulas automatically?
Excel tables commonly propagate calculated-column formulas and table formatting when a row is added, but workbook settings and the table’s existing contents can affect what fills in. Verify the new row’s formulas and values, especially when adding data through a macro.
Related
- Excel Shortcut Key to Go to the Last Row in a Sheet
- How to Add Color to a Header Row in Microsoft Word
- How to Freeze More Than the Top Row in Excel
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA to Send Email in Outlook 365: Setup and CodeYes. In Excel for Microsoft 365 on Windows, VBA can create and send an email through the classic…
- Logical Not in VBA: Not Operator & UsageThe logical NOT in VBA is the Not operator, and you use it to reverse a Boolean expression:…
- Excel VBA: Set Cell Text Color with Font.ColorTo set a cell’s text color in Excel VBA, change the cell’s Font.Color property, usually with the RGB…
- Excel VBA Indent Level: Set and Read IndentsIn Excel VBA, use a cell’s IndentLevel property to read or set its indent: for example, Range("A1").IndentLevel =…
- Excel VBA Scroll Bar Form Control: Setup, Uses, and CodeIn Excel for Microsoft 365 on Windows, insert a Form Control scroll bar from Developer > Insert >…
- Excel Macro to Clear Cell Contents: VBA Steps and SafetyTo clear cell contents with an Excel macro, use VBA’s ClearContents method on the cells you want to…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.