Want to clean up your Excel spreadsheet by keeping only the header row and deleting everything below it? Excel VBA makes this easy with a one-liner that targets the correct range efficiently. This method preserves your data, handles empty sheets, and works on any number of rows—no manual copying or deleting needed. Just paste the code, run it, and your sheet is tidy in seconds.
What You'll Need
- A working Excel workbook (Excel 2010 or later)
- Basic familiarity with Excel's VBA editor (Access the dropdown in the Developer tab)
- The target worksheet must have at least one data row below the header
Step-by-Step
- Open the VBA Editor:
1. Press
Alt+F11to open the VBA editor (if the Developer tab isn't visible, go to File > Options > Customize Ribbon and check "Developer"). 2. In the Project Explorer window (left panel), double-click the worksheet you want to clean up. If you're unsure which sheet is active, click the sheet tabs to see names. - Paste this code:
Replace all text in the code window with the following:
Range("A1").CurrentRegion.Rows.SpecialCells(xlCellTypeVisible).Delete Shift:=xlUpThis deletes all visible rows (data rows) above the current region (header row) and shifts remaining rows upward. For headers at A1, it safely removes everything below. Note: If your header isn't at A1, adjust the line to `Range("A2").CurrentRegion.Rows.SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp` to start from row 2. - Run the macro:
1. Press
F5while inside the code window. 2. Excel will prompt: "Delete rows in Sheet1" or similar. Click OK. - Verify the result: Check that only the header row remains. If data was at the bottom and shifted down, you may need to trim empty cells later. For large sheets, manually check row 1 to ensure headers are preserved.
Common Mistakes
- Moving headers: The code only works if your header row is continuous (no gaps). If headers span multiple columns or rows (e.g., subheaders), wrap the range in `Range("A1:D1").CurrentRegion` to include all header data.
- Deleting headers: The code deletes rows above the header region. If your sheet only has headers with no data below, the macro will fail silently or delete the header itself. Test on a copy first.
- Hidden columns: If columns beyond your data have hidden headers, the `CurrentRegion` method skips them. Use `Range("A1").CurrentRegion` instead to ensure all header columns are included.
- Empty sheets: The code won't work on blank sheets. Add this check before running:
If Worksheets("Sheet1").Cells(1,1).Value <> "" Then 'Run the deletion code here End If
Tips
- Dynamic headers: To preserve headers even if data expands, use:
With Range("A1").CurrentRegion .Rows.SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp End WithThis works if headers are contiguous but not necessarily column-aligned. - Selective deletion: For partial cleanup (e.g., delete rows from row 10 onward), use:
ActiveSheet.Range("A10:end").Delete Shift:=xlDownReplace "end" with a specific row number or column range. - Backup first: Copy your sheet as a template before running macros on live data. Go to
File > Save As > Excel Macro-Enabled Workbook. - Error handling: Add this to prevent crashes if the header changes:
On Error Resume Next Range("A1").CurrentRegion.Rows.SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp If Err.Number <> 0 Then MsgBox "Header row not contiguous or empty. Adjust code." End If
Troubleshooting
If the macro fails with an error message, check these steps in order:
- Check the sheet name: The macro assumes the sheet is named "Sheet1." Find its actual name in the Project Explorer and update the code to `Worksheets("SheetName")....`.
- Verify data region: If headers are split (e.g., A1:B2), use `Range("A1:B2").CurrentRegion` to include all headers. The original code assumes headers are in one contiguous block.
- Excel corruption: If the macro crashes, close Excel, restart, and open the file again. Corrupted worksheets may prevent range detection.
- Permission denied: If Excel blocks deletion, check for protected sheets. Unprotect the sheet via Review > Unprotect Sheet before running the code.
- Macro disabled: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure macros are enabled.
Frequently Asked Questions
Why does my header row disappear?
Your headers are likely at a non-contiguous location (e.g., split across rows/columns). Adjust the range to include all header cells:
Range("A1:D1").CurrentRegion (replace "D1" with the last header column).
How do I delete all rows except the first one *and* the last empty row?
Use this combined method:
With ActiveSheet
.Rows("2:" & .UsedRange.Rows(.UsedRange.Rows.Count).End(xlUp).Row).Delete Shift:=xlUp
End With
This deletes everything from row 2 up to the last non-empty row.
Will this delete hidden rows?
No. The code only affects visible rows (data + headers). Hidden rows remain unaffected.
How can I make this work for conditional headers (e.g., summary rows)?
Use `SpecialCells(xlCellTypeConstants)` to target only cells with static header text. Example:
For Each cell In Range("A1").CurrentRegion
If cell.Value = "HeaderText" Then cell.EntireRow.Select
Next
Selection.EntireRow.Delete Shift:=xlUp
Replace "HeaderText" with your actual header value.
Why does my code delete rows on the wrong sheet?
The sheet name in the code must match your active sheet. Right-click the sheet tab > "View Code" to open the sheet-specific VBA module, then update the reference to the correct sheet name.
Can I automate this for multiple sheets?
Yes. Replace the sheet name in the code with:
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "ReferenceSheet" Then ' Skip hidden template
On Error Resume Next
ws.Range("A1").CurrentRegion.Rows.SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
If Err.Number <> 0 Then ws.Activate; ws.Name = "DataCleaned_" & ws.Name
End If
Next
This adds a prefix to cleaned sheets and skips a reference sheet.