557 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
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.
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.
|
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 ...
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!
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"
You can use UBound and LBound to refer to the upper and lower limits of an array. The following code would display this message:
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
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:
Each of the 3 loops above will show the same thing: a list of the array elements.
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
You can't! If you want to sort the pet names shown above into alphabetical order, your best choice is:
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.
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.