LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
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


 
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
syntax when using a variable in a formula anny Excel Programming 2 May 8th 06 04:09 PM
syntax for variable adamaagard Excel Programming 2 January 20th 06 12:45 AM
SaveAs syntax when using a variable Keith Excel Programming 3 November 4th 03 05:48 PM
Syntax for variable search Tom Ogilvy Excel Programming 3 August 6th 03 06:16 PM
Syntax for variable search brym Excel Programming 0 August 6th 03 05:41 PM


All times are GMT +1. The time now is 02:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"