How can I use an Enum for Validation in VBA

M.Kadyrov

I'm trying to add validation to a Cell but I would like to use an Enum as Source.

 Public Enum account

      AA
      BB
      PP
      ZZ

  End Enum

  Sub Main()

    With Range("C9").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
             Operator:=xlBetween, Formula1:= ...

  End With
End Sub     

Normally I would put Join(account, ",") if account would be an array, but for an Enum it doesn't work. And if direct usage of Enum is not possible how can I add enum values to an array?

Jean-François Corbett

It's not as straightforward as you would wish, but there is a way to do this.

The underlying value of an Enum is of type Long. So it's a number, not a string. Only the VBA compiler knows about AA; this gets translated into 0 everywhere else (and BB is 1, PP is 2, etc.). So the first thing to do is to explicitly associate a string to each enum option. I've done this using functions like this, which are easy to make but do require a little bit of manual maintenance each time you add, remove, or modify an enum option:

Function AccountEnumString(a As account) As String
    Select Case a
        Case AA: AccountEnumString = "AA"
        Case BB: AccountEnumString = "BB"
        Case PP: AccountEnumString = "PP"
        Case ZZ: AccountEnumString = "ZZ"
        Case Else: Err.Raise 9999, , "Unexpected enum value."
    End Select
End Function

Then you want to loop through all enum options, making a list of their associated string. You could in principle say

Dim a As account
For a = AA To ZZ
'...

But that will be very difficult to maintain: what if you add another enum option such that ZZ isn't the last anymore? Then you'll have to change your code. A better option is to put a little more work in the enum declaration itself:

Public Enum account
    [_First] = 1
    AA = 1
    BB = 2
    PP = 3
    ZZ = 4
    [_Last] = 4
End Enum

Here you explicitly assign values to each option (rather than letting the compiler assign defaults) and you also add a First and a Last indicating the bounds of the enum. These require manual maintenance, but this is trivial in comparison to the trouble of maintaining the rest of the code as would be required without this trick.

Note on syntax: the _ underscore makes the _First and _Last elements hidden (they will not be displayed in the VB Editor's autocompletion) and the [ square brackets ] make the underscore character valid for use as the first character of a variable name. (source of this trick)

Now you can loop through your enum options, collect the associated strings in an array, join it with , delimiters, and use that as validation formula:

Dim a As account
Dim arrValidationList() As String

ReDim arrValidationList(account.[_First] To account.[_Last])
For a = account.[_First] To account.[_Last]
    arrValidationList(a) = AccountEnumString(a)
Next

With Range("C9").Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
         Operator:=xlBetween, Formula1:=Join(arrValidationList, ",")
End With

Note that each time you add or remove enum options, the following things will require manual maintenance:

  • The account enum definition, making sure that the numbering is sequential and that the First and Last elements actually represent the bounds, and
  • The AccountEnumString function, making sure the strings are representative of the enum options to your satisfaction

Also see these somewhat related questions:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I use an Enum for Validation in VBA

From Dev

How can I use the fromRaw() function of enum

From Dev

How can I use cross validation in python?

From Dev

How can I add extra option to the dynamic validation list (VBA)?

From Dev

How can I convert this IF statement to use in VBA

From Dev

Can I use an enum within an enum?

From Dev

How can I use my enum in QString.arg()?

From Dev

How can I use a Swift enum as a Dictionary key? (Conforming to Equatable)

From Dev

How can I use my enum in QString.arg()?

From Dev

How can i use enum to pass variables between two scripts?

From Dev

How can I do validation and use ..$setPristine(); in an AngularJS form?

From Dev

How can I use variable for jquery validation equalTo method?

From Dev

how can I use database data in form validation?

From Dev

How can I use a custom validation with shoulda matchers?

From Dev

How can I use variable for jquery validation equalTo method?

From Dev

How can I use validation with form without page refresh?

From Dev

how can I use database data in form validation?

From Dev

How can I use glyphicons in input for validation with HTML?

From Dev

How can I use multiple validation rules as base for multiple validation requests in Laravel

From Dev

Can I use an enum as a struct name?

From Dev

Can I use an enum in a constructor for a class?

From Dev

Can I use an enum as a struct name?

From Dev

Can I pass and use enum as a variable?

From Dev

How can I use the WEEKNUM function in a statement in VBA?

From Dev

How can I use the OFFSET worksheet function in VBA?

From Dev

How can I use a Cell Value as a Named Range in VBA?

From Dev

How can I use my defined name of range in VBA Excel?

From Dev

How can I use custom colours in the Microsoft VBA editor?

From Dev

How can I use the WEEKNUM function in a statement in VBA?

Related Related

  1. 1

    How can I use an Enum for Validation in VBA

  2. 2

    How can I use the fromRaw() function of enum

  3. 3

    How can I use cross validation in python?

  4. 4

    How can I add extra option to the dynamic validation list (VBA)?

  5. 5

    How can I convert this IF statement to use in VBA

  6. 6

    Can I use an enum within an enum?

  7. 7

    How can I use my enum in QString.arg()?

  8. 8

    How can I use a Swift enum as a Dictionary key? (Conforming to Equatable)

  9. 9

    How can I use my enum in QString.arg()?

  10. 10

    How can i use enum to pass variables between two scripts?

  11. 11

    How can I do validation and use ..$setPristine(); in an AngularJS form?

  12. 12

    How can I use variable for jquery validation equalTo method?

  13. 13

    how can I use database data in form validation?

  14. 14

    How can I use a custom validation with shoulda matchers?

  15. 15

    How can I use variable for jquery validation equalTo method?

  16. 16

    How can I use validation with form without page refresh?

  17. 17

    how can I use database data in form validation?

  18. 18

    How can I use glyphicons in input for validation with HTML?

  19. 19

    How can I use multiple validation rules as base for multiple validation requests in Laravel

  20. 20

    Can I use an enum as a struct name?

  21. 21

    Can I use an enum in a constructor for a class?

  22. 22

    Can I use an enum as a struct name?

  23. 23

    Can I pass and use enum as a variable?

  24. 24

    How can I use the WEEKNUM function in a statement in VBA?

  25. 25

    How can I use the OFFSET worksheet function in VBA?

  26. 26

    How can I use a Cell Value as a Named Range in VBA?

  27. 27

    How can I use my defined name of range in VBA Excel?

  28. 28

    How can I use custom colours in the Microsoft VBA editor?

  29. 29

    How can I use the WEEKNUM function in a statement in VBA?

HotTag

Archive