Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Enums 如何在Excel UDF中使用枚举标识符_Enums_Excel_Vba - Fatal编程技术网

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

Enums 如何在Excel UDF中使用枚举标识符,enums,excel,vba,Enums,Excel,Vba,我创建了一个名为EnergyPrice的函数,其代码如下: 这是价格表,我在上面看 Prijstabel fixed variable Startdate Einddate gas electra gas electra €/a €/a ct/KWh ct/KWh

我创建了一个名为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
公共枚举类型
v_气体=1
v_电=2
结束枚举
公共枚举固定变量
v_固定=1
v_变量=2
结束枚举
公共函数EnergyPrice(PriceDate作为日期,E_类型作为Energietype,variabel作为FixedOrVariable)作为变量
Dim PrijsTable As范围
作为整数的Dim RowNr
Dim被发现为布尔值
Dim KolomNr作为整数
Set PrijsTable=范围(“能量定价”)
如果PrijsTable.Columns.Count为6,则错误提升编号:=vbObjectError+1000,说明:=“未定义有效的价格表”
RowNr=1
发现=错误

虽然没有(找到)和(RowNr,但我看到的唯一方法是在工作簿中定义名称,为它们分配枚举的常量

  • 从菜单中:
    Insert…Name…Define
  • 名称:
    v_gas
  • 指:
    =1
或者,您也可以使用VBA创建这些名称,但这不感兴趣,因为这将是一个1快照(名称与工作簿一起保存)


通过使用这些名称,用户可以在输入公式时使用F3。

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

例如:

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

啊哈,我们得出了相同的结论。+1:)谢谢(我想)这正是我试图避免的事情,但我想我可以在一个
OnOpen
事件中在VBA中创建这些名称,并在de
OnClose
事件中销毁它们。我希望能够在VBA代码中控制我的名字。是的,在这种情况下,VBA和Excel就像两个人住在同一个公寓但在不同的房间。他们必须互相敲门才能把事情做完。