我在其中一张纸上固有自定义功能。它基本上根据一堆变量对3个表中的1个进行strightline interp。有些原因,每当我打开另一个工作簿时,我的所有输出都会变成#Value错误。如果我重置宏,那么它的工作原理。奇怪的。请帮忙!
Function INTERP(A, CURVENUM)
COL1 = CURVENUM * 3 - 2
COL2 = COL1 + 1
NUMPOINTS = Range("INV").Cells(-1, COL2)
'A = 80
MAXINV = Range("INV").Cells(1, COL1)
MININV = Range("INV").Cells(NUMPOINTS, COL1)
If A >= MAXINV Then
M = (Range("INV").Cells(2, COL2) - Range("INV").Cells(1, COL2)) / (Range("INV").Cells(2, COL1) - Range("INV").Cells(1, COL1))
Q = Range("INV").Cells(1, COL2) - (Range("INV").Cells(1, COL1) - A) * M
End If
If A <= MININV Then
M = (Range("INV").Cells(NUMPOINTS, COL2) - Range("INV").Cells(NUMPOINTS - 1, COL2)) / (Range("INV").Cells(NUMPOINTS, COL1) - Range("INV").Cells(NUMPOINTS - 1, COL1))
Q = (A - Range("INV").Cells(NUMPOINTS, COL1)) * M + Range("INV").Cells(NUMPOINTS, COL2)
End If
For I = 1 To NUMPOINTS - 1
X1 = Range("INV").Cells(I, COL1)
X2 = Range("INV").Cells(I + 1, COL1)
If A <= X1 And A > X2 Then
N2 = Range("INV").Cells(I + 1, COL2)
N1 = Range("INV").Cells(I, COL2)
D2 = Range("INV").Cells(I + 1, COL1)
D1 = Range("INV").Cells(I, COL1)
M = (N2 - N1) / (D2 - D1) 'SLOPE
Q = N2 - (D2 - A) * M
INTERP = Q
Exit Function
End If
Next I
If A = MAXINV Then Q = Range("INV").Cells(NUMPOINTS, COL2)
INTERP = Q
End Function
答案 0 :(得分:1)
此功能有2个问题:
1)它会在任何恰好是活动工作簿(当你打开另一个工作簿时会改变)中寻找Range(“Inv”)。将所有对Range(“Inv”)的引用更改为Application.caller.parent.parent.Names(“Inv”)。ReferstoRange
2)当INV发生变化时,你的函数不会重新计算,除非你使函数变为volatile或将INV作为参数添加到函数中