562 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 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.
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.
|
One of the most useful things that you can do with arrays is to split text strings into distinct words or phrases, using SPLIT:
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:
The array generated goes from 0 to 5, and has 6 elements.
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:
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
Loop
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?
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!
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.