Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 112
Default Named ranges and variables

Hello,
I'd like to select a named range that can be different depending the
conditions, and
I'd like to use a variable to contain the named range but the statement
Worksheets("Docs").Range(CurRange).Select
won't accept the CurRange variable.
Let's say I have two named ranges: myRange1 ( for the range a1:b1) and
myRange2 (for the named (range) single cell a7). Depending of the conditions
I wan't to set CurRange to contain either myRange1 or myRange 2 and the
program to select the right range. (I need to use named ranges as the
position of the named ranges will change during time.)
Is there any good solution?

Best regards
Mats Samson

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default Named ranges and variables

Hi,

Dim CurRange as Range
Dim i as integer

i = 1 ' or i=2 ... set your condition

Set CurRange = Worksheets("Docs").Range("MyRange" & i)
CurRange.Select


HTH

"Mats Samson" wrote:

Hello,
I'd like to select a named range that can be different depending the
conditions, and
I'd like to use a variable to contain the named range but the statement
Worksheets("Docs").Range(CurRange).Select
won't accept the CurRange variable.
Let's say I have two named ranges: myRange1 ( for the range a1:b1) and
myRange2 (for the named (range) single cell a7). Depending of the conditions
I wan't to set CurRange to contain either myRange1 or myRange 2 and the
program to select the right range. (I need to use named ranges as the
position of the named ranges will change during time.)
Is there any good solution?

Best regards
Mats Samson

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 112
Default Named ranges and variables

Hello Toppers!
Sorry but I can't get it to work. Even if I follow your instruction exactly
it stops
at the Set....-line with error 1004.
Again, this is how it looks:
I have two named ranges in the sheet Docs. myRange1 for cell A16 and
myRange2 for cell M20. Conditionally, I can run Sub StartOne () procedure and
set the condition CurRange = "myRange1". If I instead run the Sub StartTwo ()
procedure the condition is set to CurRange= "myRange2". Both these two
procedures will jump to Sub Goal() and depending on the CurRange variable it
will (should) select the requested cell through a statement similar to
CurRange.Select

Can you solve it, please
Best regards
Mats

"Toppers" wrote:

Hi,

Dim CurRange as Range
Dim i as integer

i = 1 ' or i=2 ... set your condition

Set CurRange = Worksheets("Docs").Range("MyRange" & i)
CurRange.Select


HTH

"Mats Samson" wrote:

Hello,
I'd like to select a named range that can be different depending the
conditions, and
I'd like to use a variable to contain the named range but the statement
Worksheets("Docs").Range(CurRange).Select
won't accept the CurRange variable.
Let's say I have two named ranges: myRange1 ( for the range a1:b1) and
myRange2 (for the named (range) single cell a7). Depending of the conditions
I wan't to set CurRange to contain either myRange1 or myRange 2 and the
program to select the right range. (I need to use named ranges as the
position of the named ranges will change during time.)
Is there any good solution?

Best regards
Mats Samson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Named ranges and variables

Hi Mats,

Perhaps something like:

Sub Tester02()
Dim CurRange As Range

' Define the 2 named ranges
With Worksheets("Docs")
.Range("A1:B1").Name = "myRange1"
.Range("A7").Name = "myRange2"
End With

If Range("D1") 10 Then '<=== Change condition to suit!
Set CurRange = Range("myrange1")
Else
Set CurRange = Range("myrange2")
End If

Application.Goto CurRange

End Sub


---
Regards,
Norman



"Mats Samson" wrote in message
...
Hello,
I'd like to select a named range that can be different depending the
conditions, and
I'd like to use a variable to contain the named range but the statement
Worksheets("Docs").Range(CurRange).Select
won't accept the CurRange variable.
Let's say I have two named ranges: myRange1 ( for the range a1:b1) and
myRange2 (for the named (range) single cell a7). Depending of the
conditions
I wan't to set CurRange to contain either myRange1 or myRange 2 and the
program to select the right range. (I need to use named ranges as the
position of the named ranges will change during time.)
Is there any good solution?

