Excel VBA Resize Named Range: Step-by-Step Guide
In this article
You can resize a named range in Excel VBA by reassigning its RefersTo property to a new range address. The shortest working method is: ThisWorkbook.Names("MyRange").RefersTo = "=Sheet1!$A$1:$D$20". This changes the range the name points to without deleting and recreating the name, so any formulas that use the name keep working. The steps below show how to do this reliably, including how to build the new address dynamically and how to avoid the errors that trip people up.
What You'll Need
- Excel desktop (Windows or Mac) with macros enabled. The VBA editor is not available in Excel for the web.
- A workbook that already contains at least one defined name, or permission to create one.
- Basic familiarity with the VBA editor: press Alt+F11 (Windows) or Fn+Option+F11 (Mac) to open it.
- Macro security set to allow macros, or the workbook saved as .xlsm and opened with macros enabled.
Step-by-Step
- Open the VBA editor and insert a module. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac). In the Project Explorer, right-click your workbook, choose Insert, then Module. All code below goes in that module.
- Confirm the exact name you want to resize. On the worksheet, open Formulas > Name Manager (labels can differ slightly by version and region). Note the name exactly, including any underscores. Names are not case-sensitive in VBA, but spelling must match.
- Write a minimal test macro. Start with a hard-coded address so you can confirm the name exists and the workbook reference is correct:
Replace MyRange with your name and Sheet1 with your sheet's actual tab name. Run it with F5.Sub ResizeNamedRange() ThisWorkbook.Names("MyRange").RefersTo = "=Sheet1!$A$1:$D$20" End Sub - Check the result in Name Manager. Reopen Formulas > Name Manager and confirm the Refers To box now shows the new address. If it does not, the name string or sheet name is wrong; see Troubleshooting.
- Build the address dynamically instead of hard-coding it. For a range that should grow with your data, use the worksheet's UsedRange or CurrentRegion to calculate the last row and column, then construct the address string:
This approach handles a data block that starts at A1. If your data starts elsewhere, adjust the starting cell in the Range call.Sub ResizeToUsedRange() Dim ws As Worksheet Dim lastRow As Long, lastCol As Long Dim addr As String Set ws = ThisWorkbook.Worksheets("Sheet1") lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column addr = "=" & ws.Name & "!" & ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, lastCol)).Address ThisWorkbook.Names("MyRange").RefersTo = addr End Sub - Quote sheet names that contain spaces. If the tab is called Sales Data, the address must wrap the sheet name in apostrophes:
="'Sales Data'!$A$1:$D$20". The dynamic example above does not add these automatically, so add them when needed, for example by concatenating"'" & ws.Name & "'". - Decide between workbook-level and sheet-level names. ThisWorkbook.Names refers to workbook-scoped names. If the name was created at worksheet scope, reference it through the sheet:
Worksheets("Sheet1").Names("MyRange").RefersTo = .... Using the wrong scope raises a "Subscript out of range" error. - Save and test dependent formulas. Close the editor, change a value inside the new range, and confirm formulas that use the name update. If a chart series or data validation list uses the name, check those too; they follow the name automatically once RefersTo changes.
Common Mistakes
- Omitting the equals sign. RefersTo expects a string that begins with =, such as "=Sheet1!$A$1:$D$20". Without it, Excel may reject the assignment or store an invalid reference.
- Forgetting apostrophes around sheet names with spaces or special characters. This is the most frequent cause of runtime error 1004.
- Using the wrong name scope. A sheet-level name is not visible through ThisWorkbook.Names, and vice versa.
- Hard-coding a sheet name that later changes. Renaming the tab breaks the address string. Build the address from the worksheet object as shown in step 5.
- Assuming the name exists. If the name was deleted or misspelled, the code errors. Add a check with
On Error Resume Nextaround the lookup, or test with a helper function before assigning. - Resizing to an empty range. If the last-row calculation returns 0 because the column is empty, the address becomes invalid. Guard against zero before assigning.
Tips
- Use
RefersToRangeto read the current address before changing it, which helps with logging or undo logic:Debug.Print ThisWorkbook.Names("MyRange").RefersToRange.Address. - For a name that should always match a table, point it at the table's DataBodyRange instead of calculating rows manually.
- Keep the resizing macro in a standard module and call it from a button or the Workbook_Open event if the range should refresh each time the file opens.
- Test on a copy of the workbook first. Changing a name affects every formula, chart and validation list that uses it.
- If you need to change the name itself rather than its address, use the Name property, but note that formulas referring to the old name will break.
Troubleshooting
If you get runtime error 1004 "Method 'RefersTo' of object 'Name' failed", check three things in order: the name exists in the scope you are using, the address string starts with =, and any sheet name with spaces is wrapped in apostrophes. If the macro runs but the range does not change, confirm you are editing the same workbook that is active; ThisWorkbook always refers to the workbook containing the code, which may not be the one on screen. If a formula using the name shows #REF!, the new address likely points to a deleted sheet or an invalid cell reference; reopen Name Manager and correct the Refers To box manually. If the name is not found, verify its scope in Name Manager; sheet-level names appear under their sheet heading. Finally, if changes do not persist after closing, make sure the workbook is saved as .xlsm and that macros were enabled when you ran the code.
Frequently Asked Questions
Can I resize a named range without VBA?
Yes. Open Formulas > Name Manager, select the name, and edit the Refers To box directly. VBA is only needed when the resize should happen automatically or repeatedly.
Does resizing a named range break formulas that use it?
No. Formulas, charts and data validation lists that refer to the name follow the new address automatically. Only formulas that hard-code the old cell addresses are unaffected by the change.
What is the difference between RefersTo and RefersToRange?
RefersTo is the text string you assign, such as "=Sheet1!$A$1:$D$20". RefersToRange is a read-only Range object that returns the cells the name currently points to.
Why does my macro resize the wrong name?
Most likely the name exists at both workbook and worksheet scope, and the code is picking up the one you did not intend. Check the scope in Name Manager and reference the correct collection.
Can I resize a name to a non-contiguous range?
Yes, but the address string must use the union syntax with commas, for example "=Sheet1!$A$1:$A$10,Sheet1!$C$1:$C$10". The same quoting rules apply.
Does this work in Excel for the web?
No. Excel for the web does not include the VBA editor. Use the desktop app to run or edit the macro, then the resized name will appear in the web version once the file is saved.