To rename a named range in Excel VBA, you delete the existing name and create a new one with the Names.Add method, because VBA does not have a direct “rename” command for Name objects. For example, ThisWorkbook.Names("OldName").Delete followed by ThisWorkbook.Names.Add Name:="NewName", RefersTo:="=Sheet1!$A$1:$B$10" accomplishes the rename. This article explains the process in detail, including how to preserve the original range reference, handle scope, and avoid common errors.
What a Named Range Is in Excel VBA
A named range is a defined name that refers to a cell, a range of cells, a formula, or a constant. In Excel’s object model, each name is a Name object contained in the Names collection of a workbook. The Name object has two critical properties: Name (the name itself, e.g., “SalesData”) and RefersTo (the formula that defines what the name points to, e.g., “=Sheet1!$A$1:$B$10”). When you “rename” a range, you are actually changing the Name property of the object, but VBA does not expose a Rename method. The standard approach is to delete the old name and add a new one with the same RefersTo value.
How to Rename a Range with VBA: Step-by-Step
Here is a simple macro that renames a named range. It assumes the workbook is active and the name exists.
Sub RenameNamedRange()
Dim oldName As String
Dim newName As String
Dim rngRef As String
oldName = "OldName"
newName = "NewName"
' Save the reference before deleting
rngRef = ThisWorkbook.Names(oldName).RefersTo
' Delete the old name
ThisWorkbook.Names(oldName).Delete
' Add the new name with the same reference
ThisWorkbook.Names.Add Name:=newName, RefersTo:=rngRef
End Sub
Run this macro from the Visual Basic Editor (press Alt+F11 on Windows, Option+F11 on Mac). Before running, save a copy of your workbook because macros cannot be undone with Ctrl+Z.
Understanding Scope: Workbook vs. Worksheet Names
Named ranges can have workbook scope (visible on all sheets) or worksheet scope (visible only on a specific sheet). The Name property of a Name object includes the scope in its display name. For example, a worksheet-level name on Sheet1 appears as Sheet1!MyName in the Name Manager. When renaming, you must specify the scope correctly.
- Workbook-level: Use
ThisWorkbook.Names("OldName")— the name is unique across the workbook. - Worksheet-level: Use
ThisWorkbook.Worksheets("Sheet1").Names("OldName")— the name is local to that sheet. TheRefersToproperty will include the sheet name.
If you try to access a worksheet-level name using ThisWorkbook.Names("Sheet1!OldName"), it also works because the full name includes the sheet prefix. When renaming, keep the same scope: if the old name was worksheet-level, add the new name to the same worksheet’s Names collection, not the workbook’s.
How to Choose the Right Method: Delete and Add vs. Direct Property Change
Some VBA developers try to change the Name property directly, like ThisWorkbook.Names("OldName").Name = "NewName". This does not work reliably in all versions of Excel; it often throws an error or does nothing. The safe, documented method is delete and add. However, there is a subtlety: when you delete a name, any formulas in the workbook that reference that name will break (they show #REF!). To avoid this, you should update all formulas that use the old name before deleting, or use a different approach: you can rename the name in the Name Manager manually, but in VBA, you must handle formula updates yourself.
If you have many formulas referencing the old name, consider using the Replace method on each formula cell, or use a loop to update formulas. For a single range, the delete-and-add method is fine if you don’t have formulas referencing it.
Common Problems and Solutions When Renaming Ranges
- Error: “Subscript out of range” — This occurs if the old name does not exist. Check with
If Not ThisWorkbook.Names(oldName) Is Nothing(though this check is not straightforward; use a loop orOn Error Resume Next). - Name already exists — If the new name already exists,
Names.Addwill overwrite it by default (in Excel, adding a duplicate name replaces the existing one). To avoid accidental overwrites, check withIf Not Evaluate("ISREF(NewName)")or use a loop. - Formulas break — As mentioned, deleting a name breaks references. To preserve formulas, you must replace the old name in all formulas before deletion. For example, use
Cells.Replace What:="OldName", Replacement:="NewName", LookAt:=xlPart. - Scope confusion — If you rename a worksheet-level name but add it to the workbook, the new name will be workbook-scoped, which may cause conflicts.
Alternative: Using the Name Manager (Manual) vs. VBA
If you only need to rename a range occasionally, the manual Name Manager (Formulas tab > Name Manager) is simpler: select the name, click Edit, type the new name, and press OK. This updates all references automatically. However, for bulk renaming or automated workflows, VBA is necessary. Another alternative is to use a macro to open the Name Manager dialog, but that still requires manual input. For a programmatic rename that also updates formulas, you can use a more comprehensive macro that loops through all cells and replaces the old name string.
Frequently Asked Questions
Can I rename a named range without deleting it?
No, VBA does not provide a direct rename method. You must delete the old name and add a new one. However, you can preserve the reference by storing it in a variable first, as shown above.
How do I rename a range on a specific worksheet?
Use the worksheet’s Names collection. For example, ThisWorkbook.Worksheets("Sheet1").Names("OldLocal").Delete and then ThisWorkbook.Worksheets("Sheet1").Names.Add Name:="NewLocal", RefersTo:="=Sheet1!$A$1".
What happens to formulas that reference the old name?
When you delete the name, formulas that use it will show #NAME? or #REF! errors. To avoid this, replace the old name with the new name in all formulas before deleting. You can use the Replace method across the used range.
Can I rename a range that is used in data validation or conditional formatting?
Yes, but similar to formulas, those features will stop working if the name is deleted. You must update the references manually or via VBA by modifying the Formula1 property of the validation or the Formula property of the conditional formatting rule.
Is there a way to rename a range without breaking references automatically?
No built-in VBA method does this automatically. You must write code to update every reference. The manual Name Manager does it automatically, which is why it’s safer for one-off changes.
Safety Tips and Best Practices
- Always save a copy of your workbook before running any macro that deletes names.
- Test the macro on a dummy workbook first.
- If you are renaming a range that is used in formulas, use a macro that replaces the old name in all cells before deleting. Example:
Cells.Replace What:="OldName", Replacement:="NewName", LookAt:=xlPart. - Check if the new name already exists to avoid overwriting unintended names.
Related
- Battery Bank Calculation for UPS: Step-by-Step Guide
- Casio Ink for Calculator? What to Use & Avoid
- Google Sheets SUM Formula: How to Add Numbers
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel SEQUENCE Macro: Generate Number Series in VBAIf you want a sequence of numbers in Excel, the simplest option is usually the SEQUENCE worksheet function,…
- VBA Code to Open an Excel File from SharePointIn Excel for Microsoft 365 on Windows, use VBA’s Workbooks.Open method with the file’s direct SharePoint URL, provided…
- VBA Find a Character in a StringIn VBA, use the InStr function to find a character in a string: it returns the character’s position,…
- Excel VBA Runtime Error 424: Object RequiredExcel VBA Run-time error ‘424’: Object required means a line of code is using a value where VBA…
- Excel VBA Option Explicit: What It Does and How to Use ItOption Explicit is a VBA module setting that requires you to declare variables before using them. Put it…
- Excel VBA InputBox Integer: Get and Validate Whole NumbersYes—VBA can ask for an integer with an input box, but neither common input-box option automatically guarantees a…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.