Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default Accessing a macro created named range

I am using the following code in a macro to set up a named range on a
worksheet for use by other macros. I create the named range with the
following code:

ThisWorkbook.Names.Add Name:="Table1", RefersTo:=NewSheetName & "!$B$9:$H$500"

The new named range appears fine in the Developer Name Manager list, but
does not appear in the Name Box drop down list and I code such as
i = Range("_PAI_Table_bbb").Rows.Count
that uses Table1 errors w/ a 1004 range of object _Global failed.

Somehow I'm either not creating the range properly and/or I'm not
referencing it correctly.

I appreciate your help, -John
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Accessing a macro created named range

Hi again John,

I had another look to see exactly what was occurring. When naming the range
with your code if you go to name manager and select the name then in the
Refers to field you will see double quotes around the sheet name and range
like this.

="MyTestSheet!$B$9:$H$500"

If you create the named range manually then you will see that the double
quotes are not there like this.

=MyTestSheet!$B$9:$H$500

The code that I have given you will create the named range correctly without
the double quotes.

I have no idea why xl does not error out with your creation code. Just one
of those thing I suppose.


--
Regards,

OssieMac


"OssieMac" wrote:

Hi John,

Syntax for naming the range is incorrect. It does appear to name the range
but not properly and it appears that xl does not error out on it. Try this.

Dim newSheetName As String
Dim i As Long

newSheetName = "MyTestSheet"

ActiveWorkbook.Names.Add Name:="Table1", _
RefersTo:=Sheets(newSheetName).Range("$B$9:$H$500" )

i = Range("Table1").Rows.Count

--
Regards,

OssieMac


"John" wrote:

I am using the following code in a macro to set up a named range on a
worksheet for use by other macros. I create the named range with the
following code:

ThisWorkbook.Names.Add Name:="Table1", RefersTo:=NewSheetName & "!$B$9:$H$500"

The new named range appears fine in the Developer Name Manager list, but
does not appear in the Name Box drop down list and I code such as
i = Range("_PAI_Table_bbb").Rows.Count
that uses Table1 errors w/ a 1004 range of object _Global failed.

Somehow I'm either not creating the range properly and/or I'm not
referencing it correctly.

I appreciate your help, -John

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Accessing a macro created named range

Hi John,

Syntax for naming the range is incorrect. It does appear to name the range
but not properly and it appears that xl does not error out on it. Try this.

Dim newSheetName As String
Dim i As Long

newSheetName = "MyTestSheet"

ActiveWorkbook.Names.Add Name:="Table1", _
RefersTo:=Sheets(newSheetName).Range("$B$9:$H$500" )

i = Range("Table1").Rows.Count

--
Regards,

OssieMac


"John" wrote:

I am using the following code in a macro to set up a named range on a
worksheet for use by other macros. I create the named range with the
following code:

ThisWorkbook.Names.Add Name:="Table1", RefersTo:=NewSheetName & "!$B$9:$H$500"

The new named range appears fine in the Developer Name Manager list, but
does not appear in the Name Box drop down list and I code such as
i = Range("_PAI_Table_bbb").Rows.Count
that uses Table1 errors w/ a 1004 range of object _Global failed.

Somehow I'm either not creating the range properly and/or I'm not
referencing it correctly.

I appreciate your help, -John

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Accessing a macro created named range

Probably the variable NewSheetName does not refer to the string name of a
worksheet in ThisWorkbook.

Regards,
Peter T


"John" wrote in message
...
I am using the following code in a macro to set up a named range on a
worksheet for use by other macros. I create the named range with the
following code:

ThisWorkbook.Names.Add Name:="Table1", RefersTo:=NewSheetName &
"!$B$9:$H$500"

The new named range appears fine in the Developer Name Manager list, but
does not appear in the Name Box drop down list and I code such as
i = Range("_PAI_Table_bbb").Rows.Count
that uses Table1 errors w/ a 1004 range of object _Global failed.

Somehow I'm either not creating the range properly and/or I'm not
referencing it correctly.

I appreciate your help, -John



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Accessing a macro created named range

Hi Peter and John,

Just for interest, the way John had coded the sheetname and range is missing
the leading equal (=) sign in the concatenated string.

VBA has then placed double quotes around the entire string which made the
reference invalid.

The method I posted does not need concatenation and I think is easier to
code. However, if coding with John's method then it should be like this.

ThisWorkbook.Names.Add Name:="Table1", RefersTo:= _
"=" & newSheetName & "!$B$9:$H$500"

--
Regards,

OssieMac




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Accessing a macro created named range

Yes you are right. At best, my suggestion was incomplete.

Regards,
Peter T

"OssieMac" wrote in message
...
Hi Peter and John,

Just for interest, the way John had coded the sheetname and range is
missing
the leading equal (=) sign in the concatenated string.

VBA has then placed double quotes around the entire string which made the
reference invalid.

The method I posted does not need concatenation and I think is easier to
code. However, if coding with John's method then it should be like this.

ThisWorkbook.Names.Add Name:="Table1", RefersTo:= _
"=" & newSheetName & "!$B$9:$H$500"

--
Regards,

OssieMac




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Accessing a macro created named range

I find that this syntax is easier to use:

worksheets(newsheetname).range("b9:h500").name = "Table1"
(for a workbook name)

or

with worksheets(newsheetname)
.range("b9:h500").name = "'" & .name & "'!Table1"
end with
(for a worksheet level name)


John wrote:

I am using the following code in a macro to set up a named range on a
worksheet for use by other macros. I create the named range with the
following code:

ThisWorkbook.Names.Add Name:="Table1", RefersTo:=NewSheetName & "!$B$9:$H$500"

The new named range appears fine in the Developer Name Manager list, but
does not appear in the Name Box drop down list and I code such as
i = Range("_PAI_Table_bbb").Rows.Count
that uses Table1 errors w/ a 1004 range of object _Global failed.

Somehow I'm either not creating the range properly and/or I'm not
referencing it correctly.

I appreciate your help, -John


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,069
Default Accessing a macro created named range

Ossie and Peter, thx so much! This worked great! I really appreciate your
help, John

"John" wrote:

I am using the following code in a macro to set up a named range on a
worksheet for use by other macros. I create the named range with the
following code:

ThisWorkbook.Names.Add Name:="Table1", RefersTo:=NewSheetName & "!$B$9:$H$500"

The new named range appears fine in the Developer Name Manager list, but
does not appear in the Name Box drop down list and I code such as
i = Range("_PAI_Table_bbb").Rows.Count
that uses Table1 errors w/ a 1004 range of object _Global failed.

Somehow I'm either not creating the range properly and/or I'm not
referencing it correctly.

I appreciate your help, -John

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
Accessing a sheet-level named range through ADO Ahasverus Excel Programming 2 October 19th 07 12:15 AM
Accessing data stored in the worksheet (named range) in .xla file noiseberg Excel Programming 1 September 26th 07 07:03 PM
accessing named range greg Excel Programming 13 July 12th 07 01:49 AM
New Named Range Created Each Time Data Imported into Excel via Macro Carroll Rinehart Excel Programming 2 October 28th 04 04:33 PM
Accessing Named Ranges Michael Monteiro Excel Programming 1 August 27th 03 05:29 PM


All times are GMT +1. The time now is 10:20 AM.

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

About Us

"It's about Microsoft Excel"