Range Names
Home Up Past Meetings Notes Tips Directory Links of Interest Site Map FAQs

Up ] Puzzler: How many ways can you define a Range Name?
        by Stephen M. Rasey - October 2, 2001

bullet

Rules:
-  Range Names cannot contain Spaces or any punctuation characters ( the Underscore is allowed).
-  Range Names can be 1 character (except C or R).
Range Names cannot be named the same as a cell reference, like IB39999.
-  3D ranges must refer to a block of the same cells on each sheet between a named front sheet and back sheet of the 3D range.
-  Range Names CAN refer to discontinuous cell ranges. 

Example Workbook: PastMtgs/0110/RangeNames.zip
and PastMtgs/0110/RangeNames2.zip

bullet

Use the Name Box
-  Select some Cells on a worksheet
-  Enter a name, Able, in the Name Box on the Left of the Formula Bar  

bullet

Can you use the name box to create a 3D Range?   No
-  Select more than one worksheet:  Click a worksheet tab.  Hold the Shift key down and Click another tab.  All sheets between are now selected,  too.
-  Select a range of cells on the active sheet
-  Color Fill the cells.
-  Type something in one of the cells, then press Ctrl-Shift-Enter to enter it into all the cells selected on all the selected sheets.
-  Type a Name, Baker, in the Name Box.
-  Goto one of the other sheets that was selected.    The cells are colored.
-  Type the Name into the range box.   You go to the selected cells of only the sheet that was active

bullet

Use Define Name to create a 2D cell Range
Select a worksheet.
Select a single range of cells.
Menu: Insert, Name, Define...
Type the Name in the Names in Workbook box.
The selected cells are already in the Refers To box.
Click OK.

bullet

Use Define Name to create a 3D cell Range
Menu: Insert, Name, Define...
Type the Name in the Names in Workbook box.
In the Refers To box, type the 
     Front Sheet of the 3D Range,
     A semicolon
     The Back Sheet of the 3D Range
     And exclamation point
     The Cell range on each sheet in the range.
Example:
=Sheet7:Sheet5!$C$11:$E$14
Click OK.
This name will not show up in the Name Box.   You cannot use it to go to a range and have cells selected.   You can use this named range in formulas like SUM(rangename) or COUNTA(rangename).

bullet

Use Create Name
Create a table with Row and or Column Headers.   [Sheet: WinLoss]
Select the cells of the table including the headers.
Menu: Insert, Name, Create
Set the check boxes for which headers you wish to use to create the names.
Example: set the check boxes for Top and Left.
Click OK
Now, check the Name Box to see new Names created for each column and row in the table.

bullet

Find the Intersection of two ranges
In the Name box, type the name of a column in a table above, a SPACE, and the name of a row.
Press Enter.    If the names belong to the same worksheet, you will select the intersection of the two ranges; that is the cells the ranges have in common. 
Example from the WinLoss sheet above, if you have a row range of North and a column range of Wins then make your formula:  =North Wins

bullet

Find the Range Bound by two Names
In the name box, type a range name, a colon, another range name on the same sheet.    
Press Enter.   You will select all cells between those two ranges, inclusive.
Example: if you have row ranges: North and South, then you will select all cells between those ranges, inclusive, with the range North:South

bullet

Create a Name that defines several discontinuous cell ranges.
Menu: Insert, Name, Define...
Type the Name, Delta, in the Names in Workbook box.
In the Refers To box, type the blocks separated by commas.
=Sheet2!$C$5:$D$9,Sheet2!$E$15:$G$18,Sheet2!$H$7:$J$9

bullet

Create names by Copying sheets.Yes, just by copying a worksheet containing named ranges, you will create more ranges.  Until now, the ranges we created were "Global", known throughout the workbook.   When you make a copy of a worksheet, you will cause each name defined on that worksheet to be copied, too.   These new names, however, cannot be global, but they must only refer to the new sheet.

Take the Sheet1 in the example workbook.   Copy it.   (Hold the Ctrl key down, Click down on the Sheet1 tab, drag it right until you see a little plus sign on the pointer.  Release.)    Rename the sheet by double clicking on the sheet tab until the tab text is higlighted, then type in a new name.

Menu: Insert, Name, Define
Note that the Name Able looks different than the others.   To the right of the name, it has Sheet1Copied.  This indicates that this names definition is "Local",  limited only to the Sheet1Copied worksheet.   There may be (and there is!) the name Able on other worksheets that point a different range.

Local names are visible in the Define Name dialog only when their own sheet is the active worksheet.    The Define Names Dialog only shows the local names of the active worksheet and the global names that are different from local names. 

bullet

Use Offset in a Formula in Define Name
On Sheet Edward I have a table of numbers and a named range called Echo  whose definition is =Edward!$B$4:$D$7.    You can SUM(Echo) and get the answer 2040.    Suppose we want to create a range Echo23 that is the same size as Echo, but 2 rows lower and 3 columns right.    We could define Echo23 as = Edward!$E$6:$G$9.   That would work for now.   But what if we moved the defined range of Echo to another set of cells.   If we want Echo23 to always be 2 down and 3 left from Echo, no matter where Echo is, then we want to use the OFFSET function.

