Sometimes you must deal with tables of information where each row represents a different sample and each column gives
the value of a particular attribute of the sample.
In general, each attribute may have different data type.
A classic example would be the elements in a periodic table arranged alphabetically according to the name of each element.
The atomic properties for each element are listed in a row.
The first column gives the name of the element, the second column gives its atomic symbol,
the third column gives its atomic number, the fourth column gives its atomic weight, etc.
One way to handle such data would be to use an array to store each column of information:
Dim ElementName(118) As String Dim Symbol(118) As String * 3 Dim AtomicNumber(118) As Integer Dim AtomicWeight(118) As DoubleThis would work but would be rather clumsy.
Type userType
variableName1 As type1
variableName2 As type2
...
End Type
and it is typically placed in the Public area before any Sub or Function definitions so that every module can use this type.Dim recordName(n) As userTypeIndividual samples of the record are identified by appending a period and the name of the variable as in
recordName(i).variableName2The following example shows how information from the above table of atomic properties could be stored in a record:
Option Explicit
Option Base 1
Type AtomicData
elementName As String
symbolName As String * 3
atomicNumber As Integer
atomicWeight As Double
End Type
Sub atomicInformation()
Dim info(118) As AtomicData
Dim msg As String
info(1).elementName = "Actinium"
info(1).symbolName = "Ac"
info(1).atomicNumber = 89
info(1).atomicWeight = 227.0278
...
msg = "Atomic numer =" & info(1).atomicNumber
MsgBox Msg, , info(1).elementName
End Sub
When this Sub is run, a message "Atomic number = 89" will be displayed in a message box
with the title "Actinium".Call Display(info())However, the argument of the Sub statement must include the Type specification to inform the procedure that info() is a record:
Sub Display(info() As AtomicData)