【发布时间】:2018-01-22 21:32:40
【问题描述】:
我正在使用 excel vba 2013。我正在尝试开发 2D 插值函数。我得到的错误是:“编译器错误:无效的限定符”,函数行以黄色突出显示。
我相信我的问题与“knownZ”以及我稍后在代码中引用它的方式有关。请注意,使用代码中的前 3 行和第一部分就足以获取错误。其余代码列出给感兴趣的人。
这是我的代码:
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”是一个二维单元格范围。
请注意,当我删除所有“KnownZ”引用时,不再生成错误。
我尝试搜索范围和数组的信息,但找不到我的代码的问题!任何帮助都将不胜感激,即使它是指向我应该阅读的特定页面的链接。感谢您的宝贵时间!
【问题讨论】:
-
突出显示的是哪一行?将其描述为“黄色”对我们没有帮助。
-
正如我所说,“功能行”被突出显示,这意味着它是代码中的第二行:
Function LinIntrpAsc2D(KnownX As Range, KnownY As Range, KnownZ As Range, X2 As Double, Y2 As Double) As Variant感谢您抽出宝贵时间帮助我! -
当我尝试编译你的代码时,我得到了
KnownX.Column.Count突出显示的行。我认为这是你的错字——你可能想要KnownX.Columns.Count(注意s)Column返回一个Long——对它应用Count是没有意义的。 -
非常感谢,我对VBA没有经验,我没有注意到Column和Columns之间的区别。错误不再给出!代码现在输出#value,但这是我自己要解决的另一个问题。再次感谢您。
-
@HoussamHalaby 值得注意的是,您不需要使用
WorksheetFunction.Or()。只需使用Or()。 VBA 内置了比较,例如OR和AND。您可以使用NOT OR或NOT AND来获得逆。