Naming a range with VBA - Scope Workbook

braindiesel

Well-known Member
Joined
Mar 16, 2009
Messages
571
Office Version
  1. 365
  2. 2019
  3. 2010
Platform
  1. Windows
As the subject says, I want to name a range with a workbook scope.

Here is a sample of the code

Selection.Resize(25, 12).Select
actualrange = Selection.Address
ActiveWorkbook.Names.Add Name:="actualSales1", RefersTo:=Sheets("Simply Actual").Range(actualrange)

This is setting the range name to the sheet. How can I change the scope with VBA?

Your help is greatly appreciated,

Glen
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
To set a name with WORKBOOK scope, use:

Code:
ActiveWorkbook.Names.Add

To set a name with WORKSHEET scope, use:

Code:
ActiveWorkbook.Worksheets("Sheet1").Names.Add

Set "Sheet1" to be the name of the applicable worksheet.
 
Upvote 0
Hi PJ

My original code was simply this
ActiveWorkbook.Names.Add Name:="actualSales1", RefersTo:=selection

that is creating a name in the sheet, not the workbook.

The code I posted above was an effort to make the name global. still no avail.

Any suggestions?
 
Upvote 0
That code should create a workbook scope name, not a sheet one.
You could also use:
Code:
selection.name = "actualSales1"
 
Upvote 0
I can't reproduce the error.

Perhaps there is a misunderstanding about the word "scope".

The OP code creates (on my machine) a name "actualSales1". It is scoped at the workbook level, which means that a formula (like =SUM(actualSales1)) will return the sum of all of those cells, no matter what sheet holds the formula.
The formula =SUM(actualSales1) when placed in Sheet1!C1 will sum the same cells as the same formula when placed in Sheet2!C1. The name "actualSales1" has meaning on all sheets. Its "scope" is at the workbook level.

PJ_in_FL's code that specifies a particular worksheet, results in a name that is scoped to one sheet. i.e. the formula =SUM(actualSales1) will result in a #NAME error if put on any sheet than Sheet1. The name actualSales1 has meaning on only one worksheet. It is scoped at the worksheet level.

If you are looking for a named range where the sheet it refers to is relative would be defined like
Name: myName
RefesTo: !$A$1:$A$10

The formula =SUM(myName) would return the sum of cells A1:A10 of the same sheet that holds the formula. i.e. the formula =SUM(myName) placed in Sheet1!C1 would sum different cells than the formula =SUM(myName) placed in Sheet2!C1. This is not "scope", but relative sheet addressing.


The VB code for a name that is scoped at the workbook level with relative sheet addressing would be

Code:
ActiveWorkbook.Names.Add Name:="myName", RefersToR1C1:="=!" & Selection.Address(True, True, xlR1C1)
 
Last edited:
Upvote 0
Thanks everyone for the replies

Rorya, this one
selection.name = "actualSales1"

solved it.

Thank you
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,484
Members
452,917
Latest member
MrsMSalt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top