Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default Problem with progamatically defined named ranges

In Excel 2003, I am defining a named range with the statement"
ThisWorkbook.Names.Add Name:="MyName", _
RefersTo:=Worksheets("X").Range("$A$2:$A$33")
This statement execures fine.

If I follow this by the code:
Set r = ThisWorkbook.Names("MyName").RefersToRange
Debug.Print r.Address
It prints: $A$2:$A$33

If I use
For n = 1 To ThisWorkbook.Names.Count
Debug.Print ThisWorkbook.Names(n).Name & " -- " & _
ThisWorkbook.Names(n)
Next n
it prints: MyName -- =[MyWorkboonName]X!$A$2:$A$33

All this is as I expect.

HOWEVER:
1. If I select A2:A33 the name doesn't show in the name window
2. If I use
Range(MyRange).Validation.Add Type:=xlValidateList, _
Formula1:="=MyName"
I get an error that MyName is not a valid name
3. If in Excel I select Insert/Name/Define, MyName is not is the list
of defined names

If I define the named range manually by selecting it and typing the name in
the name box then all of the above works fine. Apparently the names.add
statement is defining the name in some other collection. So, I guess the
names.add statement is wrong somehow.

How can I do it correctly?
Secondly, what is the statement I use actually doing. It is clearly doing
something, but the wrong thing.

Thanks!!


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default Problem with progamatically defined named ranges

Duh!

It works fine. I failed to mention (because I didn't think about it) that
the code was in an addin. I should have used ActibeWorkbook instead of
ThisWorkbook.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 168
Default Problem with progamatically defined named ranges

On Jul 2, 10:27*am, wrote:
In Excel 2003, I am defining a named range with the statement"
* *ThisWorkbook.Names.Add Name:="MyName", _
* *RefersTo:=Worksheets("X").Range("$A$2:$A$33")
This statement execures fine. *

If *I follow this by the code:
* *Set r = ThisWorkbook.Names("MyName").RefersToRange
* *Debug.Print r.Address
It prints: *$A$2:$A$33

If I use
* For n = 1 To ThisWorkbook.Names.Count
* * * *Debug.Print ThisWorkbook.Names(n).Name & " -- " & _ * *
* * * * * * * ThisWorkbook.Names(n)
* * * *Next n
it prints: MyName -- =[MyWorkboonName]X!$A$2:$A$33

All this is as I expect.

HOWEVER:
* *1. *If I select A2:A33 the name doesn't show in the name window
* *2. *If I use
* * * * * Range(MyRange).Validation.Add Type:=xlValidateList, _ * * * * *
* * * * * * * * * *Formula1:="=MyName"
* * * *I get an error that MyName is not a valid name
* *3. *If in Excel I select Insert/Name/Define, MyName is not is the list *
* * * * of defined names

If I define the named range manually by selecting it and typing the name in
the name box then all of the above works fine. *Apparently the names.add
statement is defining the name in some other collection. *So, I guess the
names.add statement is wrong somehow.

How can I do it correctly?
Secondly, what is the statement I use actually doing. *It is clearly doing
something, but the wrong thing.

Thanks!!


I tested your macro to name the range just fine. I usually just use
Worksheets("X").Range("$A$2:$A$33"),name="MyName"
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Problem with progamatically defined named ranges

On Jul 2, 11:01*am, wrote:
Duh!

It works fine. *I failed to mention (because I didn't think about it) that
the code was in an addin. *I should have used ActibeWorkbook instead of
ThisWorkbook.


ActiveWorkbook is for the book that is active
ThisWorkbook is the book that contains the code.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Named ranges scope / workbook/worksheet level named ranges- changeswith variable use... christian_spaceman Excel Programming 3 December 24th 07 01:15 PM
Problem with named ranges Keith Excel Programming 4 November 16th 06 02:19 PM
Reviewing a list of defined named ranges Barb Reinhardt Excel Programming 3 August 21st 06 06:00 PM
VBA Script to Print several named ranges to PDF with cell-defined filenames jbmessamore Excel Programming 0 July 14th 06 03:34 PM
ADO - recordset - closed excel workbook - know sheet name and cell name but no named ranges defined grahamd Excel Programming 1 October 18th 04 06:13 PM


All times are GMT +1. The time now is 04:06 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"