2

I have a user Form that has a combo box in it Comb_colorPick_Change, each item in the combo box represents a color as in this picture

enter image description here

I defined an enum which stores some word color constants (wdRed, wdGreen, …etc), I wanted to change the back color of the combo box using a bitwise operation (AND) between the Sum of all enum constants (all) and the combobox item index Comb_colorPick.BackColor = (Comb_colorPick.ListIndex + 1) And All, this operation results a number (ex: 3) which corresponds to a color (ex: wdRed) BUT I need the result number to be (wdRed) instead of (3). i used select case before to change the back color but i'm looking for another method

My main question is:

How do I define a two way enum where I can get a number by providing a constant name and vice versa.

my other questions

  • Is it possible to loop through a public enum, ex: WdColor enum
  • How to use “bitwise enumerations”, a practical example would be appreciated

Here is my code:

Private Enum MyColors
    Red = wdRed
    Green = wdGreen
    Blue = wdBlue
    Yellow = wdYellow
    Turquoise = wdTurquoise
    BrightGreen = wdBrightGreen
    Pink = wdPink
    Violet = wdViolet
    All = wdRed Or wdGreen Or wdBlue Or wdYellow Or wdTurquoise Or wdBrightGreen Or wdPink Or wdViolet
End Enum

Private Sub Comb_colorPick_Change()

    Dim co As MyColors
    co = All
    'Comb_colorPick.BackColor = (Comb_colorPick.ListIndex + 1) And co    '<=== i need this to be a constant name
    Label1.Caption = "Bitwise Result = " & ((Comb_colorPick.ListIndex + 1) And co)

End Sub

Private Sub UserForm_Activate()
' fill the combo during user form activation event
    With Comb_colorPick
        .AddItem "Red", 0
        .AddItem "Green", 1
        .AddItem "Blue", 2
        .AddItem "Yellow", 3
        .AddItem "Turquoise", 4
        .AddItem "BrightGreen", 5
        .AddItem "Pink", 6
        .AddItem "Violet", 7
        .Value = "BrightGreen"
    End With
End Sub
4
  • 1
    There is no built-in "two-way" enum in VBA. You would define a function which when passed a string would return the enum value, and another which did the opposite. Also: cpearson.com/excel/Enums.aspx - see "composite values" section Commented Mar 15, 2016 at 16:11
  • what about this [youtube.com/watch?v=ukFxP35k0m4 ] , and for the link you sent , this is called bitmasks as i believe i know how to use it but i don't think I'm using it as i should, that's why i asked
    – Ali_R4v3n
    Commented Mar 15, 2016 at 16:35
  • 2
    Flag enums work better when their values are powers of 2. Like, 1, 2, 4, 8, 16, 32, 64, etc. ...otherwise you can't bitwise-combine them and have a unique combination; 4+2 is 6, 5+1 is also 6. Commented Mar 15, 2016 at 17:02
  • @Mat's Mug, yes i know that but my main question is clear..
    – Ali_R4v3n
    Commented Mar 15, 2016 at 18:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.