Creating Named Ranges with VBA

B

Barb Reinhardt

I have programmatically created a named range that is tied to a worksheet
using the following

Worksheets(sht).Names.Add Name:=rangename, RefersTo:="=" & seriesrange

What would change if the range name was not tied to the worksheet, but
global for the wookbook?

Thanks,
Barb Reinhardt
 
B

Bernie Deitrick

What would change if the range name was not tied to the worksheet, but
global for the wookbook?

rangename would not have the sheetname as part of the string.

HTH,
Bernie
MS Excel MVP
 
B

Barb Reinhardt

I don't think I made myself clear. I believe that there are range names
that are global within the workbook and range names that only work on the
current worksheet.

This example:

Worksheets(sht).Names.Add Name:=rangename, RefersTo:="=" & seriesrange

Created a named range that was only available on sht.

How would I create a named range using VBA that's available on all sheets.
If you could provide the syntax, I'd appreciate it.

Thanks,
Barb
 
B

Bob Phillips

Worksheets(1).Names.Add Name:="Bob", RefersTo:="=A1:A10"
ActiveWorkbook.Names.Add Name:="Bob", RefersTo:="=M1:M10"


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
B

Bernie Deitrick

Sorry, I always create local names like this, to the names collection of the workbook:

ActiveWorkbook.Names.Add Name:="Sheet1!Test2", RefersToR1C1:="=Sheet1!R6C2"

So, to create a global name using your syntax, instead of adding the name to the
Worksheets(sht).Names collection, use

ActiveWorkbook.Names.Add Name:=rangename, RefersTo:="=" & seriesrange

HTH,
Bernie
MS Excel MVP
 
D

Dave Peterson

You have your answer, but those worksheet level names are available from other
sheets, too--just use the whole name.

If I give A1 on Sheet2 a worksheet level name of myCell, then I can get that
value on other sheets by using:
=sheet2!myCell

And if you don't have Jan Karel Pieterse's (with Charles Williams and Matthew
Henson) Name Manager, you should get it.

You can find it at:
NameManager.Zip from http://www.oaltd.co.uk/mvp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top