【发布时间】:2011-08-25 09:29:03
【问题描述】:
我使用以下代码创建了一个名为 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 电子表格中使用上述枚举?所以我可以输入如下公式:
如果我使用数字 1,2,函数可以正常工作,但我想使用枚举名称。
这可以仅使用 Excel VBA 完成吗?
【问题讨论】: