Variable Reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • HB

    Variable Reference

    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


  • Gary Miller

    #2
    Re: Variable Reference

    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" <frmnook@telus. net> wrote in message
    news:Jkfob.8671 8$EO3.31124@clg rps13...[color=blue]
    > After trying for days!! I need help:
    >
    > Is there any way I can refer to a variable with another[/color]
    variable.[color=blue]
    >
    > I.e.: I have many variables: var1 , var2 , var3 etc etc
    > I want to use a loop to change these variables, for this[/color]
    example double[color=blue]
    > 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
    >
    >[/color]


    Comment

    • MikeB

      #3
      Re: Variable Reference

      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" <frmnook@telus. net> wrote in message news:Jkfob.8671 8$EO3.31124@clg rps13...[color=blue]
      > 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
      >
      >[/color]


      Comment

      • Bruce M. Thompson

        #4
        Re: Variable Reference

        > Is there any way I can refer to a variable with another variable.[color=blue]
        >
        > 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[/color]

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

        '************EX AMPLE 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
        '************EX AMPLE END

        --
        Bruce M. Thompson, Microsoft Access MVP
        bthmpson@mvps.o rg (See the Access FAQ at http://www.mvps.org/access)[color=blue][color=green]
        >> NO Email Please. Keep all communications[/color][/color]
        within the newsgroups so that all might benefit.<<


        Comment

        • HB

          #5
          Re: Variable Reference

          Gary


          I get an error on

          var & lngVar = var & lngVar * 2


          thanks anyway!

          Brain

          "Gary Miller" <gmillerNO@SPAM .sistersnet.com > wrote in message
          news:upq5mCznDH A.1728@TK2MSFTN GP09.phx.gbl...[color=blue]
          > 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" <frmnook@telus. net> wrote in message
          > news:Jkfob.8671 8$EO3.31124@clg rps13...[color=green]
          > > After trying for days!! I need help:
          > >
          > > Is there any way I can refer to a variable with another[/color]
          > variable.[color=green]
          > >
          > > I.e.: I have many variables: var1 , var2 , var3 etc etc
          > > I want to use a loop to change these variables, for this[/color]
          > example double[color=green]
          > > 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
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • S.L.

            #6
            Re: Variable Reference

            Eval() function may help.

            "HB" <frmnook@telus. net> wrote in message
            news:Jkfob.8671 8$EO3.31124@clg rps13...[color=blue]
            > 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
            >
            >[/color]


            Comment

            • MikeB

              #7
              Re: Variable Reference


              "Bruce M. Thompson" <bthmpson@big_N OSPAM_foot.com> wrote in message
              news:OPw%232Izn DHA.2080@TK2MSF TNGP10.phx.gbl. ..[color=blue][color=green]
              > > 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[/color]
              >
              > So, why not just declare an array and you can refer to the array's index in
              > your loop:
              >[/color]

              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.
              [color=blue]
              > '************EX AMPLE 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
              > '************EX AMPLE END
              >
              > --
              > Bruce M. Thompson, Microsoft Access MVP
              > bthmpson@mvps.o rg (See the Access FAQ at http://www.mvps.org/access)[color=green][color=darkred]
              > >> NO Email Please. Keep all communications[/color][/color]
              > within the newsgroups so that all might benefit.<<
              >
              >[/color]


              Comment

              • Bruce M. Thompson

                #8
                Re: Variable Reference

                > Maybe the OP would have occasion to refer to an individual var instead of
                all of them as a group.[color=blue]
                > In such a case, the Var Name would be of consequence.[/color]

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

                --
                Bruce M. Thompson, Microsoft Access MVP
                bthmpson@mvps.o rg (See the Access FAQ at http://www.mvps.org/access)[color=blue][color=green]
                >> NO Email Please. Keep all communications[/color][/color]
                within the newsgroups so that all might benefit.<<


                Comment

                • Bruce M. Thompson

                  #9
                  Re: Variable Reference

                  > Eval() function may help.

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

                  :-)
                  --
                  Bruce M. Thompson, Microsoft Access MVP
                  bthmpson@mvps.o rg (See the Access FAQ at http://www.mvps.org/access)[color=blue][color=green]
                  >> NO Email Please. Keep all communications[/color][/color]
                  within the newsgroups so that all might benefit.<<


                  Comment

                  • MikeB

                    #10
                    Re: Variable Reference

                    d'Oh....


                    "Bruce M. Thompson" <bthmpson@big_N OSPAM_foot.com> wrote in message
                    news:eetN5X9nDH A.1740@TK2MSFTN GP12.phx.gbl...[color=blue][color=green]
                    > > Maybe the OP would have occasion to refer to an individual var instead of[/color]
                    > all of them as a group.[color=green]
                    > > In such a case, the Var Name would be of consequence.[/color]
                    >
                    > ??? Are you saying that referencing "Var(1)", rather than "Var1" would be of
                    > consequence? Please clarify your meaning.
                    >
                    > --
                    > Bruce M. Thompson, Microsoft Access MVP
                    > bthmpson@mvps.o rg (See the Access FAQ at http://www.mvps.org/access)[color=green][color=darkred]
                    > >> NO Email Please. Keep all communications[/color][/color]
                    > within the newsgroups so that all might benefit.<<
                    >
                    >[/color]


                    Comment

                    • Bruce M. Thompson

                      #11
                      Re: Variable Reference

                      ;-)

                      --
                      Bruce M. Thompson, Microsoft Access MVP
                      bthmpson@mvps.o rg (See the Access FAQ at http://www.mvps.org/access)[color=blue][color=green]
                      >> NO Email Please. Keep all communications[/color][/color]
                      within the newsgroups so that all might benefit.<<


                      Comment

                      • Rick Rothstein

                        #12
                        Re: Variable Reference

                        > > Maybe the OP would have occasion to refer to an individual var instead
                        of[color=blue]
                        > all of them as a group.[color=green]
                        > > In such a case, the Var Name would be of consequence.[/color]
                        >
                        > ??? Are you saying that referencing "Var(1)", rather than "Var1" would be[/color]
                        of[color=blue]
                        > consequence? Please clarify your meaning.[/color]

                        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


                        Comment

                        • Bruce M. Thompson

                          #13
                          Re: Variable Reference

                          > Actually, I got the impression that Var1, Var2, Var3, etc. were generic[color=blue]
                          > 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.[/color]

                          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>

                          --
                          Bruce M. Thompson, Microsoft Access MVP
                          bthmpson@mvps.o rg (See the Access FAQ at http://www.mvps.org/access)[color=blue][color=green]
                          >> NO Email Please. Keep all communications[/color][/color]
                          within the newsgroups so that all might benefit.<<


                          Comment

                          Working...