PC Review


Reply
Thread Tools Rate Thread

Copy named range based on variable

 
 
IanC
Guest
Posts: n/a
 
      10th Dec 2009
Is it possible to copy a named range based on a variable?

I have a number of named ranges which I need to copy based on the
results of a cell value.

My whole routine is nearly 500 lines of code, so I've only included the
relevant bits in this message. For example, I have the following lines at
the start of my routine:

With Worksheets("Lookup")
Set MTS215Vision30 = .Range("MTS2.16Vision30")
Set MTS216Vision30 = .Range("MTS2.16Vision30")
End With

and want to copy one of these based on the value of cell which could
currently contain 2.15 or 2.16.

This line:
sMTSVer = "MTS" & Worksheets("Input").Range("T3") * 100 & "Vision30"
makes sMTSVer = MTS216Vision30

This works:
MTS216Vision30.Copy .Range("A5")
But this doesn't:
sMTSVer.Copy .Range("A5")

I know I could use something like .....
With Worksheets("Input")
If .Range("T3")=2.15 then
MTS215Vision30.Copy .Range("A5")
Elseif .Range("T3")=2.16 then
MTS216Vision30.Copy .Range("A5")
End If
End WIth
...... but in time there will be 2.17, 2.18 etc and I am hoping to "future
proof" the code so that I only need to create the named ranges and add a
"Set" line rather than having to add more If conditions, as the Vision30
example is only one of a number of similar situations.

Any ideas? I need this to work on any machine with a "default" Excel 2000
installation (ie not an add-in).

--
Ian
--


 
Reply With Quote
 
 
 
 
Chip Pearson
Guest
Posts: n/a
 
      10th Dec 2009

If you have a string that names a range, just use Range to get a real
range from the string. E.g.,


sMTSVer = "some string"
Set R = Range(sMTSVer)
R.Copy destiantion:=Range("A5")

Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]




On Thu, 10 Dec 2009 19:37:29 -0000, "IanC" <(E-Mail Removed)> wrote:

>Is it possible to copy a named range based on a variable?
>
>I have a number of named ranges which I need to copy based on the
>results of a cell value.
>
>My whole routine is nearly 500 lines of code, so I've only included the
>relevant bits in this message. For example, I have the following lines at
>the start of my routine:
>
>With Worksheets("Lookup")
> Set MTS215Vision30 = .Range("MTS2.16Vision30")
> Set MTS216Vision30 = .Range("MTS2.16Vision30")
>End With
>
>and want to copy one of these based on the value of cell which could
>currently contain 2.15 or 2.16.
>
>This line:
> sMTSVer = "MTS" & Worksheets("Input").Range("T3") * 100 & "Vision30"
>makes sMTSVer = MTS216Vision30
>
>This works:
> MTS216Vision30.Copy .Range("A5")
>But this doesn't:
> sMTSVer.Copy .Range("A5")
>
>I know I could use something like .....
>With Worksheets("Input")
> If .Range("T3")=2.15 then
> MTS215Vision30.Copy .Range("A5")
> Elseif .Range("T3")=2.16 then
> MTS216Vision30.Copy .Range("A5")
> End If
>End WIth
>..... but in time there will be 2.17, 2.18 etc and I am hoping to "future
>proof" the code so that I only need to create the named ranges and add a
>"Set" line rather than having to add more If conditions, as the Vision30
>example is only one of a number of similar situations.
>
>Any ideas? I need this to work on any machine with a "default" Excel 2000
>installation (ie not an add-in).

 
Reply With Quote
 
IanC
Guest
Posts: n/a
 
      15th Dec 2009
Thanks Chip. It wasn't quite the way I'd intended, but with a bit of
recoding it's working well.

--
Ian
--

"Chip Pearson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> If you have a string that names a range, just use Range to get a real
> range from the string. E.g.,
>
>
> sMTSVer = "some string"
> Set R = Range(sMTSVer)
> R.Copy destiantion:=Range("A5")
>
> Cordially,
> Chip Pearson
> Microsoft MVP 1998 - 2010
> Pearson Software Consulting, LLC
> www.cpearson.com
> [email on web site]
>
>
>
>
> On Thu, 10 Dec 2009 19:37:29 -0000, "IanC" <(E-Mail Removed)> wrote:
>
>>Is it possible to copy a named range based on a variable?
>>
>>I have a number of named ranges which I need to copy based on the
>>results of a cell value.
>>
>>My whole routine is nearly 500 lines of code, so I've only included the
>>relevant bits in this message. For example, I have the following lines at
>>the start of my routine:
>>
>>With Worksheets("Lookup")
>> Set MTS215Vision30 = .Range("MTS2.16Vision30")
>> Set MTS216Vision30 = .Range("MTS2.16Vision30")
>>End With
>>
>>and want to copy one of these based on the value of cell which could
>>currently contain 2.15 or 2.16.
>>
>>This line:
>> sMTSVer = "MTS" & Worksheets("Input").Range("T3") * 100 & "Vision30"
>>makes sMTSVer = MTS216Vision30
>>
>>This works:
>> MTS216Vision30.Copy .Range("A5")
>>But this doesn't:
>> sMTSVer.Copy .Range("A5")
>>
>>I know I could use something like .....
>>With Worksheets("Input")
>> If .Range("T3")=2.15 then
>> MTS215Vision30.Copy .Range("A5")
>> Elseif .Range("T3")=2.16 then
>> MTS216Vision30.Copy .Range("A5")
>> End If
>>End WIth
>>..... but in time there will be 2.17, 2.18 etc and I am hoping to "future
>>proof" the code so that I only need to create the named ranges and add a
>>"Set" line rather than having to add more If conditions, as the Vision30
>>example is only one of a number of similar situations.
>>
>>Any ideas? I need this to work on any machine with a "default" Excel 2000
>>installation (ie not an add-in).



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy&Paste variable range based on cell value =?Utf-8?B?VGhvbWFz?= Microsoft Excel Programming 0 27th Jul 2007 06:40 PM
Referencing a named range based upon Range name entry in cell =?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?= Microsoft Excel Worksheet Functions 14 20th Jun 2007 07:19 PM
inserting a named range into new cells based on a named cell =?Utf-8?B?UGV0ZXIgUy4=?= Microsoft Excel Misc 1 4th Jun 2006 03:53 AM
Finding a named range based on cell value and copy/paste to same sheet? Simon Lloyd Microsoft Excel Programming 1 11th May 2006 11:25 PM
Re: Variable Named Range Andy B Microsoft Excel Worksheet Functions 0 17th Mar 2004 04:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:27 AM.