这是我自Excel 7.0以来首次涉足用户定义的函数。所以我很确定我犯了新手错误。
我在我输入公式的单元格中收到#VALUE
错误。当我使用错误检查显示计算步骤时,它将函数参数计算为单元格引用而不是值。
单元格中的公式:
= PavementNPV(P2,U2,T2,R2,Y2,Z2,AA2,I2,J2,K2,B2,W2,V2,X2)
错误检查窗口说明了公式,所有单元格引用都是下划线,X2是斜体。在底部是下一个评估将导致错误的消息。错误是#VALUE。
单元格中的值:
代码:
Function PavementNPV(AssumedLife, PvmtCondition, AgeByCondition, Curve, CarVOC As Currency, BusVOC As Currency, TruckVOC As Currency, AvgDailyTraffic, TruckCount, BusCount, SegLength, RestPCTGain, RestAgeByCondition, Rate) As Currency
'Calculate Yr1 restored PCI(PvmtCondition - Pavement Condition Index) value
' =PvmtCondition + Assumed Life
Dim Yr1RestPCI
Yr1RestPCI = PvmtCondition + AssumedLife
'For each year of Assumed life, calculate doNothingTotalAnnualVOCIncreaseRange and restoredTotalAnnualVOCIncreaseRange:
' =365*(CarVOCIncrease% as (Application.WorksheetFunction.VLOOKUP((PCIofYr as (If Yr1 then PvmtCondition, else 100*(1/(1+EXP((AgeByCondition+year(e.g. 2)-1)/Application.WorksheetFunction.VLOOKUP(Curve,Table4[#All],2,FALSE)-Application.WorksheetFunction.VLOOKUP(Curve,Table4[#All],3,FALSE))))*Application.WorksheetFunction.VLOOKUP(Curve,Table4[#All],4,FALSE)))),'User Cost'!$BC$5:$BF$151,2))*CarVOC*AvgDailyTraffic+BusVOCIncrease%*BusVOC+TruckVOCIncrease%*TruckVOC)*SegLength/5280)
Dim arydoNothingPCI()
ReDim arydoNothingPCI(1 To AssumedLife)
Dim aryRestoredPCI()
ReDim aryRestoredPCI(1 To AssumedLife)
Dim arydoNothingVOCIncrease() As Currency
ReDim arydoNothingVOCIncrease(1 To AssumedLife)
Dim aryRestoredVOCIncrease() As Currency
ReDim aryRestoredVOCIncrease(1 To AssumedLife)
Dim i
arydoNothingPCI(1) = PvmtCondition
aryRestoredPCI(1) = Yr1RestPCI
For i = 2 To AssumedLife
arydoNothingPCI(i) = 100 * (1 / (1 + Application.WorksheetFunction.Exp((AgeByCondition + i) - 1) / Application.WorksheetFunction.VLookup(Curve, "Table4[#All]", 2, False) - Application.WorksheetFunction.VLookup(Curve, "Table4[#All]", 3, False))) * Application.WorksheetFunction.VLookup(Curve, "Table4[#All]", 4, False)
aryRestoredPCI(i) = 100 * (1 / (1 + Application.WorksheetFunction.Exp((RestAgeByCondition + i) - 1) / Application.WorksheetFunction.VLookup(Curve, "Table4[#All]", 2, False) - Application.WorksheetFunction.VLookup(Curve, "Table4[#All]", 3, False))) * Application.WorksheetFunction.VLookup(Curve, "Table4[#All]", 4, False)
Next
'Testing function so far by asking it to return something simple
PavementNPV = CarVOC
'Calculate Total PV Benefits by calculating NPV of doNothing minus NPV or restored
' =Application.WorksheetFunction.NPV(rate,doNothingTotalAnnualVOCIncreaseRange)- Application.WorksheetFunction.NPV(rate,restoredTotalAnnualVOCIncreaseRange)
' or for each NPV =(Yr1VOCIncrease/(1+rate)^1)+(Yr2VOCIncrease/(1+rate)^2)+(Yr3VOCIncrease/(1+rate)^3) etc for all years
End Function
我最初定义了所有数据类型,但删除了这些作为我的第一个故障排除步骤。我最初也使用表格引用输入了公式(例如[@columnname])。我希望能够回归到那里以便于阅读。
如果您需要任何其他信息,请与我们联系。提前谢谢。
答案 0 :(得分:3)
Arg2
的参数WorksheetFunction.VLookup
不正确。它们不能是字符串。
使用
Application.WorksheetFunction.VLookup(Curve, Application.Range("Table4[#All]"), 2, False)
例如。
另一个错误可能是WorksheetFunction.VLookup
返回#N/A
错误,因为在表数组中找不到查找值。在这种情况下,函数在此时断开并返回#Value。这样您就可以避免在On Error Resume Next
和WorksheetFunction.VLookup
之后使用On Error GoTo 0
。
提示调试:打开VBA编辑器。在函数体内的某处设置断点。通过在单元格中将其作为公式输入来调用该函数。代码在断点处停止。切换到VBA编辑器窗口并逐步执行代码。