Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Insert an Entire Row (Step-by-Step)

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Use Rows(10).Insert to insert a full row above row 10, or Rows(10).EntireRow.Insert when you are working from a Range object that may not span the whole row. That single line is all you need for the simplest case; the rest of this guide covers capturing the row you actually want, keeping formatting sensible, inserting several rows at once, and avoiding the errors that catch most people. Nothing needs to be installed first, but macros must be enabled and your workbook saved as a macro-enabled file if you want to keep the code.

What You'll Need

  • Microsoft Excel on Windows or Mac with the Developer tab visible. If it is hidden, right-click the ribbon and choose Customize the Ribbon, then tick Developer in the list on the right.
  • A workbook saved as .xlsm (Excel Macro-Enabled Workbook). If you save VBA in a plain .xlsx file, Excel strips the code on close.
  • Macro security set to allow your code to run. In the Trust Center, choose Disable all macros with notification, which lets you approve macros each time rather than blocking them silently.
  • A sheet to test on. Inserting rows shifts everything below down and can break references, so practice on a copy before running the macro on live data.

Menu names and dialog wording differ slightly between Excel versions, between Windows and Mac, and by region, so treat the paths below as close approximations of what you will see.

Step-by-Step

  1. Open the editor. Press Alt + F11 on Windows, or Option + F11 on Mac, to open the VBA editor. Choose Insert > Module to create a standard code module. If you already have a module, reuse it.
  2. Write the simplest version first. Type the following and press F5 to run it:

    Sub InsertRow()
        Rows(10).Insert
    End Sub

    This inserts one blank row above row 10 on the active sheet and pushes the old row 10 down to row 11.
  3. Decide which row you really mean. Rows(10) is the tenth row of the worksheet, not the tenth row of your table. If your data starts in row 5 with a header, the tenth data row is row 14, not row 10. Count from the top of the sheet or use a variable instead of a fixed number.
  4. Use a variable so the macro follows your data. Replace the hard-coded number with something that resolves at run time:

    Sub InsertAtActiveCell()
        ActiveCell.EntireRow.Insert
    End Sub

    Select the cell in the row where you want the break, run the macro, and the new row appears above it. This is the version most people want when they are working interactively rather than running a batch job.
  5. Work with a named sheet for reliability. Rows(10).Insert acts on whichever sheet is active, so a macro that runs while the wrong tab is showing will insert into the wrong place. Anchor it:

    Worksheets("Data").Rows(10).Insert

    On sheets that carry their own module name, you can also write Sheet1.Rows(10).Insert, but the visible sheet name is easier to keep readable.
  6. Insert several rows at once. Pass a count, or work from a range spanning multiple rows:

    Rows(10).Resize(3).Insert
    Range("10:12").Insert

    Both add three blank rows. Do not loop the same insert call three times unless you also work out where each one lands, because each insert shifts the rows below it.
  7. Copy the formatting and values from above. A plain insert leaves plain rows and can inherit odd formatting from the row above depending on how your sheet is laid out. If you want the new row to match the one above, use:

    Rows(10).Insert CopyOrigin:=xlFormatFromLeftOrAbove

    For values as well as formats, copy first and then insert: Rows(9).Copy, then Rows(10).Insert, then Application.CutCopyMode = False to clear the marching ants border.
  8. Test on a copy. Run the macro once, check the row numbers below, and confirm that named ranges, formulas and charts still point where you expect. Undo with Ctrl + Z does not work for most macro actions, so save a copy before you iterate.

Common Mistakes

Forgetting that rows shift. If you insert at row 10 and then insert again at row 10, everything moves twice. The second insert lands above the first blank row, not below it. Track the index deliberately or insert once with a count.

Using Range("A10").Insert and expecting a whole row. That inserts a cell shift unless you add the row argument. Use EntireRow.Insert or an explicit row range.

Running against the wrong sheet. The bare Rows(n).Insert call is sheet-relative. Name the sheet whenever the macro is triggered from a button or event code.

Losing formulas or totals. Inserting above a SUM range shifts the reference correctly, but inserting inside a table can leave a total row orphaned. Check totals after every test run.

Saving in the wrong file format. Code vanishes when a macro-enabled workbook is saved as .xlsx. Excel usually warns you; take the warning seriously.

Copying and never clearing the clipboard. Rows(9).Copy leaves a copy loop active, and the next insert can bring the copied content along. Always follow with Application.CutCopyMode = False.

Tips

  • Turn off screen updating during loops with Application.ScreenUpdating = False before the loop and restore it to True at the end. This speeds up bulk edits noticeably.
  • Use Range("A:A").Find or Cells(Rows.Count, 1).End(xlUp).Row to locate the last used row, then insert relative to that number rather than a fixed one.
  • Bind the macro to a keyboard shortcut through the Macros dialog (Options button) so you do not need the editor each time.
  • Keep each macro small and single-purpose. One macro inserts a row; another fills it. That makes debugging far easier.
  • Add On Error GoTo handling that reports the row number, so a failed insert tells you where it stopped.
  • Comment the expected sheet name at the top of each module. Six months from now it saves you a hunt.

Troubleshooting

The macro does nothing. Check you pressed Run with the cursor inside the procedure and that the sheet is not protected. A protected sheet silently blocks inserts; unprotect it in Review > Unprotect Sheet, or have the code unprotect first if you know the password.

Runtime error 1004. The most common causes are a protected sheet, a protected workbook structure, or a sheet reference that does not exist. Confirm the sheet name spelled in code matches the tab exactly, including spaces.

Rows appear in the wrong place. You are almost certainly counting from the data instead of the sheet. Print the target row number with Debug.Print and read it in the Immediate window before the insert runs.

Formatting looks wrong afterwards. Use CopyOrigin:=xlFormatFromLeftOrAbove, or copy and paste-special formats from a known-good row.

The code disappeared. The workbook was saved as .xlsx or as a template without macros. Save as .xlsm and re-enter the code, then check that the module exists under the VBAProject in the editor.

Excel hangs in a loop. Press Esc and Ctrl + Break to interrupt, then inspect the loop bounds. An insert inside a loop that increments the row counter will double the number of rows each pass.

Frequently Asked Questions

What is the difference between Insert and EntireRow.Insert?

Rows(n).Insert already refers to a whole row. EntireRow is what you add when you start from a single cell or a multi-column range, as in Range("B10").EntireRow.Insert, to make the insert span the full row.

Can I insert a row above the active cell without knowing the row number?

Yes. ActiveCell.EntireRow.Insert inserts above whatever cell is selected.

Does inserting rows break formulas?

Excel adjusts standard references and table references automatically. Absolute references and hard-coded row numbers inside VBA do not adjust, so check any code that targets specific rows after a run.

Can I undo a VBA insert?

Not with Ctrl + Z. Macro actions are generally outside the undo stack, so keep a backup copy or save before running.

How do I insert a row at the bottom of my data?

Find the last used row with Cells(Rows.Count, 1).End(xlUp).Row, add one, and insert above that number.

Will this work on Excel for Mac?

Yes, the same object model and code work. Only the editor shortcut and some ribbon wording differ, so use Option + F11 to open the editor on a Mac.