Variable Reference

H

HB

After trying for days!! I need help:

Is there any way I can refer to a variable with another variable.

I.e.: I have many variables: var1 , var2 , var3 etc etc
I want to use a loop to change these variables, for this example double
their value
instead of writing
var1 = var1 * 2
var2 = var2 * 2
var3 = var3 * 2
var4 = var4 * 2
etc etc
be nice if I could do the following
for each a in array(var1,var2,var3 etc etc):a = a*2:next

Thanks in advance
 
G

Gary Miller

How about something like...

Dim lngVar as Long, lngLastVar as Long

lngVar = 1
' You need to put the number of your highest variable here
lngLastVar = 56

Do until lngVar > lngLastVar
' var1 = var1 * 2
var & lngVar = var & lngVar * 2
' Increment the var number
lngVar = lngVar + 1

Loop

Gary Miller
Sisters, OR
 
M

MikeB

Private Sub ColInts()
Dim var1 As Integer
Dim var2 As Integer
Dim var3 As Integer
Dim var4 As Integer
Dim colVar As New Collection
Dim a As Variant

var1 = 1
var2 = 1
var3 = 1
var4 = 1

colVar.Add var1, "var1"
colVar.Add var2, "var2"
colVar.Add var3, "var3"
colVar.Add var4, "var4"

For Each a In colVar
Debug.Print a
CalcVar a, 2
Debug.Print a
Next a
End Sub
Private Sub CalcVar(ByRef iVar As Variant, ByVal iMultiply As Integer)
iVar = iVar * iMultiply
End Sub
 
B

Bruce M. Thompson

Is there any way I can refer to a variable with another variable.
I.e.: I have many variables: var1 , var2 , var3 etc etc
I want to use a loop to change these variables, for this example double
their value
instead of writing
var1 = var1 * 2
var2 = var2 * 2
var3 = var3 * 2
var4 = var4 * 2
etc etc
be nice if I could do the following
for each a in array(var1,var2,var3 etc etc):a = a*2:next

So, why not just declare an array and you can refer to the array's index in
your loop:

'************EXAMPLE START
Dim dblMyVar(1 To 4) As Double
.... <assign values to array's rows> ...

Dim iCount As Integer 'To hold array's index values in For ... Next
'Manipulate values in array
For iCount = 1 to Ubound(dblMyVar()) '1 To 4, in this case
dblMyVar(iCount) = dblMyVar(iCount) * 2
Next
'************EXAMPLE END
 
M

MikeB

Bruce M. Thompson said:
So, why not just declare an array and you can refer to the array's index in
your loop:

Maybe the OP would have occasion to refer to an individual var instead of all of them as a group.
In such a case, the Var Name would be of consequence.
 
B

Bruce M. Thompson

Maybe the OP would have occasion to refer to an individual var instead of
all of them as a group.
In such a case, the Var Name would be of consequence.

??? Are you saying that referencing "Var(1)", rather than "Var1" would be of
consequence? Please clarify your meaning.
 
B

Bruce M. Thompson

Eval() function may help.

Won't work with VBA variable names. It *would* have made things easier, though.

:)
 
R

Rick Rothstein

Maybe the OP would have occasion to refer to an individual var instead
of
all of them as a group.

??? Are you saying that referencing "Var(1)", rather than "Var1" would be of
consequence? Please clarify your meaning.

Actually, I got the impression that Var1, Var2, Var3, etc. were generic
names for the posting example only and that the OP was using more project
specific names that didn't share a common "root" like Var. However, if that
is the case, then I think the OP will have trouble doing what he asked for.

Rick - MVP
 
B

Bruce M. Thompson

Actually, I got the impression that Var1, Var2, Var3, etc. were generic
names for the posting example only and that the OP was using more project
specific names that didn't share a common "root" like Var. However, if that
is the case, then I think the OP will have trouble doing what he asked for.

Agreed. I provided a solution that would allow for performing the type of
operation the OP suggested within a loop, but it may not fit his particular
requirement. Of course, he could always rewrite his application to fit my
solution. <G>
 

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