425,635 Members | 911 Online
Need help? Post your question and get tips & solutions from a community of 425,635 IT Pros & Developers. It's quick & easy.

# Variable Reference

 P: n/a 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 Nov 12 '05 #1
12 Replies

 P: n/a 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 "HB" wrote in message news:Jkfob.86718\$EO3.31124@clgrps13... 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 Nov 12 '05 #2

 P: n/a 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 "HB" wrote in message news:Jkfob.86718\$EO3.31124@clgrps13... 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 Nov 12 '05 #3

 P: n/a > 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 .... ... 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 -- Bruce M. Thompson, Microsoft Access MVP bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access) NO Email Please. Keep all communications within the newsgroups so that all might benefit.<< Nov 12 '05 #4

 P: n/a Gary I get an error on var & lngVar = var & lngVar * 2 thanks anyway! Brain "Gary Miller" wrote in message news:up**************@TK2MSFTNGP09.phx.gbl... 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 "HB" wrote in message news:Jkfob.86718\$EO3.31124@clgrps13... 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 Nov 12 '05 #5

 P: n/a Eval() function may help. "HB" wrote in message news:Jkfob.86718\$EO3.31124@clgrps13... 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 Nov 12 '05 #6

 P: n/a "Bruce M. Thompson" wrote in message news:OP****************@TK2MSFTNGP10.phx.gbl... 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: 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. '************EXAMPLE START Dim dblMyVar(1 To 4) As Double ... ... 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 -- Bruce M. Thompson, Microsoft Access MVP bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access) NO Email Please. Keep all communications within the newsgroups so that all might benefit.<< Nov 12 '05 #7

 P: n/a > 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. -- Bruce M. Thompson, Microsoft Access MVP bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access) NO Email Please. Keep all communications within the newsgroups so that all might benefit.<< Nov 12 '05 #8

 P: n/a > Eval() function may help. Won't work with VBA variable names. It *would* have made things easier, though. :-) -- Bruce M. Thompson, Microsoft Access MVP bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access) NO Email Please. Keep all communications within the newsgroups so that all might benefit.<< Nov 12 '05 #9

 P: n/a d'Oh.... "Bruce M. Thompson" wrote in message news:ee**************@TK2MSFTNGP12.phx.gbl... 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. -- Bruce M. Thompson, Microsoft Access MVP bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access) NO Email Please. Keep all communications within the newsgroups so that all might benefit.<< Nov 12 '05 #10

 P: n/a ;-) -- Bruce M. Thompson, Microsoft Access MVP bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access) NO Email Please. Keep all communications within the newsgroups so that all might benefit.<< Nov 12 '05 #11

 P: n/a > > 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. 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 Nov 12 '05 #12

 P: n/a > 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. -- Bruce M. Thompson, Microsoft Access MVP bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access) NO Email Please. Keep all communications within the newsgroups so that all might benefit.<< Nov 12 '05 #13

### This discussion thread is closed

Replies have been disabled for this discussion.