Is there a way to get the enums in VBA?

前端 未结 8 1989
南旧
南旧 2020-12-06 07:51

Is there a way to get the enums in VBA? Something like this example for C#, but for VBA?

using System;

class EnumsExampleZ
{
    private enum SiteNames
             


        
8条回答
  •  佛祖请我去吃肉
    2020-12-06 08:41

    If the reason you're looking for enum names is because you mean to use them in a user interface, know that even in C# that's bad practice; in .net you could use a [DisplayAttribute] to specify a UI-friendly display string, but even then, that's not localization-friendly.

    In excel-vba you can use Excel itself to remove data from your code, by entering it into a table, that can live in a hidden worksheet that can literally act as a resource file:

    localized captions

    Then you can have a utility function that gets you the caption, given an enum value:

    Public Enum SupportedLanguage
        Lang_EN = 2
        Lang_FR = 3
        Lang_DE = 4
    End Enum
    
    
    Public Function GetFruitTypeName(ByVal value As FruitType, Optional ByVal langId As SupportedLanguage = Lang_EN) As String
        Dim table As ListObject
        Set table = MyHiddenResourceSheet.ListObjects("FruitTypeNames")
        On Error Resume Next
        GetFruitTypeName = Application.WorksheetFunction.Vlookup(value, table.Range, langId, False)
        If Err.Number <> 0 Then GetFruitTypeName = "(unknown)"
        Err.Clear
        On Error GoTo 0
    End Function
    

    Or something like it. That way you keep code with code, and data with data. And you can quite easily extend it, too.

提交回复
热议问题