Excel VBA UsedRange: How to Use It Correctly
In this article
UsedRange is the rectangle of cells Excel considers "in use" on a worksheet, and you reach it in VBA with ActiveSheet.UsedRange or Worksheets("Sheet1").UsedRange. The direct answer to the usual question is: it is not the same as the data you can see, and it is not reliable until you understand how Excel builds it. Excel tracks the furthest row and column that have ever held a value, a formula, or formatting, and it keeps that boundary even after you delete the contents. So a sheet with three rows of data can report a UsedRange of A1:Z5000 if someone once typed in Z5000 and cleared it. The fix is to save the workbook, which forces Excel to recalculate the used range, or to delete the entire rows and columns beyond your data rather than just clearing them.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer, or Excel > Preferences > Ribbon & Toolbar on Mac). Labels vary slightly by version and region.
- A workbook saved as .xlsm or .xlsb if you want to keep your macro; .xlsx discards VBA code.
- Basic familiarity with the VBA editor: press Alt+F11 on Windows, or Option+F11 on Mac.
- A test copy of any workbook you plan to run cleanup code on, because deleting rows cannot be undone after saving.
Step-by-Step
- Open the VBA editor and insert a module. Press Alt+F11 (Option+F11 on Mac), then in the Project Explorer right-click your workbook and choose Insert > Module. Code placed in a standard module can be run from anywhere; code in a sheet module only fires on events.
- Read the current used range. Type a short procedure such as
Sub ShowUsed()thenMsgBox ActiveSheet.UsedRange.AddressthenEnd Sub. Press F5. The message box shows the address, for example $A$1:$F$240. This is your baseline before you change anything. - Get the last row and column as numbers. Use
ActiveSheet.UsedRange.Rows.CountandActiveSheet.UsedRange.Columns.Count. Be aware these count from the first cell of the used range, not from row 1 of the sheet. If the used range starts at row 5 and is 20 rows tall, Rows.Count returns 20, not 24. To get the true last row, addActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1. - Loop only over the used range. Instead of looping to row 1,048,576, set a range variable:
Dim r As Range: Set r = ActiveSheet.UsedRange, then loopFor Each cell In r. This keeps macros fast on large sheets and avoids touching empty cells. - Reset a bloated used range. If the address looks far larger than your data, first save the workbook and re-check. If it is still wrong, select the first empty row below your data, press Ctrl+Shift+Down (Cmd+Shift+Down on Mac) to select to the bottom, then right-click the row headers and choose Delete. Do the same for columns to the right using Ctrl+Shift+Right. Save again.
- Force a recalculation in code. You can call
ActiveSheet.UsedRangeon its own line without assigning it; this is a known trick that makes Excel rebuild the used range. Follow it withActiveWorkbook.Savefor the change to stick. - Test on a copy before trusting it. Run your macro against a duplicate file, compare the reported address before and after, and confirm no data disappeared. Only then run it on the original.
Common Mistakes
The most frequent error is assuming UsedRange equals the data region. It does not: formatting alone, such as a fill colour applied to a whole column, can extend it. Another is using Rows.Count from UsedRange as if it were the last row of the sheet, which produces off-by-one and offset errors when the range does not start at A1. People also clear contents with the Delete key and expect the used range to shrink; it will not until the rows or columns themselves are deleted and the file is saved. Finally, code written against ActiveSheet breaks the moment the user clicks another tab, so name the sheet explicitly in production macros.
Tips
Use Worksheet.UsedRange rather than Selection or ActiveCell so your macro does not depend on where the cursor happens to be. For a more predictable data boundary, consider Cells(Rows.Count, 1).End(xlUp).Row, which finds the last filled cell in a specific column and ignores stray formatting elsewhere. Store the used range in a variable once and reuse it rather than reading the property repeatedly inside a loop. If you work with tables, refer to the ListObject instead, since a table's range is defined by its headers and does not drift. Keep a backup copy of any workbook before running range-cleanup code, and remember that deleting rows removes anything in them, including comments and hidden values.
Troubleshooting
If the reported address never changes after deleting rows, save and close the workbook, reopen it, and check again; Excel recalculates the used range on open. If your macro reports a huge range on a brand-new sheet, look for formatting applied to entire columns or a stray value far down; select the suspect area and inspect it with Ctrl+End, which jumps to the last used cell. If code errors with "Object required", confirm the sheet name exists exactly, including spaces. If the macro runs but skips rows, check whether your loop starts at the used range's first row rather than row 1. If results differ between Windows and Mac, verify you are using the same file and that references are set the same way; shortcut keys differ, so run macros from the Developer tab or the Macro dialog rather than relying on a keyboard shortcut. When nothing works, rebuild the sheet by copying your data into a fresh worksheet, which guarantees a clean used range.
Frequently Asked Questions
Does UsedRange include formatted but empty cells?
Yes. Any cell that has ever held a value or had formatting applied is counted, which is why the range can be much larger than your visible data.
Why does UsedRange start at a row other than 1?
Because the first used cell is not in row 1. The range begins at the top-left cell Excel considers in use, so offsets must be calculated from that starting row and column.
How do I shrink UsedRange permanently?
Delete the entire empty rows and columns beyond your data, then save the workbook. Clearing contents alone is not enough.
Is UsedRange the same as CurrentRegion?
No. CurrentRegion is the block of cells around a given cell bounded by blank rows and columns; UsedRange covers everything Excel tracks on the sheet.
Can UsedRange slow down my macro?
It can if the range is bloated, because you loop over far more cells than needed. Resetting the range or looping over a specific column usually fixes the slowdown.
Does deleting rows affect my formulas?
Yes. Deleting rows removes their contents and can break references. Always test on a copy and check formulas afterwards.