Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default referring to a named range on another worksheet

i am getting an error message in my code when trying to refer to a named
range in a different worksheet (not the active sheet). i have a variable
"Rng" that refers to a non-contiguous named range on sheet1. a macro on a
form control is attempting to copy this range and paste it to sheet2. do i
always have to refer to the sheet name with a named range? i.e.
sheets("Sheet1").Range("MyRange")
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default referring to a named range on another worksheet

to copy a range from one sheet to another. Assuming rng is a range object

set rng = worksheets("sheet1").Range("b2:m15")

With rng


Worksheets("Sheet2").Range("A1").Resize(.Rows.Coun t,.Columns.Count).Value _
= .Value

End With





"Virginia" wrote:

i am getting an error message in my code when trying to refer to a named
range in a different worksheet (not the active sheet). i have a variable
"Rng" that refers to a non-contiguous named range on sheet1. a macro on a
form control is attempting to copy this range and paste it to sheet2. do i
always have to refer to the sheet name with a named range? i.e.
sheets("Sheet1").Range("MyRange")

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default referring to a named range on another worksheet

Virginia:

first an important lesson in the theory of names

names exist in 2 places:

at the worksheet level 'local' name
at the workbook level 'global' name

all the names are members of the workbook's
names collection.

local names look like: sheet1!aName
global names look like: bName

for local names you'll see the sheetname
in the right column of the define names dialog

local names can be called from their 'own' sheet
without the sheet prefix.

HOWEVER:
you cannot access the GLOBAL name if a LOCAL name exist
on the activesheet with the SAME name

SO you should keep local and global ranges
separate.. and avoid 'duplicates'

Excel does not make this easy. Names are defined
as global by default. and when you copy a sheet
that contains global names, the NEW sheet will have
local names and the original sheet will retain the global names.

Confusing?..

download NameManager addin from www.jkp-ads.com
to help you manage names. Indispensible.


so back to your problem:
when you want to have the same name "rng" on multiple sheets
ensure that they are local names and that there is NO global name "rng"
(use NameManager for this!)

If and when the above is true...
(assuming names in activeworkbook)

strRngAddress = Range("sheet1!rng"),address
names.add "sheet2!rng", refersto = strRngAddress
or
worksheets(2).names.add "rng", _
refersto:=worksheets(1).Range("rng").address


there is another problem.. when you add a name it's "refersto"
argument is limited in length to 255 chars in r1c1 notation
.... which may represent a problem for more complicated multiarea ranges.

if so try following tedious method to copy the name:

Sub CreateAcomplicateName()
Dim i&, rg As Range
Worksheets(1).Activate
Set rg = Cells(1)
For i = 6 To 500 Step 5
Set rg = Union(rg, Cells(i, 1))
Next
rg.Name = "'" & ActiveSheet.Name & "'!test"
End Sub

Sub CopyAComplicatedName()
Dim rg As Range
Worksheets(1).Activate
Set rg = Range("'" & ActiveSheet.Name & "'!test")
rg.Select
Worksheets(2).Select False
Worksheets(2).Activate
Worksheets(2).Select True
Set rg = Selection
rg.Name = "'" & ActiveSheet.Name & "'!test"
End Sub


long story what.. :)
Names ARE handy.. but complicated and imo somewhat buggy.


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Virginia wrote :

i am getting an error message in my code when trying to refer to a
named range in a different worksheet (not the active sheet). i have
a variable "Rng" that refers to a non-contiguous named range on
sheet1. a macro on a form control is attempting to copy this range
and paste it to sheet2. do i always have to refer to the sheet name
with a named range? i.e. sheets("Sheet1").Range("MyRange")

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default referring to a named range on another worksheet

Thank you very much, your post was very helpful.

"keepITcool" wrote:

Virginia:

first an important lesson in the theory of names

names exist in 2 places:

at the worksheet level 'local' name
at the workbook level 'global' name

all the names are members of the workbook's
names collection.

local names look like: sheet1!aName
global names look like: bName

for local names you'll see the sheetname
in the right column of the define names dialog

local names can be called from their 'own' sheet
without the sheet prefix.

HOWEVER:
you cannot access the GLOBAL name if a LOCAL name exist
on the activesheet with the SAME name

SO you should keep local and global ranges
separate.. and avoid 'duplicates'

Excel does not make this easy. Names are defined
as global by default. and when you copy a sheet
that contains global names, the NEW sheet will have
local names and the original sheet will retain the global names.

Confusing?..

download NameManager addin from www.jkp-ads.com
to help you manage names. Indispensible.


so back to your problem:
when you want to have the same name "rng" on multiple sheets
ensure that they are local names and that there is NO global name "rng"
(use NameManager for this!)

If and when the above is true...
(assuming names in activeworkbook)

strRngAddress = Range("sheet1!rng"),address
names.add "sheet2!rng", refersto = strRngAddress
or
worksheets(2).names.add "rng", _
refersto:=worksheets(1).Range("rng").address


there is another problem.. when you add a name it's "refersto"
argument is limited in length to 255 chars in r1c1 notation
.... which may represent a problem for more complicated multiarea ranges.

if so try following tedious method to copy the name:

Sub CreateAcomplicateName()
Dim i&, rg As Range
Worksheets(1).Activate
Set rg = Cells(1)
For i = 6 To 500 Step 5
Set rg = Union(rg, Cells(i, 1))
Next
rg.Name = "'" & ActiveSheet.Name & "'!test"
End Sub

Sub CopyAComplicatedName()
Dim rg As Range
Worksheets(1).Activate
Set rg = Range("'" & ActiveSheet.Name & "'!test")
rg.Select
Worksheets(2).Select False
Worksheets(2).Activate
Worksheets(2).Select True
Set rg = Selection
rg.Name = "'" & ActiveSheet.Name & "'!test"
End Sub


long story what.. :)
Names ARE handy.. but complicated and imo somewhat buggy.


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Virginia wrote :

i am getting an error message in my code when trying to refer to a
named range in a different worksheet (not the active sheet). i have
a variable "Rng" that refers to a non-contiguous named range on
sheet1. a macro on a form control is attempting to copy this range
and paste it to sheet2. do i always have to refer to the sheet name
with a named range? i.e. sheets("Sheet1").Range("MyRange")


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
Referring to specific cell in named range in Excel formula Nigel Barton Excel Worksheet Functions 3 August 18th 09 04:25 PM
Do not specify worksheet in a named range hmm Excel Discussion (Misc queries) 3 September 18th 07 01:30 PM
Getting a named range to appear in another worksheet John Excel Discussion (Misc queries) 2 July 28th 07 02:12 AM
How do I use indirect when referring to a named range in a closed Ed Green Excel Worksheet Functions 3 May 22nd 06 08:01 PM
named range not specific to worksheet Mark Kubicki Excel Programming 1 November 26th 03 04:39 PM


All times are GMT +1. The time now is 06:51 PM.

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"