Microsoft Office Tutorials and References
In Depth Information
User-Defined Types
A user-defined type , or UDT , can be used to define your own data structures. A UDT contains two
or more elements and is designed to meet specific data storage needs of your macro. To define a
UDT, use the Type ... End Type statement:
Type UDTName
Element1 As type
Element2 As type
....
Elementn As type
End Type
Each element can be any of VBA’s data types: Byte , Boolean , Integer , Long , Currency ,
Single , Double , Date , variable-length or fixed-length String , Object , Variant , another
UDT, or an object type. Each element can be a single variable or an array. The rules for naming the
UDT itself and it elements are the same as the rules for naming VBA variables as described earlier
in this chapter.
Once you have defined a UDT, you declare it using the Dim statement. A UDT must be declared in
a module but not within any procedure. Here’s a UDT definition:
Type Person
FirstName As String
LastName As String
EmailAddress As String
End Type
Then declare in a variable of this type:
Dim MyBestFriend As Person
Finally, access the elements of the UDT using the VariableName.ElementName syntax:
MyBestFriend.FirstName = “Alice”
MyBestFriend.LastName = “Wilson”
MyBestFriend.EmailAddress = “alice@somewhere.net”
You can create arrays of UDTs as well:
Dim AllMyFriends(100) As Person
Enumerations
An enumeration is a user-defined data type that consists of a defined set of symbolic constants. You
use the Enum keyword to create an enumeration as shown in this example:
Enum Flavors
Vanilla
Search JabSto ::




Custom Search