Excel UDF参数返回单元格引用而不是值

这是自Excel 7.0以来我第一次进入用户定义的函数。 所以我很确定我正在做一个新手的错误。

我在input公式的单元格中收到#VALUE错误。 当我使用错误检查来显示计算步骤时,它将函数参数作为单元格引用而不是值来计算。

单元格中的公式:

=路面NPV(P2,U2,T2,R2,Y2,Z2,AA2,I2,J2,K2,B2,W2,V2,X2)

错误检查窗口说明公式完全与所有单元格引用下划线和X2斜体。 底部是下一个评估将导致错误的消息。 错误是#VALUE。

单元格中的值:

  • P2 = 10
  • U2 = 63.17986
  • T2 = 10
  • R2 = 3
  • Y2 = $ 0.28
  • Z2 = $ 1.32
  • AA2 = $ 2.58
  • I2 = 14000
  • J2 = 45
  • K2 = 60
  • B2 = 292
  • W2 = 73.17986
  • V2 = 8
  • X2 = 0.05

代码:

 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(eg 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 

我原来已经定义了所有的数据types,但是把它们作为我的第一个故障排除步骤。 我原本也有使用表引用input的公式(例如[@columnname])。 我希望能够回到可读性。

请让我知道,如果你需要任何额外的信息来帮助。 先谢谢你。

WorksheetFunction.VLookup参数Arg2不正确。 他们不能是string。

使用

 Application.WorksheetFunction.VLookup(Curve, Application.Range("Table4[#All]"), 2, False) 

例如。

另一个错误可能是WorksheetFunction.VLookup返回#N/A错误,因为查找值在表数组中找不到。 在这种情况下,函数在此处中断并返回#Value。 这可以避免在调用WorksheetFunction.VLookupOn Error GoTo 0之前使用On Error Resume Next

提示debugging:打开VBA编辑器。 在函数体内的某处设置一个断点。 通过在单元格中input公式,调用函数。 代码停在断点处。 切换到VBA编辑器窗口并单步执行代码。