ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Reference a named range (https://www.excelbanter.com/excel-programming/342199-reference-named-range.html)

John M.[_3_]

Reference a named range
 
My data spreadsheet has several named ranges, with meaningful names. I'd like
to refer to them in VBA, but all attempts so far lead me to absolute
addresses like $R1$C1, or sheet1!R23C45. If a user cselects a named range how
can I read the name of the range (that might be a sinlge cell) and how can I
write to another named range (that will have many cells)?
Many thanks whoever is out there.


Tom Ogilvy

Reference a named range
 
If the the selection is the entire named range and nothing else

msgbox selection.name.name

if the user selects A1:B2 and the named range is only A1, then
selection.Name.Name will return an error.

Other than that, you would have to loop through the names collection and
test where the selection is relative to each named range.


You can refer to a known named range with

set rng = Range("MyNamedRange")

selection.Copy Destination:=rng

--
Regards,
Tom Ogilvy


"John M." wrote in message
...
My data spreadsheet has several named ranges, with meaningful names. I'd

like
to refer to them in VBA, but all attempts so far lead me to absolute
addresses like $R1$C1, or sheet1!R23C45. If a user cselects a named range

how
can I read the name of the range (that might be a sinlge cell) and how can

I
write to another named range (that will have many cells)?
Many thanks whoever is out there.




John M.[_3_]

Reference a named range
 
Great, thanks Tom. "name.name" is one of the less intuitive versions that I
did not try. I can pre-filter all multiple selections and get exactly what I
need. Best wishes

"Tom Ogilvy" wrote:

If the the selection is the entire named range and nothing else

msgbox selection.name.name

if the user selects A1:B2 and the named range is only A1, then
selection.Name.Name will return an error.

Other than that, you would have to loop through the names collection and
test where the selection is relative to each named range.


You can refer to a known named range with

set rng = Range("MyNamedRange")

selection.Copy Destination:=rng

--
Regards,
Tom Ogilvy


"John M." wrote in message
...
My data spreadsheet has several named ranges, with meaningful names. I'd

like
to refer to them in VBA, but all attempts so far lead me to absolute
addresses like $R1$C1, or sheet1!R23C45. If a user cselects a named range

how
can I read the name of the range (that might be a sinlge cell) and how can

I
write to another named range (that will have many cells)?
Many thanks whoever is out there.






All times are GMT +1. The time now is 01:11 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com