Menu: Insert, Name, Define.
Type Echo23 in the Names in Workbook box.
Type in the Refers To box: =OFFSET(Echo,2,3)

Think of it.   OFFSET is POWERFUL!!!    You can establish a couple of fixed benchmarks in a worksheet, and define all other names relative to those benchmarks.   You can use Echo23 in a SUM or other function just as you would Echo.

One drawback:  You cannot use Echo23 in the Name box to go to it.   VBA can use Echo23 to select, however.

bullet

Use Offset in a Formula in Define Name and Resize a range.
Echo is a 4x3 cell range.   Suppose we want EchoSW1 to be cells D4:D5 when Echo is B4:D7.   That means we need use Echo, offset it 3 down, 0 right, and we resize it to be 1 row tall and 2 rows wide.
Menu: Insert, Name, Define.
Type EchoSW1 in the Names in Workbook box.
Type in the Refers To box: =OFFSET(Echo,3,0,1,2)
The last two arguments of Offset are optional and they describe how many rows and columns the range is to be.   If they are omitted, as in the previous example, the size of the referenced range is used.

You can use OFFSET in a worksheet cell, too.

bullet

Use Index in a Formula in Define Name
Suppose we want to return the 3rd column of the 2nd row of a range, such as Echo.   We could use an offset, =Offset(Echo,1,2,1,1), which is take Echo, offset it 1 down (2nd row), 2 right (3rd column), and give me one cell (one row high one row wide.).    It would work, but it is a bit confusing.

A better function is INDEX which you would write as =INDEX(Echo,2,3), get the value from the 2nd row, 3rd column of the Echo range.

bullet

VBA: Names.Add
 Names.Add Name:="Table2", RefersTo:="=$C$15:$F$19"

bullet

VBA: Range(cells).Name = Name
Range("$d$5:$g$9).Name = "FoxTrot"

bullet

VBA: Workbook.Names    (Global Names)
Activeworkbook.Names.Add Name:="Table2", RefersTo:="=$C$15:$F$19"

bullet

VBA: worksheet(i).Names      (Local Sheet Range Names)
Worksheet("Sheet1Copied").Names.Add Name:="Table2", RefersTo:="=$C$15:$F$19"

bullet

VBA: usage of RefersTo
String = Names("Echo").RefersTo
RefersTo mearly returns the definition of the name, not the actual range. 

bullet

VBA: usage of RefersToRange
 Names("Wins").RefersToRange.Select
The RefersToRange takes whatever the name RefersTo and resolves it into the actual range of cells in the workbook.

bullet

VBA: using Offset
Define a Named Range, "Table3", that is 8 rows down, and 2 rows left  (-2 rows right) from where Table2 is defined.  Make it the same size as Table2.
Range("Table2").Offset(8, -2).Name = "Table3"

bullet

VBA: Using Resize
Make "Table4",  10 rows deep and 5 columns wide.    Its upper left corner should start 8 rows down and 2 rows right from the upper left corner of Table2.
Range("Table2").Offset(8, 2).Resize(10,5).Name = "Table4"

bullet

Three ways to select a cell range

1.   Application.Goto "Able"                      'Nice in that it will take you to the correct worksheet, too.

2.   With Names("Able").RefersToRange     'Get the Range object that the name Able Describes
          .Parent.Select                                  'Activate the sheet Able is on.    
          .Select                                            'Select the cell range
      End With

3.  [Able].Parent.Select
     [Able].Select

This third method, using [Able] instead of Names("Able").RefersToRange
is something I saw Bob Umlas do in the Excel-L archives with in the past 10 days.

I'm of two minds about it.   On the one hand, it saves a lot of typing!   On the other hand, it seems to viloate in a big way many of the grammar rules of VBA, particularly that Able is not enclosed in quotes and the square brackets are usually reserved for Filenames in workbook addresses.

At this moment, I'm not going to advocate its use.   At least not until I learn its querks.   But I thought I'd pass it along just in case you find it in someone's work.   BTW, if this syntax is in the help files, I have yet to find it.

Draft code

 

Sub test1()
    [able].Parent.Select
    [able].Select
End Sub


Sub Table2()
    Worksheets("Sheet7").Select
    Application.Goto "Wins North"
    Application.Goto "Able"
    Names("Wins").RefersToRange.Parent.Select
    Names("Wins").RefersToRange.Select
    Selection.CurrentRegion.Select
    Selection.Name = "Table1"
    
    ActiveWindow.RangeSelection.Name = "Table1"
    'This does not work in debug, because the worksheet is not the active window in debug.
    
    
    Names.Add Name:="Table2", RefersTo:="=$C$15:$F$19"
    Names("Table2").RefersToRange.Select
    
    Range("Table2").Offset(8, 2).Name = "Table3"
    Range("Table3").Interior.Color = vbCyan
    
    
End Sub

For questions or comments concerning content on this website: Stephen Rasey
Design of this site by Cheryl D. Wise
Copyright © 2000-2004 by WiserWays. All rights reserved.
Revised: 2005-07-10 01:08 .