Use VBA to create required number of variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NDayave
    New Member
    • Feb 2007
    • 92

    #1

    Use VBA to create required number of variables

    How do,

    I want to be able to make a certain number of variables depending on the number of data items i have to be used.

    For example, i would need 3 variables defined when i have 3 numbers and 5 variables when i have 5 numbers.

    Is there any way in the VBA that I can define variables with variables in the name (ie: Dim Number & x as Double within a For Next loop). I can do this with the text boxes on the form, so I was wondering if I could do it with defining variables.

    I want to do it this way as the numbers are the overall Lap Time in a race and need to be assigned points (eg: 1st = 5 points 2nd = 3 points etc) and there is the possibility of any number of Race Times to be compared.

    Obviously if you have any other ideas they are much appreciated.

    Thanks in advance,

    NDayave
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by NDayave
    How do,

    I want to be able to make a certain number of variables depending on the number of data items i have to be used.

    For example, i would need 3 variables defined when i have 3 numbers and 5 variables when i have 5 numbers.

    Is there any way in the VBA that I can define variables with variables in the name (ie: Dim Number & x as Double within a For Next loop). I can do this with the text boxes on the form, so I was wondering if I could do it with defining variables.

    I want to do it this way as the numbers are the overall Lap Time in a race and need to be assigned points (eg: 1st = 5 points 2nd = 3 points etc) and there is the possibility of any number of Race Times to be compared.

    Obviously if you have any other ideas they are much appreciated.

    Thanks in advance,

    NDayave
    Don't think that this can be done, but what about a Dynamic Array to store the values, something like:
    Code:
    Dim lngCounter As Long
    Const conNUM_OF_RACES As Long = 4
    
    Dim alngRaces() As Long
    
    ReDim alngRaces(1 To 4)
    
    For lngCounter = 1 To conNUM_OF_RACES
      alngRaces(lngCounter) = lngCounter ^ 3
    Next
    
    'Playback the elements in the Array
    For lngCounter = LBound(alngRaces) To UBound(alngRaces)
      Debug.Print alngRaces(lngCounter)
    Next
    OUTPUT:
    Code:
    1 
    8 
    27 
    64

    Comment

    • NDayave
      New Member
      • Feb 2007
      • 92

      #3
      Thanks for the input,

      This appears to do what I need, the ReDim statement is what I was after.

      These kind of things are impossible to google.

      Thanks again,

      NDayave

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by NDayave
        Thanks for the input,

        This appears to do what I need, the ReDim statement is what I was after.

        These kind of things are impossible to google.

        Thanks again,

        NDayave
        You are quite welcome.

        Comment

        • nico5038
          Recognized Expert Specialist
          • Nov 2006
          • 3080

          #5
          ReDim is a rather "costly" statement.
          An alternative to using arrays is to switch to using collections. These don't require a ReDim and can be processed using a For Next loop.

          Nic;o)

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by nico5038
            ReDim is a rather "costly" statement.
            An alternative to using arrays is to switch to using collections. These don't require a ReDim and can be processed using a For Next loop.

            Nic;o)
            Hello Nico, just for curiosity, are you saying that it would be faster to dynamically Add/Remove Items from a Collection as opposed to dynamically Resizing an Array?

            Comment

            • nico5038
              Recognized Expert Specialist
              • Nov 2006
              • 3080

              #7
              The ReDim is "costly" because memory has to be re-allocated. A Collection holds coded methods to add/remove items and the FOR NEXT will make processing easy.
              Saw a nice piece of loop time testing by you in the "Insights" section, perhaps worthwhile to re-use <LOL>

              Nic;o)

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                Originally posted by nico5038
                The ReDim is "costly" because memory has to be re-allocated. A Collection holds coded methods to add/remove items and the FOR NEXT will make processing easy.
                Saw a nice piece of loop time testing by you in the "Insights" section, perhaps worthwhile to re-use <LOL>

                Nic;o)
                Thanks for the explanation Nico, always a learning experience around here (LOL).

                Comment

                Working...