VBA macros · Data
VBA macro to insert multiple rows or columns in Excel
Insert any number of blank rows above the active cell, or columns to its left, and insert a blank row between every row of data.
Inserting ten rows by hand means selecting ten rows first, which is awkward in the middle of a long list. This macro asks how many rows you need and inserts them above the active cell, copying the formatting of the row above. Below it you will find versions for columns and for putting a blank row between every data row.
Option Explicit
Sub InsertMultipleRows()
Dim n As Variant
If TypeName(Selection) <> "Range" Then Exit Sub
n = Application.InputBox("How many rows should be inserted above row " & _
ActiveCell.Row & "?", "Insert rows", 1, Type:=1)
If VarType(n) = vbBoolean Then Exit Sub ' Cancel
n = Int(n)
If n < 1 Or n > 10000 Then
MsgBox "Please enter a number from 1 to 10,000.", vbExclamation
Exit Sub
End If
On Error GoTo Fail
ActiveCell.EntireRow.Resize(n).Insert Shift:=xlDown, _
CopyOrigin:=xlFormatFromLeftOrAbove
Exit Sub
Fail:
MsgBox "Could not insert rows: " & Err.Description, vbExclamation
End SubHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickInsertMultipleRowsand click Run. - Click a cell in the row that should move down, then run the macro.
- Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.
How it works
Application.InputBox with Type:=1 makes Excel check that a number was typed. Int(n) drops any decimals, and the range check stops silly values; inserting a million rows would push data off the bottom of the sheet.
ActiveCell.EntireRow is the full row of the active cell. Resize(n) stretches it to n rows downward, and Insert Shift:=xlDown inserts that many empty rows, pushing the active row and everything below it down. CopyOrigin:=xlFormatFromLeftOrAbove gives the new rows the formatting of the row above, the same as Excel's default when you insert by hand. Use xlFormatFromRightOrBelow to copy from below instead.
The error handler catches the usual reasons an insert fails: a protected sheet, or data in the very last rows of the sheet that would be pushed off the grid. Resize and EntireRow are covered in Range and Cells: working with cells in VBA.
Change it
For columns, the same pattern uses EntireColumn and resizes across instead of down:
ActiveCell.EntireColumn.Resize(, n).Insert Shift:=xlToRight, _
CopyOrigin:=xlFormatFromLeftOrAboveTo put a blank row between every row of data (for printing or for later notes), loop from the bottom up. Working upwards means each insert only shifts rows you have already handled:
Sub InsertBlankRowBetweenRows()
Dim ws As Worksheet
Dim r As Long, lastRow As Long
Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For r = lastRow To 3 Step -1 ' row 1 = headings, row 2 = first data row
ws.Rows(r).Insert Shift:=xlDown
Next r
Application.ScreenUpdating = True
End SubChange Insert to Resize(2).Insert for two blank rows each time. To insert a row whenever the value in column A changes (to separate groups), test If ws.Cells(r, 1).Value <> ws.Cells(r - 1, 1).Value Then ws.Rows(r).Insert in the same bottom-up loop.
What can go wrong
ListObject.ListRows.Add instead; see Excel tables in VBA: working with ListObjects.Formulas that refer to the shifted cells adjust automatically, exactly as when you insert rows by hand. Hard-coded addresses in VBA code or in INDIRECT text do not. If the sheet has data near row 1,048,576, Excel refuses to insert because it would lose that data; usually that means stray formatting at the bottom, which VBA macro to reset the used range and clear excess formatting cleans up.
Ctrl+Z (Mac: Cmd+Z). Save the workbook, or make a copy with VBA macro to save a backup copy with a timestamp in Excel, before you run it on real data.Related: VBA macro to delete blank rows in Excel, For loops: For...Next and For Each.