BLOGS BY TOPIC
BLOGS BY AUTHOR
BLOGS BY YEAR
You can use arrays (multiple variables) in VBA to speed up code - and they are also invaluable for processing CSV files and the like.
- Declaring and Using Static Arrays
- Dynamic Arrays using REDIM
- Multi-dimensional Arrays
- Splitting Text into Arrays (this blog)
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:
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:
Const txt As String = "I wandered lonely as a cloud"
'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"
This would produce the following output:
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:
In Excel we now have 3 columns.
The CSV file generated looks like this:
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:
'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
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
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?
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!