Using Enum

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,099
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I have a list of strings and numbers that go with them.

I'm sending the string to a Function to return the Number and
was using Select Case but have an idea there's something better.
I tried Enum. Can I use the variable name though, or must be a literal ?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I have a list of strings and numbers that go with them.

I'm sending the string to a Function to return the Number and
was using Select Case but have an idea there's something better.
I tried Enum. Can I use the variable name though, or must be a literal ?

It must be a literal as it will be handled before the code is compiled. I am pretty sure VB treats Enum's as a special kind of Const statement (everywhere the literal is found, VB replaces it with the equivalent value, and then proceeds to compile the code).
 
Upvote 0
Not quite with you mean Rick, say I have

Code:
Enum AccessCol
    Composer = 13
End Enum

Sub TheNum()
Dim MyString As String
MyString = "Composer"

Debug.Print AccessCol.Composer  ' this works

13

'But what if I want to use "myString" variable e.g.
Debug.Print AccessCol.MyString
End Sub

I can't do it, can I ?
 
Upvote 0
Not quite with you mean Rick, say I have

Code:
Enum AccessCol
    Composer = 13
End Enum

Sub TheNum()
Dim MyString As String
MyString = "Composer"

Debug.Print AccessCol.Composer  ' this works

13

'But what if I want to use "myString" variable e.g.
Debug.Print AccessCol.MyString
End Sub

I can't do it, can I ?

No, you cannot do that... Composer must be a literal. If you have ever worked with constants (Const statements), Enum is just a way to bundle several Const statements (without the Const keyword) into a named group. Const statements, and hence Enum members, are evaluated before the code is compiled. What that means (for the code you posted) is that VB will look for AccessCol.Composer in your source code and replace it with the number 13, then it will compile the code. This means the keyword Composer will not be found anywhere within the source code at the time the source code is compiled. Variables that you Dim are only "created" (a memory location is assign to them) during the compile process, hence, a variable cannot be used to set up a reference to an Enum member because by the time the variable is created, the Enum has already been handled and is no longer in existences (only the replaced values are in the source code at that time).
 
Upvote 0
Thanks Rick, I understand now, and prob should stick with the Select Case structure. It works fine, but I wanted to try some alternatives.
 
Upvote 0
Using a collection is likely the most efficient, with the number as the item and the name as the key
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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