i'm getting #value error when try call custom function. supposed bit of math. see wrong here?
i copied 1 off internet:
source:
http://www.engineerexcel.com/linear-interpolation-vba-function-in-excel/
function lininterp(x, xvalues, yvalues) 'x , y values must in ascending order top bottom. 'x must within range of available data. x1 = application.worksheetfunction.index(xvalues, application.worksheetfunction.match(x, xvalues, 1)) x2 = application.worksheetfunction.index(xvalues, application.worksheetfunction.match(x, xvalues, 1) + 1) y1 = application.worksheetfunction.index(yvalues, application.worksheetfunction.match(x, xvalues, 1)) y2 = application.worksheetfunction.index(yvalues, application.worksheetfunction.match(x, xvalues, 1) + 1) lininterp = y1 + (y2–y1) * (x–x1) / (x2–x1) end function
this simplified version made thinking worksheet function calls may causing error:
function lininterp(x, x1, x2, y1, y2) lininterp = y1 + (y2–y1) * (x–x1) / (x2–x1) end function
my test data in unrelated workbook: (all formatted "general")
a1: 633 a2: 634 b1: 14.968 b2: 15.024 c1 (my x): 633.6
just plugging actual math cell works expected. calling function throws #value error.
my function saved in module in workbook have saved , added excel add-in.
my sampling of formula , data threw error on hyphens not being interpreted 'minus signs'. in fact, come unicode 8211. retyping them, declaring vars variants , removing ...worksheetfunction...
fixed problem.
function lininterp(x, xvalues, yvalues) dim x1 variant, x2 variant, y1 variant, y2 variant 'x , y values must in ascending order top bottom. 'x must within range of available data. x1 = application.index(xvalues, application.match(x, xvalues, 1)) x2 = application.index(xvalues, application.match(x, xvalues, 1) + 1) y1 = application.index(yvalues, application.match(x, xvalues, 1)) y2 = application.index(yvalues, application.match(x, xvalues, 1) + 1) lininterp = y1 + (y2 - y1) * (x - x1) / (x2 - x1) end function
moral of story: don't trust find on internet.
Comments
Post a Comment