Insert Blank Rows Above SKU Records in Excel with VBA
Question details
Insert a blank row above each record identified by a non-empty SKU cell.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A large Excel worksheet or imported CSV contains SKU records that need a blank row before each record.
- Observed behavior
- Each qualifying SKU record should have a blank row directly above it.
Confirm that SKUs are in column A and that row 1 is a header; the macro starts checking at row 2. Open any CSV in desktop Excel and save it as an Excel Macro-Enabled Workbook (.xlsm) before adding the macro.
Run a VBA macro that inserts rows from the bottom up
Use this approach to add rows above SKU records without disrupting the positions of rows that have not yet been checked.
The macro checks column A from the last used SKU row up to row 2. It ignores blank cells, whitespace-only cells, and cells containing errors.
Processing upward prevents inserted rows from shifting the remaining records. Screen updating and calculation are restored to their previous settings, even if the macro encounters an error.
In desktop Excel, open the workbook and choose Developer > Visual Basic. On Windows, you can also press Alt+F11.
In the VBA editor, choose Insert > Module and paste this code: Sub InsertBlankRowsAboveSKUs() Dim ws As Worksheet Dim lastRow As Long, r As Long Dim oldCalc As XlCalculation Dim oldScreen As Boolean Dim errorMessage As String Set ws = ActiveSheet oldCalc = Application.Calculation oldScreen = Application.ScreenUpdating On Error GoTo Failed Application.ScreenUpdating = False Application.Calculation = xlCalculationManual lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row For r = lastRow To 2 Step -1 If Not IsError(ws.Cells(r, "A").Value2) Then If Len(Trim$(CStr(ws.Cells(r, "A").Value2))) > 0 Then ws.Rows(r).Insert End If End If Next r GoTo CleanUp Failed: errorMessage = Err.Description CleanUp: On Error Resume Next Application.Calculation = oldCalc Application.ScreenUpdating = oldScreen On Error GoTo 0 If Len(errorMessage) > 0 Then MsgBox "The macro stopped: " & errorMessage, vbExclamation End If End Sub
In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). An .xlsx or CSV file cannot retain the VBA macro.
Select the worksheet containing the SKUs, then choose Developer > Macros, select InsertBlankRowsAboveSKUs, and click Run. Enable macros if Excel prompts you and you trust the workbook.
Review column A and confirm that each qualifying record has a blank row above it. If you need a CSV, use File > Save As to create a separate CSV copy; keep the .xlsm file to retain the macro.

Work with SKU worksheets in WPS Spreadsheet
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and CSVs in WPS Spreadsheet for a straightforward transition, though it does not fix or guarantee support for this Excel VBA macro.

Frequently Asked Questions
Why does the macro process rows from the bottom up?
Inserting a row shifts rows below it. Starting at the last SKU row and moving upward keeps the positions of the remaining rows unchanged.
How do I run the macro again?
Open the .xlsm workbook, select the target worksheet, and choose Developer > Macros. Select InsertBlankRowsAboveSKUs and click Run. Running it again will insert another blank row above every qualifying SKU.
Can I keep the macro in a CSV file?
No. Save the workbook as an .xlsm file to retain the macro. If you also need a CSV, create a separate CSV copy through File > Save As.
What if my SKUs are not in column A?
In the VBA editor, replace both references to "A" in the macro with the correct column letter. Save the .xlsm workbook before running the revised macro.
Why was a SKU row skipped?
The macro starts at row 2 and skips cells in column A that are blank, contain only whitespace, or contain an Excel error. Check the SKU cell and confirm that the correct worksheet is selected.




