Excel VBA编译错误:无效的限定符

时间:2018-01-22 13:06:38

标签: excel vba excel-vba

我正在使用excel vba 2013.我正在尝试开发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)请注意&#34; KnownX&#34;是1D水平的单元格列表,而#34; KnownY&#34;与&#34; KnownX&#34;相同但垂直。 &#34; KnownZ&#34;是一系列2D细胞。

请注意,当我删除所有&#34; KnownZ&#34;引用错误不再生成。

我试过搜索有关范围和数组的信息,但无法找到我的代码的问题!任何帮助都将非常感激,即使它是我应阅读的特定页面的链接。谢谢你的时间!

1 个答案:

答案 0 :(得分:2)

首次弹出错误消息时(在任何行突出显示之前),您应该看到:

enter image description here

请注意,.Column会突出显示。问题是.Column返回一个long(电子表格中第一列的索引)。如果您想要收集所有列,则需要使用.ColumnsKnownX.Columns.Count(在代码中的其他位置使用类似的修补程序)。

如果在解除错误消息后,该函数的第一行是突出显示的行(当您将该函数作为UDF调用时),这有点误导。