Excel VBA has no built-in "BeforeInsertRow" event, so you cannot literally run code the moment a user inserts a row. What you can do is either insert the row yourself with VBA (running your pre-insert code first, then calling Insert), or detect an inserted row after the fact with the Worksheet_Change event and react immediately. This guide covers both approaches: the clean, reliable macro-based method and the event-based workaround for manual inserts.

What You'll Need

  • Excel for Windows or Mac (the VBA editor is not available in Excel for the web)
  • A workbook saved as a macro-enabled file (.xlsm), or a personal macro workbook if you want the code available everywhere
  • The Developer tab visible in the ribbon: if it is missing, right-click the ribbon, choose Customize the Ribbon, and tick Developer
  • Macros enabled when you open the file (click Enable Content in the security bar)

Step-by-Step

Method 1: Insert the row with a macro (true "before insert" behavior)

  1. Press Alt + F11 to open the VBA editor.
  2. In the Project Explorer on the left, right-click your workbook, choose Insert > Module.
  3. Paste a macro that runs your pre-insert code first, then inserts. For example:
    Sub InsertRowWithPrep()
      ' --- code that must run BEFORE the insert ---
      Application.ScreenUpdating = False
      ' insert a row above the active cell
      ActiveCell.EntireRow.Insert Shift:=xlDown
      ' --- code that must run AFTER the insert ---
      ActiveCell.Value = "New row"
      Application.ScreenUpdating = True
    End Sub
  4. Adjust the Insert line to target a specific row if needed, e.g. Rows(5).Insert to insert above row 5, or Range("A10").EntireRow.Insert.
  5. Press F5 with the cursor inside the macro, or assign it to a button (Developer > Insert > Button, then select the macro) so users insert rows through your macro instead of the ribbon.

Method 2: React to manual row inserts with Worksheet_Change

Because Excel fires no event specifically for row insertion, the standard workaround is to monitor the used range's row count in the Change event and compare it to a stored value.

  1. Press Alt + F11 to open the VBA editor.
  2. In the Project Explorer, double-click the sheet where you want the detection, e.g. Sheet1 (Sales) — not a standard module, the event code must live in the sheet module.
  3. Paste code like this:
    Dim lastRowCount As Long

    Private Sub Worksheet_Activate()
      lastRowCount = Me.UsedRange.Rows.Count
    End Sub

    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim currentCount As Long
      currentCount = Me.UsedRange.Rows.Count
      If currentCount > lastRowCount Then
        ' one or more rows were just inserted
        Call AfterRowInsert(Target)
      End If
      lastRowCount = currentCount
    End Sub

    Private Sub AfterRowInsert(ByVal Target As Range)
      ' your reaction code here, e.g.:
      ' fill formulas or formats in the new row
      Target.EntireRow.Interior.Color = RGB(255, 255, 200)
    End Sub
  4. Close the editor. When someone inserts a row manually (right-click > Insert, or Ctrl + Shift + +) and the sheet's used range grows, your code runs immediately afterwards.
  5. If your reaction code itself changes cells, wrap it in Application.EnableEvents = False ... Application.EnableEvents = True so it does not re-trigger the Change event in a loop. Always re-enable events before the procedure ends.

Common Mistakes

  • Putting Worksheet_Change code in a standard module instead of the sheet module — it will never fire there.
  • Forgetting to reset Application.EnableEvents = True after setting it to False; if the code errors out midway, events stay off for the whole session until Excel restarts. Use an error handler to re-enable them.
  • Assuming there is a real BeforeInsert event and searching for it in the editor's event dropdown — the Worksheet object offers Change, SelectionChange, BeforeDoubleClick and others, but nothing for row insertion.
  • Using UsedRange row-count comparison on sheets where rows are also deleted frequently; a delete followed by an insert in one go can cancel out and go undetected. Comparing the address of UsedRange rather than just the count makes it more robust.
  • Saving the workbook as .xlsx, which silently strips all macros. Use .xlsm.
  • Calling Insert on a merged range or a range crossing a table boundary without checking — insertion behaves differently inside ListObjects (tables auto-expand).

Tips

  • If your real goal is "new rows should look like existing rows", consider converting the data to an official Excel table (Ctrl + T). Tables copy formulas and formatting to new rows automatically, no VBA needed.
  • To make your macro the default way people add rows, place a form button next to the data and label it "Add row".
  • Store the sheet's row count in a hidden name or a cell so detection survives the workbook being closed and reopened — the module-level variable resets each session.
  • Use Application.Calculation = xlCalculationManual around heavy reaction code on large sheets, then restore it to xlCalculationAutomatic, for a noticeable speed gain.
  • Anchor inserts to a specific cell rather than ActiveCell in shared workbooks, so a stray selection cannot put the row in the wrong place.

Troubleshooting

  • The Change event never fires. Macros are probably disabled (check the security bar), events were switched off by earlier code (run Application.EnableEvents = True in the Immediate window, Ctrl + G in the editor), or the code sits in the wrong module.
  • Code runs twice or Excel hangs. Your reaction code writes to the sheet and re-triggers Change. Wrap it with EnableEvents = False/True as described above.
  • Insert is not detected. The inserted row fell outside the used range, or a simultaneous delete kept the count equal. Compare UsedRange.Address instead of the row count.
  • "Cannot run the macro" error on a button. The workbook was saved as .xlsx, or the button points to a macro name that was renamed. Re-assign the macro via the button's right-click menu.
  • Nothing happens in Excel for the web or mobile. Those versions cannot run VBA at all; you need the desktop application.

Frequently Asked Questions

Is there a built-in BeforeInsertRow event in Excel VBA?

No. Excel provides no event that fires when a row is about to be inserted. The reliable options are inserting rows through your own macro, or detecting inserts after the fact with Worksheet_Change.

Can I cancel a row insertion with VBA?

Not directly, since no pre-insert event exists. You can undo the insert in your Change handler, or better, prevent manual inserts by protecting the sheet (Review > Protect Sheet) and adding rows exclusively through your macro.

How do I insert a row above the currently selected cell?

Use ActiveCell.EntireRow.Insert Shift:=xlDown in a standard macro. Put any code that must run first above that line.

Does Worksheet_Change fire when a row is inserted?

Yes, inserting a row triggers the Change event with Target pointing at the shifted range. However, Change also fires for ordinary edits, which is why you must test the used-range size to confirm an insert actually happened.

Why does my macro disappear when I save?

The file was saved in .xlsx format, which cannot store macros. Save As and choose Excel Macro-Enabled Workbook (.xlsm).

Will this work in a shared workbook stored in OneDrive or SharePoint?

Event code runs only in desktop Excel with macros enabled, and co-authoring sessions do not execute VBA reliably. For shared files, prefer an Excel table so new rows inherit formatting and formulas automatically.