VBA macros · Utilities
VBA macro to reset the used range and clear excess formatting
Shrink a bloated used range on every sheet by deleting empty rows and columns after the last real data, then make Excel recalculate the used range.
When Ctrl+End (Mac: Fn+Ctrl+Right) jumps to row 900,000 although your data ends at row 500, the sheet has a bloated used range. It usually comes from formatting whole columns or from data that was deleted. The result is a slow, oversized file and scroll bars that are hard to use. This macro deletes every row and column after the last cell that holds a value or formula, on every sheet, and makes Excel forget the old used range.
Option Explicit
Sub ResetUsedRange()
Dim ws As Worksheet
Dim lastCell As Range
Dim lastRow As Long, lastCol As Long
Dim report As String
Dim dummy As Long
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents Then
report = report & vbLf & ws.Name & ": protected, skipped"
Else
Set lastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
If lastCell Is Nothing Then
lastRow = 1
lastCol = 1
Else
lastRow = lastCell.Row
lastCol = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
End If
On Error Resume Next
If lastRow < ws.Rows.Count Then ws.Range(ws.Rows(lastRow + 1), ws.Rows(ws.Rows.Count)).Delete
If lastCol < ws.Columns.Count Then ws.Range(ws.Columns(lastCol + 1), ws.Columns(ws.Columns.Count)).Delete
If Err.Number <> 0 Then report = report & vbLf & ws.Name & ": " & Err.Description
On Error GoTo 0
dummy = ws.UsedRange.Rows.Count ' makes Excel recalculate the used range
report = report & vbLf & ws.Name & ": " & ws.UsedRange.Address(False, False)
End If
Next ws
Application.ScreenUpdating = True
MsgBox "Used ranges now:" & report & vbLf & vbLf & "Save the file to shrink it.", vbInformation
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), pickResetUsedRangeand click Run. - 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
Excel's used range is the rectangle from A1 (or the first used cell) to the furthest cell that has ever held content or formatting since the last save. Clearing cells does not shrink it; only deleting the rows and columns does, followed by a save.
Find("*", ..., SearchDirection:=xlPrevious) starting after A1 wraps backwards to the end of the sheet and returns the last cell that contains a value or formula. Searching xlByRows gives the last row, and a second search xlByColumns gives the last column, because the last row's cell is not necessarily in the last column.
Everything below the last row and to the right of the last column is then deleted in two operations. Formatting, stray borders and phantom content there are gone. Reading ws.UsedRange afterwards forces Excel to recalculate the rectangle; the value is stored in dummy only to make that read happen. The message lists the new used range of every sheet, and the file shrinks once you save it.
Change it
To clean only the active sheet, replace the loop header and its Next with Set ws = ActiveSheet and keep the body. To clear formatting instead of deleting (for example when other sheets refer to those rows by address and you prefer to keep them), replace .Delete with .ClearFormats.
If you want to keep a small margin of formatted empty rows below the data, for example 20 rows for new entries, add the margin before deleting: lastRow = lastRow + 20.
After the clean-up, VBA macro to auto-fit all columns on all sheets in Excel and VBA macro to set all sheets to 100% zoom and select A1 make a good finishing routine. Find the last row and build dynamic ranges in VBA explains why UsedRange is not a reliable way to find the last row in your own code.
What can go wrong
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.Formulas elsewhere that refer to cells in the deleted area, such as =SUM(A2:A5000) when data ends at row 500, adjust to the smaller range or show #REF! if the whole reference was deleted. Whole-column references such as A:A are not affected. If a sheet contains an Excel table whose last rows are empty, deleting through the table can fail; that sheet is listed with the error.
Related: VBA macro to delete blank rows in Excel, VBA macro to auto-fit all columns on all sheets in Excel, Find the last row and build dynamic ranges in VBA.