To insert a row in Excel using VBA, you use the Rows or EntireRow property with the Insert method. The simplest one-line command is Rows(5).Insert, which inserts a new row above row 5. This guide walks through the exact code, where to put it, and how to handle common variations like inserting multiple rows or inserting based on a cell's value.

What You'll Need

  • Microsoft Excel (Windows or Mac) with the Visual Basic Editor (VBE) available. On Windows, the Developer tab must be enabled to access VBA easily; on Mac, the Developer tab is also needed.
  • Basic familiarity with opening the VBA editor (press Alt+F11 on Windows, Fn+Option+F11 on Mac).
  • Your workbook saved as a macro-enabled file (.xlsm) if you want to keep the macro.

Step-by-Step

  1. Open the Visual Basic Editor. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac). Alternatively, go to the Developer tab and click Visual Basic.
  2. Insert a module. In the VBE, right-click on your workbook's name in the Project Explorer (left pane), choose Insert, then Module. This creates a new module where you can write your code.
  3. Write the basic insert-row macro. In the module, type the following code:
    Sub InsertRow()
        Rows(5).Insert
    End Sub

    This inserts a new row above row 5. The existing row 5 shifts down.
  4. Run the macro. Press F5 while the cursor is inside the macro, or go to the Run menu and choose Run Sub/UserForm. You can also close the VBE and use the Macros button on the Developer tab.
  5. Insert a row based on the active cell. If you want to insert above the cell you've selected, use ActiveCell.EntireRow.Insert. This is handy for quick manual selection.
  6. Insert multiple rows. Use Rows("3:5").Insert to insert three rows above row 3. Or use Rows(3).Resize(3).Insert for a more dynamic approach.
  7. Insert a row and copy formatting from above. By default, Excel may not copy formatting. To copy the format from the row above, use:
    Rows(5).Insert CopyOrigin:=xlFormatFromLeftOrAbove
    Or for the row below, use xlFormatFromRightOrBelow.
  8. Insert a row at the end of a data range. To find the last used row and insert after it, use:
    Dim lastRow As Long
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Rows(lastRow + 1).Insert

    This is useful when you want to add a row at the bottom of a table.
  9. Insert a row when a condition is met. For example, to insert a row after every row where column A contains "Total":
    Dim i As Long
    For i = 10 To 2 Step -1
        If Cells(i, 1).Value = "Total" Then
            Rows(i + 1).Insert
        End If
    Next i

    Note the loop goes from bottom to top (Step -1) so that inserted rows don't disrupt the loop.
  10. Test your macro on a copy of your data. Before running on important sheets, try it on a dummy sheet to verify the correct rows are inserted.

Common Mistakes

  • Using the wrong row number. Rows(5).Insert inserts above row 5, not below. If you want to insert below row 5, you need Rows(6).Insert.
  • Forgetting to enable the Developer tab. You can still use Alt+F11, but if you prefer the ribbon, the Developer tab must be enabled via Excel Options (File > Options > Customize Ribbon).
  • Not saving as .xlsm. If you save as .xlsx, the macro is lost. Always use .xlsm for macro-enabled workbooks.
  • Inserting rows in a loop from top to bottom. If you loop from top to bottom and insert rows, the loop may skip rows or insert in wrong places. Always loop from bottom to top when inserting rows conditionally.
  • Assuming formatting is copied. By default, the Insert method may not bring formatting from the row above. Use the CopyOrigin parameter if you need consistent formatting.

Tips

  • Use the macro recorder to learn. Turn on the macro recorder, manually insert a row, and then examine the generated code. It often shows the exact syntax for your Excel version.
  • Use EntireRow for clarity. Rows(5).Insert and Rows(5).EntireRow.Insert do the same thing, but the latter is more explicit and easier to read.
  • Turn off screen updating. If your macro inserts many rows, add Application.ScreenUpdating = False at the start and True at the end to speed it up.
  • Use On Error Resume Next carefully. It can hide bugs. Better to write error handling that tells you what went wrong.
  • Test with a backup. Always keep a backup of your workbook before running macros that modify data.

Troubleshooting

  • Macro does nothing. Check that you're running the correct macro. Also, ensure the row number you're inserting is within the worksheet's limits (Excel has 1,048,576 rows). If you try to insert below the last row, it may fail silently.
  • Error "Method 'Insert' of object 'Range' failed". This often happens when trying to insert a row that would push data off the sheet. Make sure there is no data in the very last row of the worksheet. Clear any stray data in row 1,048,576.
  • Formatting is inconsistent. If the inserted row doesn't match the surrounding rows, use the CopyOrigin parameter or manually copy formatting from an existing row using Rows(5).Copy then Rows(6).Insert Shift:=xlDown (this copies everything, including formulas).
  • Macro works but inserts in the wrong sheet. If you have multiple sheets, always qualify the sheet name, e.g., Worksheets("Sheet1").Rows(5).Insert. Otherwise, VBA uses the active sheet.
  • Loop inserts too many rows. If your condition is met multiple times, you might insert multiple rows. Review your loop logic and ensure you're using Step -1.

Frequently Asked Questions

How do I insert a row without losing formatting?

Use Rows(5).Insert CopyOrigin:=xlFormatFromLeftOrAbove to copy the formatting from the row above. This copies number formats, fonts, and borders, but not formulas or values.

Can I insert a row using VBA without the Developer tab?

Yes, you can press Alt+F11 to open the VBA editor directly, even if the Developer tab is hidden. The Developer tab is only needed for the button interface.

How do I insert a row after every row with data?

Use a loop that goes from the last row to the first, inserting a row after each row that contains data. For example, if your data is in column A, loop from the last used row down to 1 and insert after each row.

What is the difference between Rows(5).Insert and ActiveCell.EntireRow.Insert?

Rows(5).Insert always inserts above row 5, regardless of where your cursor is. ActiveCell.EntireRow.Insert inserts above the row where your cursor is currently located. The latter is more flexible if you want to insert based on user selection.

Can I undo a row insertion done by VBA?

Yes, in most cases you can press Ctrl+Z immediately after running the macro. However, if the macro performs other actions, undo may not work. It's safer to test on a copy.

How do I insert multiple rows at once?

Use Rows("3:5").Insert to insert three rows above row 3. Or use Rows(3).Resize(3).Insert which is more dynamic if you want to change the number later.