Dim MyArray()
but you are a good programmer and/or you have read my Guide to Writing Understandable and Maintainable Code article and so you would do it more like this.
Dim strCustomerArray() As String
You could then create a second array called something like dblSales to hold the sales figures. Then through some process you would fill both arrays and when you wanted to create some sort of report you would loop through both arrays to get the data. That would work, but there's a better way.
Private Type CustomerData ' May be Public in a code module
strCustName As String
dblSales As Double
End Type
Your type can contain any number of built-in types and once created it can be used exactly like any of the built-in types, and so you can do the following in a procedure.
Dim CustData() As CustomerData
To fill the array you could do something like the following where intEntry is a variable you would increment while filling the array. (This article assumes you know how to Redim the array to initialize it and Redim Preserve it to add more entries.)
CustData(intEntry).strCustName = ...
CustData(intEntry).dblSales = ...
Rather than incrementing intEntry you can do it this way
CustData(UBound(CustData)).strCustName = ...
CustData(UBound(CustData)).dblSales = ...
and not have to worry about incrementing a variable. Of course to get the data you would do something like the following.
blah1 = CustData(intEntry).strCustName
blah2 = CustData(intEntry).dblSales
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
CustData(UBound(CustData))
CustData(UBound(CustData))
the code will create a new entry with the first assignment, causing UBound to increment. So the second line will refer to yet another new entry. You need to assign both values with one assignment.
Author
Commented:Open in new window