Arrays in Visual Basic for Applications macros
Part five 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
  3. Dynamic Arrays using REDIM
  4. Multi-dimensional Arrays
  5. Splitting Text into Arrays (this blog)

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.

Splitting Text into Arrays

One of the most useful things that you can do with arrays is to split text strings into distinct words or phrases, using SPLIT:

ArrayName = Split(Text string, character to use as delimiter)

Example: Splitting Text into Words

As an example, suppose that you want to count the number of words in a phrase like the following:

I wandered lonely as a cloud

By far the easiest way to do this is to split the string into distinct words, and then count how many there are:

Option Explicit

Const txt As String = "I wandered lonely as a cloud"

Sub Example()

'create an array to hold all of the words

Dim Words() As String

'split string into words, using space as delimiter

Words = Split(txt, " ")

MsgBox "String contains " & (UBound(Words) + 1) & " words"

End Sub

This would produce the following output:

Count of words

The array generated goes from 0 to 5, and has 6 elements.


Parsing CSV Files

Many applications generate output as CSV files (CSV stands for Comma Separated Values).  You can parse these using SPLIT.

You'll need to know how to read from TextStream objects in order to understand this example.

Suppose that you have exported our list of people (now expanded to include a hair colour column) to a CSV file.  This is what it looks like in Excel:

List of people with hair colour

In Excel we now have 3 columns.


The CSV file generated looks like this:

CSV file containing 3 columns

The CSV file contains a list of the people, with column values separated by commas.


If you wanted to read these values in to extract the name, age and hair colour for each person, by far the easiest way to do it would be to split each line up, using the commas as separation characters.  Here is code to do this:

Sub ReadLines()

'see other blog for more on this

Dim fso As New FileSystemObject

'array of values in each line

Dim LineValues() As String

'each new line read in from the text stream

Dim ReadLine As String

Dim ts As TextStream

'open file

Set ts = fso.OpenTextFile("c:\Wise Owl\people.csv")

'keep going till no more lines

Do Until ts.AtEndOfStream

'read first line

ReadLine = ts.ReadLine

'split using commas

LineValues = Split(ReadLine, ",")

'write values into this row

ActiveCell.Value = LineValues(0)

ActiveCell.Offset(0, 1).Value = LineValues(1)

ActiveCell.Offset(0, 2).Value = LineValues(2)

'go on to next row

ActiveCell.Offset(1, 0).Select


End Sub

This routine would read in the CSV file line by line, splitting each line into an array by using the comma as a separator.  The result?

Recreated Excel worksheet

Assuming that you have A1 as the active cell when you run this macro, this is what you will get.


And that completes my thoughts on using arrays in VBA!



This blog has 0 threads Add post