Best regards
Mats Samson



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 112
Default Named ranges and variables

Hi Norman,
no unfortunately not. It won't work with a predefined range as it becomes
static when written in the code. Later, when you've changed position of the
range then you have to edit the code and change the range. That's why I'm
using named ranges in the code, because you can redefine a named range in
Insert/Name/Define...without changing the code.
My problem is that I want the code to change the named range to the current
position and different start- and goal- ranges are used depending on previous
conditions. By branching and narrowing/closing (I don't know the right
expression) the procedures you can shorten and (re)use the code for different
purposes or conditions.
Thanks anyway!
Mats

"Norman Jones" wrote:

Hi Mats,

Perhaps something like:

Sub Tester02()
Dim CurRange As Range

' Define the 2 named ranges
With Worksheets("Docs")
.Range("A1:B1").Name = "myRange1"
.Range("A7").Name = "myRange2"
End With

If Range("D1") 10 Then '<=== Change condition to suit!
Set CurRange = Range("myrange1")
Else
Set CurRange = Range("myrange2")
End If

Application.Goto CurRange

End Sub


---
Regards,
Norman



"Mats Samson" wrote in message
...
Hello,
I'd like to select a named range that can be different depending the
conditions, and
I'd like to use a variable to contain the named range but the statement
Worksheets("Docs").Range(CurRange).Select
won't accept the CurRange variable.
Let's say I have two named ranges: myRange1 ( for the range a1:b1) and
myRange2 (for the named (range) single cell a7). Depending of the
conditions
I wan't to set CurRange to contain either myRange1 or myRange 2 and the
program to select the right range. (I need to use named ranges as the
position of the named ranges will change during time.)
Is there any good solution?

Best regards
Mats Samson






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Named ranges and variables

Hi Mats,

The code portion:

' Define the 2 named ranges
With Worksheets("Docs")
.Range("A1:B1").Name = "myRange1"
.Range("A7").Name = "myRange2"
End With


Was only included for demo purposes. In your situation, two ranges would be
set elsewhere.

Simply delete this portion from the demo.

---
Regards,
Norman



"Mats Samson" wrote in message
...
Hi Norman,
no unfortunately not. It won't work with a predefined range as it becomes
static when written in the code. Later, when you've changed position of
the
range then you have to edit the code and change the range. That's why I'm
using named ranges in the code, because you can redefine a named range in
Insert/Name/Define...without changing the code.
My problem is that I want the code to change the named range to the
current
position and different start- and goal- ranges are used depending on
previous
conditions. By branching and narrowing/closing (I don't know the right
expression) the procedures you can shorten and (re)use the code for
different
purposes or conditions.
Thanks anyway!
Mats

"Norman Jones" wrote:

Hi Mats,

Perhaps something like:

Sub Tester02()
Dim CurRange As Range

' Define the 2 named ranges
With Worksheets("Docs")
.Range("A1:B1").Name = "myRange1"
.Range("A7").Name = "myRange2"
End With

If Range("D1") 10 Then '<=== Change condition to suit!
Set CurRange = Range("myrange1")
Else
Set CurRange = Range("myrange2")
End If

Application.Goto CurRange

End Sub


---
Regards,
Norman



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
Cell Variables and Ranges jcottam Excel Discussion (Misc queries) 2 March 31st 06 07:33 PM
Using named variables Jessica Excel Discussion (Misc queries) 5 August 11th 05 09:56 PM
Like 123, allow named ranges, and print named ranges WP Excel Discussion (Misc queries) 1 April 8th 05 06:07 PM
named ranges - changing ranges with month selected gr8guy Excel Programming 2 May 28th 04 04:50 AM
using variables to set ranges Dave Marden[_2_] Excel Programming 1 December 2nd 03 01:24 AM


All times are GMT +1. The time now is 08:45 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"