Arrays in Visual Basic for Applications macros
Part two of a five-part series of blogs

You can use arrays (multiple variables) in VBA to speed up code - and they are also invaluable for processing CSV files and the like.

  1. Arrays
  2. Declaring and Using Static Arrays (this blog)
  3. Dynamic Arrays using REDIM
  4. Multi-dimensional Arrays
  5. Splitting Text into Arrays

This blog is part of our Excel VBA tutorial.  We also run training courses for businesses or individuals in Microsoft Excel and in VBA programming.

Posted by Andy Brown on 24 February 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Declaring and Using Static Arrays

You won't often want to use a static array, because (as the name suggests) they're quite restricted - but on the principle of learning to walk before learning to run ...

Declaring a Static Array

An array is just a collection of variables of the same data type, and so can hold text, numbers or dates:

'array to hold the 7 dwarves

Dim Dwarves(6) As String

'array to hold their ages

Dim DwarfAges(6) As Integer

'array to hold their dates of birth

Dim DwarfDobs(6) As Date

It's probably just about worth mentioning that you can declare an array upper and lower limit.  For example:

Dim Dwarves(3 to 9) As String

I have never used this in my programming career!

Setting Values in an Array

Having declared an array, you can assign values to it in the same way as for a variable:

'array to hold names of pets

Dim PetNames(2) As String

PetNames(0) = "Shadrach"

PetNames(1) = "Meshach"

PetNames(2) = "Abednego"

Referring to the Lower and Upper Bound of an Array

You can use UBound and LBound to refer to the upper and lower limits of an array.  The following code would display this message:

Message displaying array upper/lower bounds

The message displays the upper and lower bounds of the array.

 

Here is the code to demonstrate this:

Option Explicit

Sub ListPeople()

'array to hold names of pets

Dim PetNames(2) As String

'read in the names of your 3 cats

PetNames(0) = "Shadrach"

PetNames(1) = "Meshach"

PetNames(2) = "Abednego"

'display upper and lower bounds

MsgBox "Array PETNAMES goes from " & _

LBound(PetNames) & " up to " & _

UBound(PetNames)

End Sub

Looping Over the Elements in an Array

One of the most common things you will do with an array is to loop over all of the elements in it.  To do this, you should create an integer variable to refer to each subscript in the array:

Sub ListPeople()

'array to hold names of pets

Dim PetNames(2) As String

'read in the names of your 3 cats

PetNames(0) = "Shadrach"

PetNames(1) = "Meshach"

PetNames(2) = "Abednego"

'now list out the pet names

Dim i As Integer

For i = 0 To 2

Debug.Print PetNames(i)

Next i

End Sub

Note that you can loop over the elements using the upper and lower bounds instead:

'now list out the pet names

Dim i As Integer

For i = LBound(PetNames) To UBound(PetNames)

Debug.Print PetNames(i)

Next i

Or, if you prefer, you could start your loop at 1:

'now list out the pet names

Const NumberPets As Integer = 3

Dim i As Integer

For i = 1 To NumberPets

Debug.Print PetNames(i - 1)

Next i

Whichever of the methods above you choose, you should see the contents of the array in your immediate window:

Array contents in immediate window

Each of the 3 loops above will show the same thing: a list of the array elements.

 

Erasing an Array

If you want to ensure that every cell in an array is empty, you should erase it:

Option Explicit

Sub ListPeople()

'array to hold names of pets

Dim PetNames(2) As String

'read in the names of your 3 cats

PetNames(0) = "Shadrach"

PetNames(1) = "Meshach"

PetNames(2) = "Abednego"

'erase the array to start again

Erase PetNames

'now list out the pet names

Dim i As Integer

For i = 0 To 2

Debug.Print PetNames(i)

Next i

End Sub

Sorting Arrays in VBA

You can't!  If you want to sort the pet names shown above into alphabetical order, your best choice is:

  1. Put the contents of the array into worksheet cells.
  2. Sort the column created in VBA.
  3. Read the contents back into a new version of the array.

If you want to know more about sorting, I'd recommend this exhaustive article.

 

Now that you've learnt about static arrays, let's turn our attention to dynamic arrays: ones whose size can vary.

 

This blog has 0 threads Add post