ThisWorkbook.Names.Add Name:="SalesData", RefersTo:=Worksheets("Data").Range("A2:D100") creates a workbook-level name that every sheet can use, in formulas such as =SUM(SalesData) and in VBA as Range("SalesData"). To name the current selection, use RefersTo:=Selection.
Adding the name through a sheet, as in ActiveSheet.Names.Add, makes it a sheet-level name. Names can’t contain spaces, can’t start with a number and can’t look like a cell address such as A1 or Q4; an invalid name stops the code with error 1004.
To change what an existing name covers, set its RefersTo to a new address, as in ThisWorkbook.Names("SalesData").RefersTo = "=Data!$A$2:$D$" & lastRow, and run it each time the data grows. Or create a name that updates itself:
ThisWorkbook.Names.Add Name:="SalesData", _
RefersTo:="=Data!$A$2:INDEX(Data!$A:$A,COUNTA(Data!$A:$A))"
This version expands as rows are added, with no macro needed, as long as column A has no blanks. A Table does the same more reliably: refer to Range("Table1[Amount]"), and the reference always covers the whole column.
To create a named range using VBA, you need to use the “Names” property with the “Add” method. In the Add method, you have arguments to define the name that you wish to give to the range and specify the address of the range (make sure to use the dollar sign with the address to freeze the range).
Create a Name Range using VBA
- Define the workbook where you want to create the named range.
- Use the Names property and then the Add method.
- Specify the name in the “Name” argument.
- Refer to the range using the “RefersTo” argument.

In the above example, you have the active workbook, and then by using the “Names” property with the “Add” method, you have defined the name of the range, and in the end, the address of the range that you want to use.
As I said earlier, in the range address, you need to use the $ sign to freeze the address. You can also use ThisWorkbook to refer to the workbook where you are writing the code, or you can refer to a different workbook using the workbook object.
Here’s an example of the same code that you can copy, which creates a named range “myRange” for the range A1:A10 of Sheet1:
Sub create_named_range()
ActiveWorkbook.Names.Add Name:="myRange", RefersTo:="=Sheet1!$A$1:$A$10"
End Sub
A name must start with a letter, an underscore or a backslash, can’t have spaces, and can’t look like a cell reference (like “A1” or “Tax2025”). If the name breaks these rules, you get run-time error 1004.
VBA to Create Named Range from Selection
You can also use the selection property to create a named range from the selection. Consider the following code.
ActiveSheet.Names.Add Name:="myRangeName", RefersTo:=Selection
When you use ActiveSheet.Names.Add, the name is created at the worksheet level, so you can only use it on that sheet without the sheet name in front. To create a name you can use in the whole workbook, use ActiveWorkbook.Names.Add instead, or simply Selection.Name = “myRangeName”.
And in the following code, you have an input box with which you can enter the name that you want to give to the named range.
Sub vba_named_range() Dim iName As String iName = InputBox("Enter Name for the Selection.") ActiveSheet.Names.Add Name:=iName, RefersTo:=Selection End Sub
Resizing a Named Range using VBA (Dynamic Named Range)
To resize a named range that’s already there in the worksheet, you need to use the Resize property and tell VBA how many rows and columns the range should have. Consider the following code, which expands the named range “myRange”, which has cell A1 as its range initially, and resizes it to column M and row 11.

Sub vba_named_range() Dim iRow As Long Dim iColumn As Long iRow = ActiveSheet.Range("A1").End(xlDown).Row iColumn = ActiveSheet.Range("A1").End(xlToRight).Column ActiveSheet.Range("myRange") _ .Resize(iRow, iColumn).Name = "myRange" End Sub
I have split this into three parts to help you understand it. Now, let’s get into it.

- In the FIRST part, you have declared variables to store the row and column count.
- In the SECOND part, you have used the “END” method with the range to get the last row and column and store them in the variables.
- In the THIRD part, you have used the Resize property with the named range “myRange”, with the row and column numbers that you have in the variables.

When you run this code, it resizes the old range according to the data you have and makes it a dynamic named range. Whenever you need to update it, you can run the code and resize the existing named range.
This code uses End(xlDown) and End(xlToRight), so it stops at the first blank cell in column A or row 1. If your data has blank cells in between, find the last row from the bottom with Cells(Rows.Count, “A”).End(xlUp).Row instead.
Create a Dynamic Named Range with a Formula
With the code above, you need to run the macro again every time the data changes. Another way is to give the name a formula in RefersTo, so the range grows and shrinks on its own. The following code creates the name “myData” for column A, from A1 down to the last value:
Sub create_dynamic_named_range()
ActiveWorkbook.Names.Add Name:="myData", _
RefersTo:="=Sheet1!$A$1:INDEX(Sheet1!$A:$A,COUNTA(Sheet1!$A:$A))"
End Sub
COUNTA counts the values in column A, and INDEX returns the last of those cells, so the range always ends at the last value. This works as long as column A has no blank cells in between.
Use, Change and Delete a Named Range
Once you have a named range, you can use it in the code just like a range address. The following code enters a value in all the cells of “myRange”, changes the range it refers to, and in the end, deletes the name (the cells and their values stay as they are):
Sub use_change_delete_name()
Range("myRange").Value = 100
ActiveWorkbook.Names("myRange").RefersTo = "=Sheet1!$A$1:$B$20"
ActiveWorkbook.Names("myRange").Delete
End Sub
And to see all the names in the workbook with the ranges they refer to, loop through the Names collection and print them in the Immediate Window:
Sub list_all_names()
Dim nm As Name
For Each nm In ActiveWorkbook.Names
Debug.Print nm.Name, nm.RefersTo
Next nm
End Sub
Frequently Asked Questions
How do I check if a named range exists?
Try to refer to the name with On Error Resume Next and check if you got it. For example, set a Name variable to ActiveWorkbook.Names(“myRange”); if it’s still Nothing afterwards, the name doesn’t exist.
What is the difference between workbook and worksheet level names?
A workbook-level name works on every sheet of the workbook. A worksheet-level name works only on its own sheet, and from other sheets, you need the sheet name in front, like Sheet1!myRange. You can see the scope of each name in the Name Manager (Formulas ➜ Name Manager).
Does deleting a named range delete the data?
No. It only removes the name. But formulas that use that name show the #NAME? error afterwards, so update them first.
Written by
Puneet Gogia
Microsoft MVP — Excel · Founder, Excel Champs
Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.