这是自Excel7.0以来我第一次尝试用户定义的函数。所以我很确定我犯了一个新手的错误。
在输入公式的单元格中,我得到了一个#VALUE错误。当我使用错误检查显示计算步骤时,它将函数参数计算为单元格引用而不是值。
单元格中的公式:
=PavementNPV(P2、U2、T2、R2、Y2、Z2、AA2、I2、J2、K2、B2、W2、V2、X2)
错误检查窗口显示公式与所有单元格引用的下划线和X2斜体完全一致。底部是一条消息,即下一次计算将导致错误。错误是#值。
单元格中的值:
守则:
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)输入了这个公式。为了便于阅读,我很想回到那个位置。
如果您需要更多的信息来帮助,请告诉我。提前谢谢你。
发布于 2015-04-19 08:21:35
您的参数Arg2 of WorksheetFunction.VLookup不正确。它们不可能是弦乐。
使用
Application.WorksheetFunction.VLookup(Curve, Application.Range("Table4[#All]"), 2, False)例如。
另一个错误可能是WorksheetFunction.VLookup返回#N/A错误,因为在表数组中找不到查找值。在本例中,函数在此时中断并返回#Value。这可以避免在WorksheetFunction.VLookup和On Error GoTo 0调用之后使用它们。
调试提示:打开VBA编辑器。在函数体的某个位置设置一个断点。通过输入函数作为单元格中的公式来调用该函数。代码在断点处停止。更改到VBA编辑器窗口并逐步遍历代码。
https://stackoverflow.com/questions/29725860
复制相似问题