PC Review


Reply
Thread Tools Rate Thread

Declaring variables programmatically

 
 
Herman
Guest
Posts: n/a
 
      13th Dec 2010
Hello,
In some procedure, I use an array lilke my Array =
(100,160,200,280,290,.........560), some 50 numbers in all.
Furthere down the sub I need to create Ranges like R100, R160, R200,
R280.....R560, based on the numbers in the array.

Is there any smart way to do this without having to write 50 range
declarations and then 50 Set-commands manually?
Thank you very much for any help!
Herman
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      13th Dec 2010
You don't have to use individual variables for the range names. You could use a
second array:

Option Explicit
Sub testme()

Dim myArr As Variant 'array of longs
Dim myRanges() As Range 'array of ranges
Dim iCtr As Long 'looping variables

myArr = Array(100, 160, 200) 'just 3

'make myRanges the same size as myArr
ReDim myRanges(LBound(myArr) To UBound(myArr))

'I'm not sure what you're doing here, though:
For iCtr = LBound(myRanges) To UBound(myRanges)
'Set myRanges(iCtr) = somerangebasedonmyarr
'maybe...
Set myRanges(iCtr) = Worksheets("Sheet1").Range("R" & myArr(iCtr))
Next iCtr

End Sub


=========
Another option would be to look at the Type statement in VBA's help:

Option Explicit
Type myType
myNum As Long
myRng As Range
End Type
Sub testme()

Dim myArr(1 To 3) As myType
Dim iCtr As Long

myArr(1).myNum = 100
myArr(2).myNum = 160
myArr(3).myNum = 200

For iCtr = LBound(myArr) To UBound(myArr)
'still not sure what you're doing here
Set myArr(iCtr).myRng _
= Worksheets("Sheet1").Range("R" & myArr(iCtr).myNum)
Next iCtr

End Sub



On 12/13/2010 07:00, Herman wrote:
> Hello,
> In some procedure, I use an array lilke my Array =
> (100,160,200,280,290,.........560), some 50 numbers in all.
> Furthere down the sub I need to create Ranges like R100, R160, R200,
> R280.....R560, based on the numbers in the array.
>
> Is there any smart way to do this without having to write 50 range
> declarations and then 50 Set-commands manually?
> Thank you very much for any help!
> Herman


--
Dave Peterson
 
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
Declaring variables ericb Microsoft Access 9 23rd Feb 2010 05:26 PM
Declaring Variables Twice? Chad Microsoft Excel Programming 2 19th May 2009 11:48 PM
help in declaring variables =?Utf-8?B?QXNoYQ==?= Microsoft ASP .NET 2 31st Dec 2004 05:18 AM
Declaring variables Pedro Microsoft Excel Programming 1 13th Nov 2003 03:32 PM
Declaring Variables Berny Microsoft Access VBA Modules 4 10th Nov 2003 03:20 AM


Features
 

Advertising
 

Newsgroups
 


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