[英]#VALUE! error on vba function with named range vlookup
我在VBA中具有以下功能:
Function weightedAverage(x0, y0, x1, y1, x)
weightedAverage = y0 + (x - x0) * (y1 - y0) / (x1 - x0)
End Function
Function interpolateLookup(lookupVal As Range, lookupRange As Range, colID As Integer) As Double
step = 0.01
x0 = Application.Round(lookupVal.Value, 3)
y0 = Application.VLookup(x0, lookupRange, colID, True)
x1 = Application.Round(lookupVal.Value, 3) + step
y1 = Application.VLookup(x1, lookupRange, colID, False)
x = lookupVal.Value
interpolateLookup = weightedAverage(x0, y0, x1, y1, x)
End Function
這里的問題是我的interpolateLookup函數返回一個#VALUE! 我通過以下輸入時出錯:
=interpolateLookup(E5,K3:O803,5)
為什么???
真正的問題在我的腦海。 通過利用用於查找的鍵的精度找到了一個解決方案:
Function interpolateLookup(lookupVal As Range, lookupRange As Range, colID As Integer) As Double
step = 0.01
x0 = Application.Round(lookupVal.Value, 2)
y0 = Application.VLookup(x0, lookupRange, colID, False)
x1 = Application.Round(lookupVal.Value, 2) + step
y1 = Application.VLookup(x1, lookupRange, colID, False)
x = lookupVal.Value
interpolateLookup = Application.Round(weightedAverage(x0, y0, x1, y1, x), 1)
End Function
我的lookupVal的精度為3位,我的查閱關鍵字的精度為2位,因此我將lookupVal四舍五入為2位,因為我知道表中將有一個完全匹配的字符串,並且也知道這將是我的下限插值。
感謝您的評論!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.