Tag Archives: data structure

VBA Collection

VBA Collection Tutorial

1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 4.10 out of 5)
Loading...

The VBA Collection is a simple native data structure available in VBA to store (collect as you wish) objects. VBA Collections are more flexible than VBA Arrays as they are not limited in their size at any point in time and don’t require manual re-sizing. Collections are also useful when you don’t want to leverage there more complex (but quite similar) Data Structures like the VBA ArrayList or even a VBA Dictionary.

Adding items to a VBA Collection

Let’s start by creating a New VBA Collection and adding some items to it.

Notice that the VBA Collection is not typed in any way as we didn’t have to declare what types of objects it stores. This is because a VBA Collection stores object of type Variant.

By default the Add procedure will push items to the end of the VBA Collection. However, you can also choose to insert items before or after any index in the Collection like so:
Before:

After:

If you want to be able to reference a particular item in your VBA Collection by a certain string/name you can also define a key for object added to your VBA Collection like so:

Removing items from a VBA Collection

Removing items from a VBA Collection is equally easy. However, items are removed by specifying their index.

Be careful when removing items from a VBA Collection – items start indexing at 1 (not 0 as in most common programming languages.

When removing items in a loop do remember that the index of the remaining items in the VBA Collection will decrease by 1.

Clearing a VBA Collection

To Clear all items (remove them) from a VBA Collection use the Clear function.

Clearing all items from a Collection is similar to recreating a VBA Collection:

Counting items in a VBA Collection

Similarly as with other VBA Data Structures the VBA Collection facilitates the Count function which returns the amount of items currently stored.

Getting items in a VBA Collection

To get a specific item in your VBA Collection you need to either specify it’s index or optionally it’s key (if it was defined).

Traversing a VBA Collection

As with VBA Arrays you can similarly traverse a VBA Collection using loops such as For, While etc. by indexing it’s items, or by traversing its items using the For Each loop (preferred).

Check if VBA Collection contains item

Unfortunately, the VBA Collection object does not facilitate a Contains function. We can however easily write a simple Function that will extend this functionality. Feel free to use the Function below:

Usage example:

Convert VBA Collection to VBA Array

In some case you may want to convert your VBA Collection to a VBA Array e.g. if you would want to paste items from your Collection to a VBA Range. The Code below does that exactly that:

Below a test of how it works:

Summary

The VBA Collection is an extremely useful data type in VBA. It is more versatile than the VBA Array allowing you to add and remove items more freely. The Key-Value store works also similarly as for the VBA Dictionary making it a useful alternative.

As an exercise – next time you will consider using a VBA Array consider replacing it with the Collection object. Hopefully it will guest in your VBA Code more often.

vba array

The VBA Array tutorial

1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 4.13 out of 5)
Loading...

The VBA Array is a very convenient and efficient for storing multiple items of usually the same data type. The size of a VBA Array can be either fixed or dynamic depending on how it is declared. VBA Arrays can also be 1 or multi-dimensional.

linkNavigate
one dimensional VBA array
multi dimensional vba arrays
vba fixed array
vba dynamic array
sizing and moving VBA array
vba array functions

One-dimensional VBA Array

A one-dimensional VBA Array contains a sequence of elements within a single dimension. The default numbering sequence of these elements starts at 0. You can redefine the starting and ending index of the sequence of elements using the X to Y statement. See an example below:

One-dimensional VBA Array
One-dimensional VBA Array

Let’s start by introducing a simple one-dimensional VBA Array of 10 items:

Notice that I have sized the array for indices 1 to 10. This is not a required statements as I can also declare the VBA Array size without the index range.

Another example of 10 item array indexed from 2 to 11.

Multi-dimensional VBA Array

A multi-dimensional VBA Array contains a sequence of elements within multiple dimensions (as many as defined). The default numbering sequence of these elements, within each dimension, starts at 0. You can redefine the starting and ending index of the sequence of elements using the X to Y statement. See an example below:

Two-dimensional VBA Array
Two-dimensional VBA Array

In some cases you might not want to limit yourself to just 1 dimension. VBA Arrays can have multiple dimensions. See a simple example below:

In case of dynamic arrays (read on) multidimentional arrays in VBA have proven problems with using the Preserve statement to preserve the arrays contents when resizing it. For more information on how to workaround this problem see this post here.

Fixed VBA array

The VBA Arrays above were all of fixed size i.e. their size was defined when declaring the VBA Array using the Dim statement. Let’s quickly compare fixed and dynamic sized arrays.

Fixed size arrays should be used when you know before execution how many items your VBA Array should contain. Fixed arrays are equivalent in usage to dynamic arrays although simply have the limit of being un-resizable.

Dynamic VBA array

Dynamic sized VBA Arrays are arrays that can be sized and re-sized even multiple times. This is useful when you are either not sure before execution what the required size of your VBA Array should be or you want to optimize memory by allocating a large sized array for only a short period of time. Let’s start with a simple example:

If you want to set your own index range feel free to use the ReDim statement as such:

ReDim Statement – Resizing a Dynamic VBA Array

The ReDim statement is used to set the size of your VBA Array or to resize it if needed. See an example below.

The problem with a regular ReDim is that your previous items will get erased. When you want to resize an VBA Array of existing items you need to use ReDim with Preserve. See an example below:

Erasing Dynamic VBA Arrays

Dynamic VBA Arrays can contain large amounts of data that can use a lot memory space. You can erase (free memory from) a Dynamic VBA Array by using the Erase statement. It is important therefore to wipe out the Array in a proper manner. For this use the Erase procedure like so:

