如何在Excel UDF中使用枚举标识符

我用下面的代码创build了一个名为EnergyPrice的函数:

这是我正在寻找的可贵的东西

Prijstabel fixed variable Startdate Einddate gas electra gas electra €/a €/a ct/KWh ct/KWh 1-1-2010 1-7-2010 181,00 235,00 0,11 0,33 1-7-2010 1-1-2011 362,00 470,00 0,33 1,30 1-1-2011 1-7-2011 191,00 245,00 0,22 0,65 1-7-2011 1-1-2012 162,35 208,25 0,19 0,55 1-1-2012 1-7-2012 324,70 416,50 0,37 1,11 

这里是相关的代码

 Public Enum Energietype v_gas = 1 v_electricity = 2 End Enum Public Enum FixedOrVariable v_fixed = 1 v_variable = 2 End Enum Public Function EnergyPrice(PriceDate As Date, E_type As Energietype, variabel As FixedOrVariable) As Variant Dim PrijsTable As Range Dim RowNr As Integer Dim Found As Boolean Dim KolomNr As Integer Set PrijsTable = Range("EnergyPriceTable") If PrijsTable.Columns.Count <> 6 Then Err.Raise Number:=vbObjectError + 1000, Description:="No valid valid pricetable defined" RowNr = 1 Found = False While Not (Found) And (RowNr <= PriceTable.Rows.Count) Found = (PriceTable.Cells(RowNr, 1).Value <= PriceDate) And (PriceTable.Cells(RowNr, 2) > PriceDate) If Not (Found) Then RowNr = RowNr + 1 Wend If Found Then If E_type = v_gas Then KolomNr = 1 If E_type = v_elektra Then KolomNr = 2 If variabel = v_variabel Then KolomNr = KolomNr * 2 KolomNr = KolomNr + 2 EnergyPrice = PriceTable.Cells(RowNr, KolomNr).Value Else EnergyPrice = Empty End If End Function 

问题是如何在Excel电子表格中使用上面的枚举? 所以我可以input一个公式:

在这里输入图像说明

如果我使用数字1,2function正常工作,但我想使用枚举名称。
这只能使用Excel VBA来完成吗?

如果将枚举添加到工作簿中作为定义的名称,则可以将它们传递给函数,并且传递的值是您设置枚举的实际值。 如果您愿意,可以手动或通过VBA进行操作。

例:

 ActiveWorkbook.Names.Add Name:="v_gas", RefersToR1C1:="=1" ActiveWorkbook.Names.Add Name:="v_fixed", RefersToR1C1:="=2" 

我看到的唯一方法是通过在工作簿中定义名称,将它们分配给您的枚举的常量。

  • 从菜单中: Insert ... Name .... Define
  • 名称: v_gas
  • 指: =1

或者,您可以通过VBA创build这些名称,但是没有兴趣,因为它将是一个镜头(名称与工作簿一起保存)。

通过使用这样的名字,用户可以在input公式时使用F3。