Tip of the Week #2: Enum

This tip of the week will be about Enums. Enum is short for enumeration, and while you might think you have never heard of or used a enum before, I can almost certainly promise you that it is not true.


The enum you have most likely come across is vbMsgBoxResult. Again, you might think you have never seen it, but take a look below:


As far as I know there is little functionality added to the code itself by using enums, but for the coder it can make a huge difference. If we need the return value from the function, and we are writing:

IntelliseSense displays the possible return values.

IntelliseSense displays the possible return values.

You can see how IntelliseSense knows what the possible return values are, and displays them for us, making it easy for the programmer to select the correct value. Of course you could have just written the value of the constant, but I find this makes coding easier to write, and alot easier to read.


Making your own enum

Its easy enough to make your own enum. As the name indicates, its an enumeration, its numbers, more specifically its longs. This means you can’t code strings into a enum. Lets see an example below:

A few notes of worth: A enum must be placed in the declarations section of a module, that is before your first sub or function. You can reference enum members directly (i.e. you can use vbYes, you don’t have to type vbMsgBoxResult.vbYes). This also means that you should ensure your enum member names are globally unique, to avoid confusion.



Enums can make your coding life significantly easier in that it can help you to remember what the possible values are for input or output from a function.

Tagged with: , , , , , , , ,

Ask a question, leave some feedback, post a reply!

This site uses Akismet to reduce spam. Learn how your comment data is processed.