Sizing and moving through Arrays

Sizing VBA Arrays

VBA Arrays can be sized by either specifying the size of the array (dimension) or by specifying the index range of the VBA Array. I can definitely recommend the first approach as being more practical and less error prone.

VBA Array size

To get the size of your VBA Array use the following function:

Getting the size of VBA Arrays has always caused some confusion. First let’s understand the UBound and LBound functions:

LBound vs. UBound

From the example above we see that the UBound function alone might not always provide correct results.
Let’s summarize this with an example

Traversing a VBA Array

To traverse (iterate) through a VBA Array you can either use the For or For Each loops.

For Each example

For example

VBA Array Functions

Below a list of various VBA Functions useful when dealing with VBA Arrays (click to expand):

Basic functions

Function Details
LBound (Array, Rank) – return the lowest subscript (index range) of the VBA Array within a certain dimension (Rank). E.g.
UBound (Array, Rank) – return the highest subscript (index range) of the VBA Array within a certain dimension (Rank). E.g.
ReDim Reallocate storage space for the array (set new array size). Applies to dynamic sized arrays only
Erase Release array variable and deallocate the memory used. Applies to dynamic sized arrays only

Batch define VBA Array

VBA does not allow to define array elements in a batch like so:

The only supported manner of defining arrays in VBA is one by one. Awful ain’t it? Wouldn’t it be easier to just provide an enumeration of values instead? Well you can do that. With my DefineArray procedure below. It works only for 1 dimensional arrays but with little effort you can modify it to work for any number of dimensions:

Comparing two arrays (1 Dimensional)

Sorting an array (1 Dimensional) – Quick Sort

Original source here.

VBA Array Limits and Errors

VBA Array Limits

What are the limits of VBA Arrays? For one thing the maximum length of every dimension is defined by an Integer meaning that each dimension can have a maximum size of (2^31)-1 (equal to 2’147’483’647). Although, I would assume you could sooner reach the limit of the actual memory used by the array (about 500MB for 32-bit VBA and about 4GB for 64-bit VBA).

Typical VBA Array errors

Typical errors listed below:

  • Runtime Error 9: Subscript out of range – you are trying to access/set an item of the array outside its range e.g. if you array is of size 10 setting the item of index 11 will give you an out of range error

Other data structures

The VBA Array has its limits and is certainly not as versatile as you may need. In some cases it is better to replace a VBA Array with a VBA Dictionary, ArrayList, Stack, Queue or other data structure objects. Read more here:
The VBA Dictionary and other data structures

F.A.Q.

Below a list of Frequent Asked Questions on VBA Array usage:

I want to to store items in my VBA Array by Key name instead of index number. Is this possible?
No this is not possible using a VBA Array. Use a Dictionary Object.

Should I use the UBound function to get the size of an VBA Array?
No! This in case of some arrays will provide a correct result HOWEVER in some cases will provide incorrect. See this section for more.

My VBA Array will keep expanding. But I don’t want to keep using ReDim to correct the Array size. What to do?
Use the ArrayList object. It allows you to add items without needing to resize it either by upsizing or downsizing your VBA Array.

Can I pass an array as an argument to a Function or Sub?
Sure. See the example below where I pass a VBA Array by it’s reference to modify it within a separate procedure.

VBA Dictionary

VBA Dictionary – Using the VBA Dictionary. Key Value pairs

1 Star2 Stars3 Stars4 Stars5 Stars (13 votes, average: 4.54 out of 5)
Loading...

Visual Basic for Applications (VBA) facilitates many useful data structures – its not just VBA Arrays you can use. COM libraries provide additional data structures such as the VBA Dictionary, ArrayList, Queue, Stack or SortedList.
But before we jump into explaining how these more complex data structures work and how to use them, let’s start with the basics – VBA array. Often you needn’t reach out for more complex structures, so it’s important to understand first the limits of VBA Arrays.

VBA Dictionary example

vba dictionary
The VBA Dictionary is probably the second most often used data structure in VBA. Dictionaries are great at storing key-value pairs of data similarly as the VBA Collection object does.

The VBA Dictionary, however, offers some functionality that is not available with the VBA Collection object e.g. the option to specify a comparison method for Keys, which I will demonstrate below. This allows for a case-sensitive Key.

Ok, so let’s start with a simple example of how the VBA Dictionary can be used.

Traversing items and keys

Below methods for traversing either all keys or items in a VBA Dictionary

Removing items

Similar data structures

  • Hashtable — similar to the Dictionary class. Represents a collection of key/value pairs that are organized based on the hash code of the key. If you want to learn when it’s efficient to use Hashtables read here. More here.
  • SortedList — Similar to the Hashtable. Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index. More here.

Creating your own structures (VBA Class)

If none of the above data structures are suitable for your needs you can always create your own VBA Class. A Class is a VBA Module that can be instantiated as an object (similarly as the objects above), have its own properties etc. Classes are a good way of encapsulating abstraction and complexity.

Alternatively, consider also a stack of any of the mentioned data structures. For example let’s say we want to manage a structure which would store words in different languages. Later we will want to quickly check if there is a word in a certain language in our multi-dictionary. How to do this? A Dictionary of Dictionaries should do the trick. The first Dictionary will have keywords mapping to different languages. The keyword will be e.g. “English” while the value will be an object of type Dictionary in which you can store the word in the “English” language. Cool right?