473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Use VBA to create required number of variables

92
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
Dec 27 '08 #1
7 8158
ADezii
8,834 Expert 8TB
@NDayave
Don't think that this can be done, but what about a Dynamic Array to store the values, something like:
Expand|Select|Wrap|Line Numbers
  1. Dim lngCounter As Long
  2. Const conNUM_OF_RACES As Long = 4
  3.  
  4. Dim alngRaces() As Long
  5.  
  6. ReDim alngRaces(1 To 4)
  7.  
  8. For lngCounter = 1 To conNUM_OF_RACES
  9.   alngRaces(lngCounter) = lngCounter ^ 3
  10. Next
  11.  
  12. 'Playback the elements in the Array
  13. For lngCounter = LBound(alngRaces) To UBound(alngRaces)
  14.   Debug.Print alngRaces(lngCounter)
  15. Next
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. 27 
  2. 64
Dec 27 '08 #2
NDayave
92
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
Dec 27 '08 #3
ADezii
8,834 Expert 8TB
@NDayave
You are quite welcome.
Dec 28 '08 #4
nico5038
3,080 Expert 2GB
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)
Dec 28 '08 #5
ADezii
8,834 Expert 8TB
@nico5038
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?
Dec 29 '08 #6
nico5038
3,080 Expert 2GB
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)
Dec 29 '08 #7
ADezii
8,834 Expert 8TB
@nico5038
Thanks for the explanation Nico, always a learning experience around here (LOL).
Dec 29 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: PHP2 | last post by:
I have one large number with 18 decimal places for license key. have any way that I transfer my number into shorter number or string with some method?
2
by: chrispycrunch | last post by:
How do I output a row number for a table solely for the purpose of querying for a unique row? In my problem, the table from a legacy system does not have a primary key, so it limits various...
1
by: Tom Cusick | last post by:
We have a Job Shop database. When I get an order in I put all the line item information into the database. Some orders have multiple lines and most of the information is the same. (eg. Customer...
0
by: skinnybloke | last post by:
Hi I have written 3 seperate macros in access to run 3 queries to export 3 .xls files. Sometimes I will need to run them individually. Sometimes I will need to run them all together...
3
by: brian.a.kennedy | last post by:
I am a rookie and am looking to create records based on a field value. For example - I currently have sales data for three employees: Employee 1 has sold 12 units, EE 2 has 8 and EE 3 has 10....I...
3
by: Mr. Ken | last post by:
Sorry for the non-C++ topic, but it's most easy to get answer from here I guess. How do I create random numbers (integer from 0~9999) in Unix C-shell? I am writing a script which randomly...
6
by: Arne Beruldsen | last post by:
I have a windows app using vb.net 2005. I would like to create a serial number based on a couple of characteristics of the customers computer (computer name, HD number..etc). I don't need...
1
by: Deven Oza | last post by:
Hello everyone please help me to Write a complete program USING EITHER FOR LOOP OR A WHILE LOOP that will display the first n numbers of the following infinite sequence: 1 , 3 , 6 , 10 , 15 ,...
2
by: nickyazura | last post by:
Hi, I would like to ask you regarding creating file name by adding unique number where it can automatically generated Dim sFileE As String sFileE = ".csv" sFileName = "Weld_In_Record" &...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.