我想对列中的特定数字进行非线性插值,并在第二列中获取等效数字。它给我一个编译器错误列表分隔符或)
Sub interp()
Dim temp As Integer
Dim var As Integer
Dim var1 As Integer
Dim xs As Range
Dim ys As Range
Set xs = Sheets(1).Range("A8:A19")
Set ys = Sheets(1).Range("B8:B19")
temp = Sheets(1).Range("E3").Value
Dim indexxs As Integer
Dim indexys As Integer
Dim indexxs1 As Integer
Dim indexys1 As Integer
var = WorksheetFunction.Match(temp, xs, 1)
var1 = WorksheetFunction.Match(temp, xs, 1) + 1
indexxs = Application.WorksheetFunction.Index(xs, var)
indexxs1 = Application.WorksheetFunction.Index(xs, var1)
indexys = Application.WorksheetFunction.Index(ys, var)
indexys1 = Application.WorksheetFunction.Index(ys, var1)
Sheets(1).Range("g3").Value = Application.WorksheetFunction.Forecast(temp,indexys:indexys1,indexxs:indexxs1)
End Sub
答案 0 :(得分:1)
forcast想要一个范围或一个数组,而不是你传递的数字indexys:indexys1
你传递索引的值,所以它会返回类似104:34
而不是范围或一个数组。
您可以将Forcast行更改为:
Sheets(1).Range("g3").value = Application.Forecast(temp, Array(indexys, indexys1), Array(indexxs, indexxs1))
它会起作用,但有更好,更快的方法来使用vba而不是工作表函数来获得所需的结果。
因此我们可以摆脱索引并创建值的数组。然后在公式中使用这些数组。
Sub interp()
Dim temp As Long
Dim var As Long
Dim var1 As Long
Dim xs As Range
Dim ys As Range
With Worksheets(1)
Set xs = .Range("A8:A19")
Set ys = .Range("B8:B19")
temp = .Range("E3").value
var = WorksheetFunction.Match(temp, xs, 1)
var1 = var + 1
Dim y As Variant
y = .Range(ys.Cells(var, 1), ys.Cells(var1, 1)).value
Dim x As Variant
x = .Range(xs.Cells(var, 1), xs.Cells(var1, 1)).value
.Range("g3").value = Application.WorksheetFunction.Forecast(temp, y, x)
End With
End Sub