Retrieve Enum Variable Names using Enum Values

pmich

Active Member
Joined
Jun 25, 2013
Messages
294
I would like to use the values declared in the Enum section and get the names of those variables.
Code:
Private Enum Bks
 [_First] = 0
 English = 0
 Maths = 1
 Science = 2
 [_Last] = 2
End Enum


Dim MyBooks(2) As Boolean


Private Sub cmdEnumTrial_Click()
 Dim ChosenBooks As String
 Dim Runr As Integer
 Dim UserBooks() As String
 ChosenBooks = "English, Maths, Science"
 UserBooks = Split(ChosenBooks, ",")
  
  'User chooses the Option Button of MATHS.
  'This value is received using the following IF Statement.
  'The following statements are not disabled here, but are used in the main programme.
  'If OptBtnEng.Value = True Then
   'MyBooks(Bks.English) = True
  'ElseIf OptBtnMath.Value = True Then
   
   MyBooks(Bks.Maths) = True
  
  'ElseIf OptBtnSc.Value = True Then
   'MyBooks(Bks.Science) = True
  'End If


 For Runr = LBound(UserBooks) To UBound(UserBooks)
  If MyBooks(Runr) = False Then 'checking which Book has been chosen
   For ECtr = Bks.[_First] To Bks.[_Last]
    If Runr = ECtr Then
     'msgbox "You have not chosen :" & MyBooks(Runr) 'This code failed
     'msgbox "You have not chosen :" & Bks.ECtr 'This code failed
     'msgbox "You have not chosen :" & Bks & ".ECtr" 'This code failed
     
     'Result of the following is : "Not Chosen : 0" or  "Not Chosen : 2"
     msgbox "You have not chosen :" & Bks.[_First] + Runr
     'The result should be : "Not chosen English"
     
     Exit For
    End If 'Runr = ECtr
   Next ECtr 'ECtr = Bks.[_First] To Bks.[_Last]
  End If 'MyBooks(Runr) = false
 Next Runr 'Runr = LBound(UserBooks) To UBound(UserBooks)
End Sub
The result I am getting is : "Not Chosen : 0".
But I want the result to be : "Not chosen English" and in the next loop "Not chosen Science".
Kindly suggest. Thanks.
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Enums don't work like that I'm afraid, but you already have an array with the names in so you could just use that.
 
Upvote 0
Thanks, Mr. RoryA
You told me about Enum in another reply of yours. Thanks once again.
I went through a suggestion given by an expert in some other forum.
I have not used ENums so far.
Since you say Enums will not work in this way, for the time being I have to use this method, which I have given below.
Code:
If Runr = ECtr Then
     msgbox "You have not chosen :" & BkNam(Runr)    
     Exit For
    End If 'Runr = ECtr
Code:
Public Property Get BkNam(BN) As String
  Dim BkTitle As Variant
  BkTitle = Array("English", "Maths", "Science")
  BkNam = VBA.CStr(BkTitle(BN))
End Property
Anyway, Thanks for your immediate response.
I am not sure how to use the array in this code to find out the name of the Book.
 
Last edited:
Upvote 0
I got your point, Mr. RoryA
Yes, the following code does the job.
Code:
msgbox "You have not chosen :" & UserBooks(Runr)
Thanks for your valuable suggestion, which has eliminated the work around using Property.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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