Array List
Array List
The VBA ArrayList is a much better alternative to the built-in VBA Collection. It contains much
richer functionality such as sorting, converting to an array, removing all items etc.
Check out the quick guide for an overview of what the ArrayList does. The rest of this post
provides examples of how to use the ArrayList.
Contents [hide]
1 Quick Guide to the VBA ArrayList
2 Description
3 Declare and Create the VBA ArrayList
o 3.1 Late Binding
o 3.2 Early Binding
o 3.3 VBA ArrayList Automation Error
4 Adding Items to the VBA ArrayList
5 Reading through an ArrayList
6 Sorting
7 Cloning the VBA ArrayList
8 Copying from an VBA ArrayList to an Array
9 Copying to Arrays(2D)
10 Array to a VBA ArrayList(1D)
11 Array to VBA ArrayList(2D)
12 Remove All Items from the ArrayList
13 What’s Next?
14 Get the Free eBook
Late Binding
We use CreateObject to create the ArrayList using late binding:
Sub UsingArrayList()
End Sub
The disadvantage of late binding is that we don’t have access to the Intellisense. The advantage
is that it is better to use when distributing a VBA application to a user.
Early Binding
Early binding allows use to use the Intellisense to see what is available to use. We must first add
the type library as a reference and then select it from the reference list. We can use the following
steps to do this:
1. Select Tools and then References from the menu.
2. Click on the Browse.
3. Find the file mscorlib.tlb and click Open. It should be in a folder like
this C:\Windows\Microsoft.NET\Framework\v4.0.30319.
4. Scroll down the list and check mscorlib.dll.
5. Click Ok.
You can now use the following code to declare the ArrayList using early binding:
Sub AddingToList()
coll.Add "Apple"
coll.Add "Watermelon"
coll.Add "Pear"
coll.Add "Banana"
coll.Insert 0, "Plum"
End Sub
' Items must be basic data type e.g. Long, String, Double
Dim i As Long
For i = 0 To coll.Count - 1
Debug.Print coll(i)
Next i
End Sub
We can use the For Each loop with the VBA ArrayList just like we use it with a Collection:
' Items much be basic data type e.g. Long, String, Double
Debug.Print item
Next item
End Sub
Sorting
Sort will sort the VBA ArrayList in ascending order.
To sort in descending order simply use Reverse after Sort.
The following code shows an example of sorting in both ascending and descending order:
Sub Sorting()
coll.Add "Apple"
coll.Add "Watermelon"
coll.Add "Pear"
coll.Add "Banana"
coll.Add "Plum"
' Sort
coll.Sort
' Add this sub from "Reading through the items" section
PrintToImmediateWindow coll
' Reverse sort
coll.Reverse
PrintToImmediateWindow coll
End Sub
Dim i As Long
For i = 0 To coll.Count - 1
Debug.Print coll(i)
Next i
End Sub
Sub Cloning()
' Create the ArrayList
coll1.Add "Apple"
coll1.Add "Watermelon"
coll1.Add "Pear"
coll1.Add "Banana"
coll1.Add "Plum"
coll1.Clear
' Add PrintToImmediateWindow sub from "Reading through the items" section
PrintToImmediateWindow coll1
PrintToImmediateWindow coll2
End Sub
For i = 0 To coll.Count - 1
Debug.Print coll(i)
Next i
End Sub
Sub CopyToArray()
coll.Add "Apple"
coll.Add "Watermelon"
coll.Add "Pear"
coll.Add "Banana"
coll.Add "Plum"
arr = coll.ToArray
PrintArrayToImmediate arr
End Sub
Dim i As Long
Debug.Print arr(i)
Next i
End Sub
Copying to Arrays(2D)
Copying to Two-Dimensional arrays is very useful in VBA because we can then write them
directly to the Range of a worksheet.
There ArrayList doesn’t have a function that does this. However all is not lost, I have created a
function to convert the ArrayList to a two-dimensional array.
Dim i As Long
For i = 0 To coll.Count - 1
arr(i + 1, 1) = coll(i)
Next i
ToArray2D = arr
End Function
Sub ClearArrayList()
coll.Add "Apple"
coll.Add "Watermelon"
coll.Add "Pear"
coll.Add "Banana"
coll.Add "Plum"
arr = ToArray2D(coll)
Sheet1.Range("A1:A5") = arr
' We can also write directly to the range from the function
Sheet1.Range("C1:C5") = ToArray2D(coll)
End Sub
Array to a VBA ArrayList(1D)
As we have seen, there is an in-built function ToArray which will copy from an ArrayList to an
Array.
If we want to copy from an Array to an ArrayList we need to create our own function which I
have done below. Because we read through the items one at a time, it may be a bit slow if we
have a lot of data:
ret = -1
ret = UBound(arr, 2)
On Error Goto 0
End If
Dim i As Long
coll.Add arr(i)
Next i
Sub ReadFromArray1D()
arr(1) = "PeterJ"
arr(2) = "Jack"
arr(3) = "Jill"
PrintToImmediateWindow coll
End Sub
End If
Dim i As Long
coll.Add arr(i, 1)
Next i
End Function
Sub ReadFromArray()
PrintToImmediateWindow coll
End Sub
Remove All Items from the ArrayList
We can remove all the items from an ArrayList by using the Clear function:
Sub ClearArrayList()
coll.Add "Apple"
coll.Add "Watermelon"
coll.Add "Pear"
coll.Add "Banana"
coll.Add "Plum"
Debug.Print vbCrLf & "The number of items is: " & coll.Count
coll.Clear
End Sub
What’s Next?
Free VBA Tutorial If you are new to VBA or you want to sharpen your existing VBA skills
then why not try out the The Ultimate VBA Tutorial.