To insert a variable number of rows in Excel with VBA, use the Rows(...).Insert or Range(...).EntireRow.Insert method and pass the number of rows as a variable in the range reference. For example, to insert n rows above row 5 of the active sheet: Rows("5:" & 4 + n).Insert. This shifts existing data down and keeps everything intact, which is useful any time the count of blank rows you need depends on a user input, a cell value, a count of records, or a loop counter. Nothing beyond Excel itself is required, but you must save your workbook first because inserted rows cannot be undone with Ctrl+Z after a macro runs.
What You'll Need
- Excel with macros enabled (any desktop version on Windows or Mac; the steps below use the desktop app, not Excel for the web, which cannot run VBA).
- A workbook saved as .xlsm (macro-enabled) if you want the code to persist.
- Basic access to the Developer tab. If it is not visible, right-click the ribbon, choose Customize the Ribbon, and tick Developer in the right-hand list. Labels can differ slightly by version and region.
- A backup copy of your data, since insert operations change the sheet immediately.
Step-by-Step
- Open the Visual Basic Editor. Press
Alt+F11on Windows (Fn+Option+F11on many Macs), or go to Developer > Visual Basic. - Insert a module. In the editor choose Insert > Module. Code in a standard module can be run from the Macros dialog; code in a sheet module only runs from events.
- Declare your variables. Start with
Dim n As Longand, if you are working with a specific sheet,Dim ws As WorksheetthenSet ws = ThisWorkbook.Worksheets("Sheet1"). Using an explicit sheet reference prevents the macro from inserting rows on whatever sheet happens to be active. - Set the count. Assign
nfrom wherever your number comes from: a user prompt (n = InputBox("How many rows?")), a cell (n = ws.Range("B1").Value), or a calculation (n = Application.WorksheetFunction.CountA(ws.Range("A:A"))). - Validate the number before using it. Insert a guard such as
If n < 1 Then Exit Suband optionallyIf n > 1000 Then MsgBox "Too many rows": Exit Sub. Without this, a blank cell or a negative count will throw an error or flood the sheet. - Insert the rows. The most reliable pattern is to build the range from the row numbers:
ws.Rows("5:" & 4 + n).Insertinsertsnrows starting at row 5 and pushing the old row 5 downward. Equivalent forms includews.Range("A5").Resize(n).EntireRow.Insertandws.Range("5:" & 4 + n).Insert Shift:=xlDown. - Clear formatting if you want blank rows. Inserted rows inherit formatting from the row above, so borders and fills can appear where you did not want them. Follow the insert with
ws.Rows("5:" & 4 + n).ClearFormatsorClearContentsas appropriate. Note that inserting full rows anywhere in a table extends the table, which may not be what you want. - Disable screen updating for speed. Wrap the work in
Application.ScreenUpdating = Falseat the start and set it back toTruebefore the procedure ends, including on error. Inserting many rows one at a time is dramatically slower than inserting them in a single call, so always insert the whole block at once rather than loopingInsertn times. - Run and check. Press
F5with the cursor inside the procedure, or return to Excel and use Developer > Macros. Confirm the right rows moved and the count matches your expectation. - Save as .xlsm. Use File > Save As and pick the macro-enabled format. A plain .xlsx save will strip the code after a warning.
Common Mistakes
Off-by-one errors in the range string. Rows("5:" & n) with n = 3 inserts rows 5, 6 and 7 only if you meant three rows starting at 5, which gives "5:" & 5 + n - 1, or 4 + n. Test with a small number and count the visible new rows.
Looping one row at a time. A loop that calls .Insert repeatedly leaves you with rows inserted in reverse order relative to your logic and is slow on large ranges. Insert the block once.
Referencing the active sheet implicitly. Rows("5:" & 4 + n).Insert without a sheet qualifier acts on whatever sheet is active, so the macro inserts in the wrong place if the user clicks elsewhere first.
Inserting inside a table. If the rows sit within an Excel Table, EntireRow.Insert adds rows to the table itself. To add plain rows, insert above the table, or temporarily convert the table to a range.
Forgetting error handling. If the sheet is protected, the insert fails with a run-time error. Unprotect the sheet first (ws.Unprotect "password") and reprotect afterward, handling passwords securely rather than hard-coding them in shared workbooks.
Inserting with merged cells nearby. Merged cells can make whole-row insertion raise an error; unmerge or insert above the merged area.
Tips
Use Long rather than Integer for row counts, since Excel has far more rows than an Integer can hold. Store the target row in a variable such as startRow so the code reads clearly and you can change the insertion point in one place. If you want the inserted rows to carry no formatting from the row above, copy an empty formatted row and Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow or clear formats afterwards. For repeated inserts in different places, write a small helper procedure that takes the sheet, start row and count as arguments. Turn off Application.Calculation too when the workbook is heavy, and always restore both settings in a clean-up line.
Troubleshooting
If the macro inserts in the wrong sheet, qualify every range with a worksheet object and avoid ActiveSheet in production code.
If you get a "Method 'Insert' of object 'Range' failed" error, check for sheet protection, merged cells, or a filter that is hiding rows. Removing the filter range or unprotecting the sheet usually resolves it.
If nothing happens at all, confirm macros are enabled in the Trust Center and that you are running the intended procedure from the Macros list, not a different module with the same name.
If the wrong number of rows appear, print the computed row string with Debug.Print or a MsgBox just before the insert and compare it with what you expect.
If rows look odd after inserting, clear formats or conditional formatting from the new range, since formatting and data validation can be inherited from the row above.
Frequently Asked Questions
Can I insert a number of rows taken from a cell?
Yes. Read the cell into a Long variable, validate it, then use it in the range string as shown in step 6.
Why does my macro insert thousands of rows?
Usually the count variable holds an empty cell read as 0, or a stray value from UsedRange. Add a sanity check on the upper bound before inserting.
Does this work in Excel for the web?
No. The browser version cannot run VBA macros. You need the desktop app.
Can I undo an insert made by a macro?
Not with Ctrl+Z. Macros clear the undo stack, so keep a backup or make the macro save a copy first.
How do I insert rows without inheriting formatting?
Run ClearFormats on the newly inserted range immediately after the insert.
Can I insert rows below existing data instead?
Yes. Find the last used row, then set the start row to that value plus one and insert the block there.