Page 85 - Excel Progamming Weekend Crash Course
P. 85

d540629 ch04.qxd  9/2/03  9:28 AM  Page 60




                60                                                            Friday Evening

               Enumerations

               An enumeration is a defined data type that contains a set of symbolic constants as specified
               by the programmer. When a variable is declared as an enumeration type, it can take only
               these predefined values. Enumerations are useful when a variable’s data can take only cer-
               tain values, such as a variable to hold the name of the month. By using an enumeration
               that contains the 12 month names, you ensure that a variable of that type never can hold
               erroneous data.
                  You use the Enum...End Enum statement to define an enumeration:
                  Enum EnumName
                       Member1
                       Member2
                       ...
                  End Enum

                  Here’s an example:
                  Enum Flavor
                       Vanilla
                       Chocolate
                       Strawberry
                       Coffee
                  End Enum
                  Each constant in an Enum statement is assigned a numeric value. If you do not specify
               the values, they are assigned in order starting with 0. In the previous example, Vanilla has
               the value 0, Chocolate has the value 1, and so on. If desired, you can specify the values
               explicitly:

                  Enum Flavors
                       Vanilla = 1
                       Chocolate = 4
                       Strawberry = 7
                       Coffee = 16
                  End Enum

                  An Enum statement must be defined at the module level, outside of any procedure
               (similar to a UDT). It then becomes available as a data type that you can use in your
               variable declarations:
                  Dim MyIceCream As Flavor
                  Dim MenuItems(50) As Flavor

                  When working with an Enum variable, VBA’s Auto List feature displays a list of the Enum
               statement’s defined constants, which is a real convenience because you can simply select
               from the list rather than type the data in manually.
   80   81   82   83   84   85   86   87   88   89   90