Multiple Variables' Declaration In Loops

F

Faraz A. Qureshi

Any idea how to declare multiple variables depending upon the times looping
shall be carried on.

An idea may be grasped by my unfit CoDe as below:

Sub test()
Dim Counter As Long
Counter = Application.InputBox("How Many Ranges?", , , , , , , 1)
For x = 1 To Counter
Dim Range & x As range
set Range & x = application.InputBox("Select Range " & x)
Next
For Z = 1 To Counter
msgbox "Range "& Z " comprised of " & (Range & Z).address
Next
End Sub

Please help me out by rectifying the above code, or designing a similar code
or referring to a site, for which I shall be grateful.

Thanx in advance.
 
R

Rick Rothstein

You should be using an (dynamic) array. You would declare it like this...

Dim MyRange() As Range

then ReDim it to the correct size after you know how big to make it (that
is, after the Counter variable is set). Then you use the loop counter as the
index for the array. Something like this is what I think you are looking
for...

Sub test()
Dim X As Long, Counter As Long
Dim MyRange() As Range
Counter = Application.InputBox("How Many Ranges?", , , , , , , 1)
ReDim MyRange(1 To Counter)
For X = 1 To Counter
Set MyRange(X) = Application.InputBox("Select Range(" & _
X & ")", , , , , , , 8)
Next
For X = 1 To Counter
MsgBox "MyRange(" & X & ") comprised of " & MyRange(X).Address
Next
End Sub
 
P

p45cal

Sub test()
Dim RangesArray()
Dim Counter As Long
Counter = Application.InputBox("How Many Ranges?", , , , , , , 1)
ReDim RangesArray(Counter)
For x = 1 To Counter
Set RangesArray(x) = Application.InputBox("Select Range", Type:=8)
Next x
For Z = 1 To Counter
MsgBox "Range " & Z & " comprised of " & RangesArray(Z).Address
Next
End Su
 
S

Sam Wilson

sub somethinglikethis()

dim l as long
dim x as long
dim aStr() as string

l = Application.InputBox("How Many Ranges?", , , , , , , 1)
redim preserve aStr(l)

for x = 1 to l
aStr(x) = application.InputBox("Select Range " & x)
next x

for x = 1 to l
msgbox "Range " & x & " adress is " & astr(x)
next x

end sub
 
F

Faraz A. Qureshi

Concept of ReDIM was XClent 4 sure pal!

Thanx again.
--
Best Regards,

Faraz


Rick Rothstein said:
You should be using an (dynamic) array. You would declare it like this...

Dim MyRange() As Range

then ReDim it to the correct size after you know how big to make it (that
is, after the Counter variable is set). Then you use the loop counter as the
index for the array. Something like this is what I think you are looking
for...

Sub test()
Dim X As Long, Counter As Long
Dim MyRange() As Range
Counter = Application.InputBox("How Many Ranges?", , , , , , , 1)
ReDim MyRange(1 To Counter)
For X = 1 To Counter
Set MyRange(X) = Application.InputBox("Select Range(" & _
X & ")", , , , , , , 8)
Next
For X = 1 To Counter
MsgBox "MyRange(" & X & ") comprised of " & MyRange(X).Address
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top