In Excel, you can define names for ranges, arrays, constants, and formulas.
Most Excel users are familiar with named ranges. The ability to name a range makes our formulas easier to read and write.
However, the technique of defining names in Excel has special powers that many Excel users are unfamiliar with. You will see names being used at various points in this book to overcome Excel limitations.
This comprehensive chapter will cover all the fundamentals of defined names so that when they are used within formulas later in the book, you are well versed in handling them.
We will cover different methods of creating named ranges, how to use them in formulas, and a few tips and tricks for using names in Excel.
We will show how to find and edit existing names and see a basic example of creating a named formula.
Named Ranges
named-ranges.xlsx
As mentioned, you can define names for many things in Excel including constants, formulas, arrays, and tables (covered separately in the next chapter). However, when using names, we are often referring to named ranges.
This range can be a single cell, multiple cells, or even an entire row or column.
Why Use Named Ranges?
More meaningful references
Using named ranges makes your formulas easier to read and write. Consider a reference to $G$4 vs. CityTax. The reference to CityTax is much simpler and more meaningful than a reference to the grid such as $G$4.
Faster formula creation
Named ranges appear as you write your formulas, making them faster to type and eliminating the need to click between sheets and scroll on a sheet to locate the cell(s) you want to reference.
Easy navigation
Special powers
Named ranges have always had a special power, and this is to link Excel features that cannot work together directly. You will see many examples of this throughout the book.
Define a Named Range with the Name Box
The quickest way to define a named range is to use the Name Box . Let’s see how to do this and then use that name in a formula.
- 1.
Click cell F2.
- 2.
Click in the Name Box and type “Threshold” (Figure 3-2).
- 3.
Press Enter.
- 4.
Repeat the steps to name cell F4 as “Discount”.
You must press Enter when confirming a name in the Name Box. Pressing Tab or clicking the sheet will not work.
You can check that the names have been created successfully by clicking the drop-down list arrow on the Name Box . Figure 3-3 shows both names listed along with an existing [lstProducts] name.
Using Named Ranges in Formulas
Let’s now replace the cell references in the formula with references to the named ranges.
As you type the name, a list appears that shows names of functions and ranges. You can identify a named range by the icon to the left of the name.
If you select a range that has been named from within a formula, the named range is returned. However, the grid reference is still understood, and any existing formulas or features that are using the grid reference will continue to work.
You can also insert a named range using a list on the Ribbon. This is especially helpful if you are unsure on the name that was assigned.
- 1.
Click Formulas ➤ Use in Formula.
- 2.
Click the name in the list (Figure 3-5).
Another alternative is to press the keyboard shortcut F3. This opens the Paste Name window (Figure 3-6). Select the name to paste and click OK.
Rules for Defining Names
A name can include numbers, but a name cannot begin with a number.
You cannot use spaces in a name.
Other invalid characters that cannot be used in a name include the /, &, %, and $. The underscore “_” and period “. ” characters are both allowed.
You cannot use a name that has already been assigned, for example, a cell address such as D2 or existing names of ranges, formulas, macros, etc.
Names are not case-sensitive. Therefore, the names rngSales, rngsales, and RNGSALES are all the same.
If you try to use a name that already exists, you are taken to that name. Quickly jumping to a range or macro using this method is a cool feature of the Name Box.
Tips for Defining Names
Use an underscore “_”, period “. ”, or camel casing as an alternative to a space. For example, I like the camel casing approach and use names such as thisYear. A capital letter is used to denote a word change.
Use a prefix for your names. You can use whatever prefix you like, just be consistent. Common prefixes include rng for range, fx for formula, lst for list, and tbl for table (discussed in the next chapter).
You can also use a consistent suffix to a name.
You want your names to be quick to find and easy to understand. These are only ideas, and you should do what works for you.
Named Ranges with Multiple Cells
Named ranges are not restricted to single cell ranges. A named range can be multiple contiguous cells, a selection of non-contiguous cells, or even an entire row or column.
- 1.
Select range B2:B8.
- 2.
Type “rngTotal” in the Name Box (Figure 3-9).
- 3.
Press Enter.
Figure 3-10 shows the completed formula. It has spilled to the other cells in range C2:C8, and this is identified by the blue border.
If you are using a non-dynamic array–enabled version of Excel, you will need to fill the formula down the range. It will not spill.
The IF function expects a single value, but multiple values were provided to it when using the [rngTotal] name, so the formula assumed the use of the value in the same row on the grid. This behavior is known as implicit intersection.
As an advanced Excel formula user, it is important to understand this behavior so that we can recognize it on spreadsheets. And there is a case that the formula is more meaningful when using the named range.
However, despite demonstrating this technique, it is not one that I would encourage. A better approach would be to format the range as a table and use the qualities of the table for the formula. Tables are discussed in the next chapter.
Named Range for a Drop-Down List
A common scenario for defining a multicell named range is when using it for the source of a drop-down list .
On the [Sales] worksheet, there is a SUMIFS function (covered later in the book) in cell F3 returning the total sales for the product entered in cell E3 (Figure 3-12).
- 1.
Click cell E3.
- 2.
Click Data ➤ Data Validation.
- 3.
From the Settings tab, select List from the Allow list.
- 4.
In the Source field, type "=lstProducts" (Figure 3-13). Click OK.
You can also use the F3 shortcut when in the Source field to open the Paste Name window.
The drop-down list can then be used to easily select the required product (Figure 3-14).
Define a Dynamic Named Range
The products’ drop-down list works great, but it is not dynamic. If products are added to the end or removed from the list on the [Lists] sheet, the [rngProducts] name will not automatically adjust in size.
Sure, there are clever techniques around this issue, such as inserting cells in the middle of the range. This forces the named range to adjust its height.
However, this is an advanced Excel formulas book, and it would be great if the named range automatically changed height when products are added or removed.
To do this, we will use the brilliant INDEX function as the source for the named range.
You do not get any assistance when entering a formula into an Excel window, so it is good practice to enter it into a cell first and then copy it into the required window.
The first INDEX function returns the address of the cell in row 2 of column A. Row 2 is entered as a constant value. In the second INDEX function, the COUNTA function is used to count the number of non-blank cells in column A. In this example, it successfully returns the address of the final non-blank cell.
The INDEX function is covered in depth in Chapter 11. If you are new to this function, you can jump to that chapter for an explanation on how this formula works.
- 1.
Copy the formula from the Formula Bar.
- 2.
Click Formulas ➤ Name Manager.
- 3.
Select the lstProducts name and click Edit (Figure 3-16).
- 4.
Delete the contents of the Refers to field and paste in the formula (Figure 3-17). Click OK.
When new products are added to the end of the list, they are automatically included in the drop-down list.
Dynamic named ranges can also be created using a combination of tables and named ranges. We show an example of this in the next chapter.
Define Names with Create from Selection
Another way to create named ranges is to use the Create from Selection option. This is great for creating multiple named ranges at once.
In this example, we have the data shown in Figure 3-20. We have three levels that each apply a different discount.
The discount’s range is on the same sheet to simplify the example. Named ranges default to having a workbook scope, so they are just as easy to access on other worksheets. That is another great strength of using them.
- 1.
Select range F3:G5.
- 2.
Click Formulas ➤ Create from Selection or press Ctrl + Shift F3.
- 3.
The Create Names from Selection window appears and guesses where the values you want to use for the names are (Figure 3-21). Very often, this is correct, but you should check. Left column is selected in our example. This is correct. Click OK.
- 4.
The following SWITCH function can be entered into cell D2 and filled down (Figure 3-22):
The SWITCH function checks the level in column B against its list of values and returns the matching discount to be applied to the amount.
Scope of a Defined Name
Workbook: The name is available to all sheets of a workbook. The name is unique across all sheets of the workbook and can easily be referenced using its name.
Worksheet: The name is only unique and easily accessible within that specific worksheet.
The default scope of a named range is the workbook level, and all the names created so far in this chapter have had that scope.
You can reference names with a worksheet scope from other sheets by preceding its name with the sheet name, for example, Sheet3!TotalSales. However, this is not encouraged, and the name should have been given workbook scope.
Let’s look at how to create a named range with worksheet scope.
- 1.
Click cell E3.
- 2.
Click Formulas ➤ Define Name.
- 3.
In the New Name window (Figure 3-24), change the Name to “rngTarget”.
- 4.
Change the Scope using the drop-down list provided from Workbook to Scope and Constants (this is the name of the worksheet). Click OK.
It is worth noting the behavior of names when sheets are copied in Excel. The behavior is often undesirable causing confusion and cluttered workbooks. Therefore, it is important to be familiar and understanding of what happens when sheets are copied.
When a worksheet with a workbook scope named range is duplicated within the same workbook, a second name with the same name but worksheet level scope is created.
Figure 3-27 shows two [Discount] and [Threshold] names caused by copying the [Named Ranges] worksheet. These new names have worksheet scope. This is shown in the [Scope] column of the Name Manager window. The name of the duplicated worksheet [Named Ranges (2)] is shown.
When a worksheet with a named range is copied to another Excel workbook, the name is copied along with the sheet. This applies to both workbook and worksheet scope named ranges.
This is generally a good action as you probably require the named range in that workbook too. However, it is important to be aware of this behavior; otherwise, Excel files can get cluttered quickly with redundant names.
When a workbook contains a named formula with workbook scope, and a worksheet is copied to another workbook, the named formula is also copied to the other workbook. However, the named formula links back to the workbook from which it was copied.
Figure 3-28 shows the [lstproducts] name that we created using the INDEX formula. A worksheet was copied to another workbook, and this named formula was copied with it, but links back to the [named-ranges.xlsx] workbook.
This behavior is bad. Be careful of external links in a workbook that are generated by users copying sheets to the workbook. This only happens with workbook scope names.
In Chapter 15, when LAMBDA functions are discussed, we will use the technique of copying ranges that contain formulas as a simple method to copy our custom named formulas to other workbooks.
Define a Named Constant
In addition to naming ranges, it is also possible to name constants. As its name suggests, a constant is a value that does not change, unless the named constant is edited.
So, it is not necessary to have a value on the worksheet to be able to assign a name to it.
This can help to free your worksheet from unnecessary clutter while enabling the ability to use meaningful names for values.
- 1.
Click Formulas ➤ Define Name.
- 2.
In the Name field, enter “cstDiscount” (Figure 3-29).
- 3.
In the Refers to field, enter “=0.15”. Click OK.
The prefix cst has been used in the name to easily identify it as a constant. Remember, prefixes are optional, and you can use whatever you want. Other common prefixes for constants include const and k.
This named constant can now be used in the formula instead of typing 15% (Figure 3-30).
This constant has workbook scope. So, if the constant were changed to a different percentage in the future, all formulas that reference it would update.
Finding Named Ranges
Using the Name Box
The Name Box provides a quick no-nonsense method of jumping to a named range. Click the list arrow on the end of the Name Box to list the named ranges in the current scope. Click a name to jump to that selected range.
cstDiscount: It only lists named ranges, so named constants will not appear in the list.
lstProducts: This name is the dynamic named range created earlier using the INDEX formula. Names that use a formula as their source are not shown in the Name Box list.
rngTarget: This name was created from the [Scope and Constants] worksheet with worksheet-level scope. Therefore, it is not visible from the Name Box list on the [Lists] sheet.
You can also navigate to these names by typing their names into the Name Box. This includes those that use a formula as their source such as [lstProducts].
This applies to grid references too. So, if you type D10000 into the Name Box and press Enter, you are taken to cell D10000. If you type A1 and press Enter, you are taken back to A1.
You can even take this further and enter ranges such as B2:D10 to select that range. You can include the sheet name in the range such as Sheet3!B2:D10. You can even enter R1C1 references such as R1C19 to jump to row 1 column 19.
The Go To window
The Go To window provides another fast way to jump to a named range from anywhere within its scope.
Press F5 on the keyboard to open the Go To window (Figure 3-32). Select the name you want to go to and click OK.
You can also open the Go To window by pressing the Ctrl + G shortcut or by clicking Home ➤ Find & Select ➤ Go To.
Just like the Name Box, the Go To window only lists the names that are from a range. So, names that use a formula for their source, such as [lstProducts], are not listed.
Once you have followed a name, you can return to the previously active range quickly by pressing F5 and then Enter.
Relative Named Ranges
Named ranges are absolute by default. And this makes sense as you are defining a specific area in the workbook.
However, it is possible to create relative named ranges in Excel. Let’s see an example of how and why you would want to do this.
- 1.
Click cell C3.
- 2.
Click Formulas ➤ Define Name.
- 3.
Enter “PrevMonth” as the Name for the range (Figure 3-35).
- 4.
Change the Scope to the worksheet. The worksheet we are using is named Relative Named Range . I do not want this name appearing on other sheets.
- 5.
Enter a descriptive comment such as “Refers to the cell one row above and one column to the left.” In this example, I do not think the name is descriptive enough and a comment is required.
- 6.
In the Refers to field, change the cell on the end of the reference from $C$3 to B2. Click OK.
- 1.
From cell C3, click Formulas ➤ Define Name.
- 2.
Enter “CurrentMonth” for the Name (Figure 3-36).
- 3.
Set the Scope to the current worksheet Relative Named Range .
- 4.
Enter a descriptive comment such as “Refers to the cell in the current row and one column to the left.”
- 5.
In the Refers to field, change the cell on the end of the reference from $C$3 to B3. Click OK.
Named Formulas
In addition to naming ranges and constants in Excel, you can also create named formulas. We have actually already done an example of this – the dynamic named range using the INDEX function.
Now, that formula returned a range, so it was really still a named range. Let’s see an example of a named formula that returns a value.
We want to return a count of the products so that we can use that result within other formulas in Excel.
- 1.
Click any cell of the worksheet and enter the following formula. It is easier to write a formula in a cell first.
- 2.
Copy the formula text.
- 3.
Click Formulas ➤ Define Name.
- 4.
Enter “fCountProducts” as the Name for the formula (Figure 3-39).
- 5.
Paste the formula into the Refers to field and click OK.
- 6.
The formula can now be deleted from the cell it was written in.
This name can now be used in a cell, or within another formula, to always return the count of products.
We will see more advanced examples of naming formulas in Chapter 15, when we explore the LET and LAMBDA functions of Excel.
Managing Names
The Name Manager in Excel makes it super simple to find, review, edit, and delete your names.
To open the Name Manager, click Formulas ➤ Name Manager or press Ctrl + F3.
The Name Manager lists all the names in the workbook (Figure 3-41). This includes the constants, tables, and named formulas.
It displays the current value (if possible) and shows what the name refers to and the scope of the name. Everything you need to know about a name is here. It truly is the name manager.
Edit a Name
- 1.From the Name Manager, select the “Threshold” name and click Edit at the top of the window (Figure 3-41).
- 2.
Edit the Name to read “rngThreshold” (Figure 3-42). Click OK.
Note You can also edit the Refers to and Comment fields, but note that the Scope of a name cannot be changed.
All formulas that reference the name [Threshold] will automatically update to reference [rngThreshold].
Filtering Names
The Name Manager provides an excellent filter – very useful if you have many names.
For example, you can filter for only names with workbook scope, or only the defined names and ignore the tables, or filter to only show names that have errors.
Figure 3-43 shows three of the names displaying the #REF! error. This is caused because the data that the names were using has been removed.
Print a List of the Names
You can also print a list of the names to the worksheet. This feature will only print names that have workbook scope.
- 1.
Click cell A2.
- 2.
Click Formulas ➤ Use in Formula ➤ Paste Names or press F3.
- 3.
Click the Paste List button (Figure 3-45).
This list is not dynamic and therefore will not update when names are added, removed, or changed.
Apply Names to Existing Formulas
If you define names after using them within formulas, there is a neat way to update these formulas with the names.
Let’s use an example with the formula we used in the “Define Names with Create from Selection” section of this chapter.
- 1.
Click Formulas ➤ list arrow next to Define Name ➤ Apply Names (Figure 3-48).
- 2.
Click the Bronze, Gold, and Silver names. Click OK (Figure 3-49).
Note The names will be applied to all formulas where they can match the name and grid reference in the entire worksheet.
The Ignore Relative/Absolute option ensures that the name and grid are an exact match. If the grid reference is relative, but the named range is absolute, it will be ignored.
This is preferable, so we leave it checked. Changing relative references to absolute and vice versa at large scale does not sound good.
Summary
This chapter has provided a comprehensive understanding of defining names in Excel. We have covered the creation, application, and best practice of named ranges, formulas, and constants. We also saw how to work with existing names in an Excel workbook and how to find and manage the different types of names.
We will see names throughout this book. They will be used in different moments to simplify our formulas and to achieve tasks that would be difficult or not even possible without them.
In the next chapter, we will explore tables in Excel. Tables provide a way for us to work with structured data easier. They enable us to work beyond the grid and make referencing and analyzing Excel data more meaningful and simpler. Tables have been mentioned a few times in this chapter as they are also a named item and appear in the Name Manager. This makes tables a logical chapter to succeed the topic of defined names.