View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Norman Jones[_2_] Norman Jones[_2_] is offline
external usenet poster
 
Posts: 421
Default Variable Syntax

Hi Minitman,

For one possible approach, try something
like:

'==========
Public Sub Demo()
Dim rRng As Range
Dim i As Long
For i = 1 To 3
Set rRng = myRange(i)
MsgBox rRng.Address(0, 0)
Next i
End Sub

'--------------
Public Function myRange(iArg As Long) As Range
Dim Rng1 As Range
Dim Rng2 As Range
Dim Rng3 As Range
Dim arr As Variant
Dim rRng As Range
Dim i As Long

Set Rng1 = Range("A1:A10")
Set Rng2 = Range("A20:A25")
Set Rng3 = Range("A30:A40")

arr = Array(Rng1, Rng2, Rng3)
Set myRange = arr(iArg - 1)

End Function
'<<==========


As an alternative to the array, you could
replace the function with the following
version:

'==========
Public Function myRange(iArg As Long) As Range
Dim Rng1 As Range
Dim Rng2 As Range
Dim Rng3 As Range
Dim myColl As Collection
Dim rRng As Range
Dim i As Long

Set Rng1 = Range("A1:A10")
Set Rng2 = Range("A20:A25")
Set Rng3 = Range("A30:A40")

Set myColl = New Collection

myColl.Add Rng1, Rng1.Address
myColl.Add Rng2, Rng2.Address
myColl.Add Rng3, Rng3.Address

Set myRange = myColl(iArg)

End Function
'<<==========




---
Regards.
Norman


"Minitman" wrote in message
...
Greeting,

I am confused over how to make a variable to be variable.

I am running Office 2003 on an XP Pro machine

I have a set of 21 variables called Rng1, Rng2, ... Rng21. these
variable are equal to named ranges on a sheet. So far so good.

I am trying to eliminate the use of select case in this procedure. It
appears that I have the syntax wrong.

Here is one of the case statements:

Sub DoThis(iArg as Integer)
Select Case iArg
...
Case 6
Set rRng = Rng6
Set cmBox = TB6
...

I tried:

Set rRng = Range("Rng" & iArg)

And got the following error on this line:

Run-time error '1004':
Method 'Range' of object '_Global' failed.

The Rng's are dimmed as Range.

Anyone have any idea what I did wring? Or on how to fix it?

Any help is appreciated.

-Minitman