我正在使用excel vba2013。我正在尝试开发2D插值函数。我得到的错误是:“编译器错误:无效的限定符”,功能行以黄色突出显示。
我相信我的问题与“ knownZ”有关,以及稍后在代码中如何引用它。请注意,使用代码的前3行和第I部分足以获取错误。其余代码针对感兴趣的人列出。
这是我的代码:
Option Explicit
Function LinIntrpAsc2D(KnownX As Range, KnownY As Range, KnownZ As Range, X2 As Double, Y2 As Double) As Variant
Dim Y0 As Double, Y1 As Double, X0 As Double, X1 As Double, i As Long, j As Long, Z() As Variant, Z1 As Double, Z2 As Double, Xfound As Boolean, Yfound As Boolean
'------------------------------------------------------------------------------
'I. Check Preliminary Error
'------------------------------------------------------------------------------
If WorksheetFunction.Or(KnownX.Column.Count <> KnownZ.Column.Count, KnownY.Row.Count <> KnownZ.Row.Count) Then
LinIntrpAsc2D = "Number of rows or columns for given range does not match."
Exit Function
End If
'------------------------------------------------------------------------------
'II. Interpolation
'------------------------------------------------------------------------------
Xfound = False
Yfound = False
For i = 1 To KnownX.Column.Count
If X2 = KnownX.Cells(i) Then
Xfound = True
GoTo 1
ElseIf KnownX.Cells(i) < X2 Then
X0 = KnownX.Cells(i)
ElseIf KnownX.Cells(i) > X2 Then
X1 = KnownX.Cells(i)
GoTo 1
End If
Next i
1:
For j = 1 To KnownY.Row.Count
If Y2 = KnownY.Cells(j) Then
Yfound = True
GoTo 2
ElseIf KnownY.Cells(j) < Y2 Then
Y0 = KnownY.Cells(j)
ElseIf KnownY.Cells(j) > Y2 Then
Y1 = KnownY.Cells(j)
GoTo 2
End If
Next j
2:
If WorksheetFunction.And(Xfound = False, Yfound = False) Then
Z(1, 1) = KnownZ(j - 1, i - 1)
Z(1, 2) = KnownZ(j - 1, i)
Z(2, 1) = KnownZ(j, i - 1)
Z(2, 2) = KnownZ(j, i)
Z1 = (X2 - X0) * (Z(1, 2) - Z(1, 1)) / (X1 - X0) + Z(1, 1)
Z2 = (X2 - X0) * (Z(2, 2) - Z(2, 1)) / (X1 - X0) + Z(2, 1)
LinIntrpAsc2D = (Y2 - Y0) * (Z2 - Z1) / (Y1 - Y0) + Z1
Exit Function
ElseIf WorksheetFunction.And(Xfound = False, Yfound = True) Then
Z(1, 1) = KnownZ(j, i - 1)
Z(1, 2) = KnownZ(j, i)
LinIntrpAsc2D = (X2 - X0) * (Z(1, 2) - Z(1, 1)) / (X1 - X0) + Z(1, 1)
Exit Function
ElseIf WorksheetFunction.And(Xfound = True, Yfound = False) Then
Z(1, 1) = KnownZ(j - 1, i)
Z(2, 1) = KnownZ(j, i)
LinIntrpAsc2D = (Y2 - Y0) * (Z(2, 1) - Z(1, 1)) / (Y1 - Y0) + Z(1, 1)
Exit Function
ElseIf WorksheetFunction.And(Xfound = True, Yfound = True) Then
LinIntrpAsc2D = KnownZ(j, i)
Exit Function
End If
End Function
我在excel工作表中使用的代码是=LinIntrpAsc2D(C19:P19,B20:B32,C20:P32,1250,3.5)
请注意,“ KnownX”是单元格的一维水平列表,而“ KnownY”与“ KnownX”相同,但是垂直的。“ KnownZ”是2D范围的单元格。
请注意,当我删除所有“ KnownZ”引用时,将不再生成错误。
我尝试搜索有关范围和数组的信息,但是找不到我的代码存在的问题!任何帮助都将不胜感激,即使它是指向我应该阅读的特定页面的链接也是如此。感谢您的时间!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句