简体   繁体   中英

Array in excel vba

I want to have an array list in vba, hence I have a variant declared in excel vba like:

Dim Students(10) as variant

Now I want to store numbers in Students list. the numbers are not continuous. Sometime like:

Students(2,7,14,54,33,45,55,59,62,66,69)

How can I do this in vba? also how can I access the list items?

Students must be declared as a dynamic array. That is, an array whose bounds can be changed. Dim Students(10) gives an array whose bounds cannot be changed and cannot be loaded from an array.

Dim Students() As Variant

To load Students:

Students = Array(2,7,14,54,33,45,55,59,62,66,69)

To access the elements:

Dim Inx As Long

For Inx = LBound(Students) to UBound(Students)
  Debug.Print Students(Inx)
Next

LBound (Lower bound) and UBound mean that the for loop adjusts to the actual number of elements in Students.

This is too complex for you right now, and you'll probably never run into a situation where you'll need this, but:

I use the following method for forming more memory-efficient arrays (because Variant uses the most memory of any variable type) while still having the convenience of declaring the array contents in one line. To follow your example:

Dim Students() As Long
Dim Array2() As String

Array2() = Split("2,7,14,54,33,45,55,59,62,66,69", ",")

ReDim Array1(0) As Long
For Loop1 = LBound(Array2()) To UBound(Array2())
    ReDim Preserve Array1(0 To (UBound(Array1) + 1)) As String
    Array1(Loop1) = Array2(Loop1)
Next Loop1
ReDim Preserve Array1(0 To (UBound(Array1) - 1)) As Long

Erase Array2

An example of accessing it would be something like:

For Loop1 = LBound(Students) to UBound(Students)
    Msgbox Students(Loop1)
Next Loop1

I learned this from here: http://www.vbforums.com/showthread.php?669265-RESOLVED-VBA-Excel-Assigning-values-to-array-in-a-single-line&p=4116778&viewfull=1#post4116778

You can add values to an Array like this...

For i = 1 to 10
    Students(i) = i
Next i

Or like this

Students = Array(2,7,14,54,33,45,55,59,62,66,69)

Then you can access the values in the same manor. Note if you use the second option you'll need to declare it as follows:

Dim Students() As Variant

Well, That depends on how you would supply the values for the array, would you get the values from Worksheet.Range or from TextBox or ListBox , But basically the code would be something like that :

Dim students(10) as Integer
Dim Carrier as Integer
For i = LBound(students) To UBound(Students)
     'some code to get the values you want to from whatever is your source
     'then assign the value to Carrier

     students(i)=Carrier
Next i

It is not good practice to dim an array as Variant when you certainly know that you are going to use integers only, as it will eat alot of memory that is not needed in the first place. You also should be aware of the bounds of the numbers that are going to be assigned, if it exceeds the Integer limit then you should use Double or Float. This is my first participation in the site,Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM