Increment a Variable?

G

Guest

I am looping on an varying number of items and would like to assign a
different variable to each. Possible to increment a variable and assign the
unique variable on each loop? If so, how would I do it?

For example, I may have 5 items that I want to set as a myvar1, myvar2, ...
myvar5 on one loop, but the next time I may have 15 items (myvar1 ...
myvar15) the next time.

Any help is greatly appreciated. Thanks.

Gary
 
D

Dirk Goldgar

G said:
I am looping on an varying number of items and would like to assign a
different variable to each. Possible to increment a variable and
assign the unique variable on each loop? If so, how would I do it?

For example, I may have 5 items that I want to set as a myvar1,
myvar2, ... myvar5 on one loop, but the next time I may have 15 items
(myvar1 ... myvar15) the next time.

Any help is greatly appreciated. Thanks.

You can't do that in any practical way, but you can use an array:

Dim MyArray(100) As Variant ' or as a specific type
Dim I As Integer

Do While <some condition>

If I >= UBound(MyArray) Then
MsgBox "Oops! Too many items!"
Exit Do
Else
I = I + 1
MyArray(I) = <some value>
End If

Loop
 
G

Guest

Have you thought about using an array?

See if this helps

Dim strAnyString() As String
Dim X, I As Integer

X = 5 'Number of variables

ReDim strAnyString(X) 'Redimension variable for X variables
'ReDim Preserve strAnyString(X) 'Redimension variable for X variables and
save existing values

For I = 0 To X - 1
strAnyString(I) = "Test " & I + 1
Next

For I = 0 To X - 1
Debug.Print strAnyString(I)
Next
 
G

Guest

Or:
Dim MyArray(100) As Variant ' or as a specific type
Dim I As Integer

Do While <some condition>

If I >= UBound(MyArray) Then
Redim MyArray(I+1) Preserve
End If
I = I + 1
MyArray(I) = <some value>

Loop
 
D

Dirk Goldgar

Klatuu said:
Or:
Dim MyArray(100) As Variant ' or as a specific type
Dim I As Integer

Do While <some condition>

If I >= UBound(MyArray) Then
Redim MyArray(I+1) Preserve
End If
I = I + 1
MyArray(I) = <some value>

Loop

Dim MyArray(100) As Variant

to this:

Dim MyArray() As Variant

' ...

ReDim MyArray(100) ' maybe a good starting size

or just this:

ReDim MyArray(100) ' to start with
 
G

Guest

No, it does not have to be a Variant type, but you can't change data types
with a redim unless the array is contained in a Variant.
 
D

Dirk Goldgar

Klatuu said:
No, it does not have to be a Variant type, but you can't change data
types with a redim unless the array is contained in a Variant.

No, that wasn't my point. My point was that if you declare the array as
non-dynamic (by using Dim with specific bounds), you won't be able to
use ReDim to change the bounds.
 
G

Guest

Sorry, Dirk, I misunderstood. You are correct.

Dirk Goldgar said:
No, that wasn't my point. My point was that if you declare the array as
non-dynamic (by using Dim with specific bounds), you won't be able to
use ReDim to change the bounds.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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