Excel VBA: How to Insert a Row Below
In this article
To insert a row below a given cell in Excel VBA, use the Insert method on the row beneath it: Rows(10).Insert Shift:=xlDown puts a new blank row in row 10, which pushes the old row 10 down and effectively places a blank row directly below row 9. There is no InsertBelow method in Excel VBA, so the trick is always to target the row number you actually want the insertion at, not the row you are sitting in. If you are working from an active cell, the pattern is ActiveCell.Offset(1, 0).EntireRow.Insert. Everything below explains the variants, the gotchas and how to avoid overwriting data.
What You'll Need
- Microsoft Excel on Windows or Mac with the Developer tab enabled. On Windows: File > Options > Customize Ribbon and tick Developer. On Mac: Excel > Preferences > Ribbon & Toolbar and tick Developer.
- A workbook saved in a macro-enabled format (
.xlsm) if you want the code to persist. A plain.xlsxwill discard your macros when saved. - A basic familiarity with the Visual Basic Editor (VBE), which you open with Alt+F11 on Windows or Option+F11 on Mac. Labels and menu positions can differ slightly between Excel versions, so treat the path names below as a guide.
- Optionally, a backup copy of the sheet you are testing on, because an insert script that runs on the wrong row will shift data and is not always easy to undo.
Step-by-Step
Open the Visual Basic Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project Explorer on the left, find your workbook, right-click it and choose Insert > Module. A blank code window appears. Working in a standard module keeps the macro available in the Macros list; code placed in a sheet module runs only on that sheet and usually needs a trigger.
Start the macro with a clear name. Type
Sub InsertRowBelow()and press Enter. Name it something you will recognise later, particularly if you plan to assign it to a button. Avoid names that clash with Excel's own objects, such asInsertorRows.Declare the target row. Add a line such as
Dim targetRow As Longand then set it. For a fixed position, usetargetRow = 10. To insert below whatever the user has selected, usetargetRow = ActiveCell.Row + 1. Using a variable rather than hard-coding the number inside the insert line makes the macro far easier to adjust later.Insert the row. Write
Rows(targetRow).Insert Shift:=xlDown. TheShift:=xlDownargument is important: without it Excel guesses based on surrounding data, and on some sheets that guess isxlShiftRight, which shifts cells sideways instead of down. Being explicit avoids that entirely.Use the Offset shortcut if you prefer working from a cell. The single line
ActiveCell.Offset(1, 0).EntireRow.Insert Shift:=xlDowninserts a blank row directly beneath the selected cell.Offset(1, 0)moves one row down,EntireRowexpands the reference to the whole row, andInsertdoes the work. This is the most common one-liner people use for "insert below".Insert more than one row. Change the reference to cover multiple rows, for example
Rows("10:12").Insert Shift:=xlDownfor three blank rows starting at row 10, or, in the Offset style,ActiveCell.Offset(1, 0).Resize(3).EntireRow.Insert. The count must match the number of rows you actually want to appear.Copy formatting if you need it. A plain
Insertusually takes on the formatting of the row above, but that is not guaranteed when borders, fill colours or merged cells are involved. To be certain, copy the row above and insert the copied row:Rows(targetRow - 1).Copyfollowed byRows(targetRow).Insert Shift:=xlDown. Afterwards callApplication.CutCopyMode = Falseto clear the marching-ants selection, otherwise Excel keeps the copied range alive.Insert at the end of a data block. If you want the new row directly below the last used row, use
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow.Insert Shift:=xlDown. This assumes column A is populated for every record; if it is not, the macro may insert in the wrong place, so pick a column that is always filled.Test and step through. Press F8 to run the code one line at a time and watch the worksheet. When you are satisfied, press F5 to run it fully, or close the editor and use Developer > Macros, select your macro and click Run. If you want it on a button, insert a shape from Insert > Shapes, right-click it and choose Assign Macro.
Common Mistakes
- Inserting above instead of below.
Rows(10).Insertcreates the blank row at row 10, pushing the old row 10 down. If you meant "below row 10", you want row 11. Off-by-one errors here are the single most common cause of data landing in the wrong place. - Omitting
Shift:=xlDown. Excel then uses its own guess, and on sheets where the surrounding data forms a horizontal block it can shift cells right, corrupting the layout. - Forgetting that
ActiveCellmay not be where you think. Macros run against whatever is selected at the time. If the user clicked elsewhere, the row lands somewhere unexpected. Prefer explicit row numbers or a named range. - Leaving copy mode on. After a
CopyandInsertpair, the clipboard stays active unless you clear it, so pressing Enter later pastes over something. - Running the macro over merged cells. Inserting into rows that contain vertically merged cells throws an error or produces odd results. Unmerge first, insert, then re-merge if needed.
- Not handling a protected sheet. On a protected worksheet, the insert fails with a permission error. Unprotect with the password, or have the macro do it and re-protect afterwards.
Tips
- Wrap the insert in
Application.ScreenUpdating = Falseand set it back toTrueat the end when running it in a loop; it is much faster on large sheets. - Add
On Error GoTohandling so a failed insert shows a friendly message rather than a raw error dialog. - Use
Application.EnableEvents = Falsewhile inserting if the sheet has event code, otherwise the insert itself can trigger your ownWorksheet_Changeprocedure. - If the new row should also carry a formula, write the formula into the row after inserting rather than relying on the insert to drag it down.
- Keep a single "helper" macro that takes a row number as an argument, then call it from other procedures. It keeps all the insert logic in one place.
Troubleshooting
Nothing appears to happen. The macro may have run on a different sheet than the one you are viewing, or the new row may look identical to the one above. Check the row numbers in the left margin and confirm the sheet name with MsgBox ActiveSheet.Name before the insert line.
Runtime error 1004. This is Excel's generic "application-defined or object-defined error" and here it usually means a protected sheet, an invalid row reference such as Rows(0), or an insert that would push data past the last row. Check the sheet protection state and the row number you are passing.
Data shifted sideways. You almost certainly omitted Shift:=xlDown. Undo immediately with Ctrl+Z, add the argument and rerun on a copy of the sheet.
"Sub or Function not defined". The macro name is misspelled, or the module failed to compile because of another error higher in the subroutine. Use Debug > Compile VBAProject to find the real culprit.
Macros are disabled. Excel shows a security bar. Because your file is your own, enable content, and confirm the workbook is saved as .xlsm. Do not download macro workbooks from untrusted sources to get this working.
Frequently Asked Questions
Is there a direct "insert row below" command in VBA?
No. VBA has only the Insert method, which inserts at the row you reference. To get a blank row below row 9, you insert at row 10, or use Offset(1, 0).EntireRow.Insert from a cell in row 9.
How do I insert a row below the last row of data?
Use Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow.Insert, changing the column number to a column that is filled for every row. Note that Offset(1, 0) here already moves one row past the last used row, so you would drop it if you want the insert directly on the first blank row.
Can I insert a row and copy the formula from the row above?
Yes. Copy the row above, insert the copied row with Shift:=xlDown, then set Application.CutCopyMode = False. The inserted row carries the copied formulas and formats.
Why does the macro fail on a protected sheet?
Inserting rows changes the worksheet structure, which protection blocks. Unprotect the sheet first, insert, then re-protect. If you do not know the password, ask whoever set it; there is no supported way to work around protection.
How do I insert several blank rows at once?
Reference a multi-row range, such as Rows("10:14").Insert Shift:=xlDown for five rows, or use Resize(5) with the Offset method. The count in the range or Resize is the number of blank rows created.
Will inserting a row break my named ranges or charts?
Named ranges usually expand automatically, but hard-coded chart source ranges and some pivot table source references may not. After inserting rows in a sheet that feeds charts or pivot tables, refresh the pivot table and check the chart data range.