Keywords in an Enumerated Data Type

jerH

Board Regular
Joined
Dec 3, 2008
Messages
168
Hopefully this is a simple answer...

I'd like to create an enumerated data type for the two-character abbreviations of the 50 US states. So in theory

Code:
Enum StateAbbrev
    AL
    AK
    AZ
    AR
    CA
    CO
    CT
    DE
    FL
    GA
    HI
    ID
    IL
    IN
    IA
    KS
    KY
    LA
    ME
    MD
    MA
    MI
    MN
    MS
    MO
    MT
    NE
    NV
    NH
    NJ
    NM
    NY
    NC
    ND
    OH
    OK
    OR
    PA
    RI
    SC
    SD
    TN
    TX
    UT
    VT
    VA
    WA
    WV
    WI
    WY
End Enum

But of course, IN, ME, and OR are all VBA keywords....so how can I accomplish this?

Thanks!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
please clarify, do you want to call AL 01 and WY 50 and all the numbers in between?
 
Upvote 0
That would be fine....my understanding was that if the associated longs were omitted then the first would be 0 and the rest would increment by 1 so you'd have 0 to 49...I'm not worried about that aspect, but as it is it sees IN as a keyword and won't even run....
 
Upvote 0
Hmm, not certain but maybe you could change IN to IN_ and ME to ME_ then whatever code you are calling this from, selected on first 2 characters???
 
Upvote 0
How are you going to use the enumeration?
 
Upvote 0
Interestingly, I found that if I place the offending values in square brackets, then it works...sorta. So IN and ME are now [IN] and [ME] but Intellisense shows them as IN and ME. But when I put OR in brackets it becomes [Or] (note the capitalization) and Intellisense shows it as Or. Weird....there should be some way to escape the characters don't you think?
 
Upvote 0
How are you going to use the enumeration?
I have that same question. Enumeration names cannot be referred to using text string values... they are more a shorthand for the programmer to use in code so he/she does not have to keep remembering the number itself.
 
Last edited:
Upvote 0
Hi jerH

Like you saw, you can escape keywords when defining the ID's in the enum by enclosing them in square brackets, like

Code:
Enum StateAbbrev
    AL
    [in]
    [me]
    [Or]
    pa
End Enum

You just have to remember that this does not disable the keywords, they are still vba keywords.

This means that with that definition of the enum you still can't use for ex.:

Code:
If i(4) = [B][COLOR=#800000]Or[/COLOR][/B] Then MsgBox "OK"

you must qualify it so that it's interpreted in the enum context, for ex.:

Code:
Option Explicit

Enum StateAbbrev
    AL
    [in]
    [Me]
    [Or]
    pa
End Enum


Sub Test()
Dim i(1 To 5) As Long

i(1) = AL
i(2) = StateAbbrev.in
i(3) = StateAbbrev.Me
i(4) = StateAbbrev.Or
i(5) = pa

If i(4) = StateAbbrev.Or Then MsgBox "OK"

End Sub
 
Upvote 0
This is just an example I'm putting together to show students how to use enumerated data types and user defined types...personally while I define my own types I never bother with enumerations, so I'm on slightly new ground here...

I generated a table of transactions that looks like
IDLastFirstAddressCityStateZipEventTicket 1Ticket 2Ticket 3Total Purchase
282CurielVeronica148 South GlenwoodFredericksburgVA22405Concert036$375
480HameedFernando20 Lawrence StNew CityNY10956Sports257$850

<tbody>
</tbody>

etc.

I created the enum for the state abbreviations, then a type for customers that looks like

Code:
Type Customer
    CustomerID As Integer
    LastName As String
    FirstName As String
    Address As String
    City As String
    State As StateAbbrev
    zip As String
End Type
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,835
Members
449,471
Latest member
lachbee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top