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

侯萨姆·哈拉比

我正在使用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”引用时,将不再生成错误。

我尝试搜索有关范围和数组的信息,但是找不到我的代码存在的问题!任何帮助都将不胜感激,即使它是指向我应该阅读的特定页面的链接也是如此。感谢您的时间!

约翰·科尔曼

当错误信息第一次弹出时(在突出显示任何行之前),您应该看到以下内容:

在此处输入图片说明

请注意.Column突出显示。问题是.Column返回一个长整数(电子表格中第一列的索引)。如果要收集所有列,则需要使用.Columns:(KnownX.Columns.Count在代码中的其他位置进行了类似的修复)。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

excel,查找stament地址无效限定符

VBA Excel-编译错误-无效使用属性

将类型字符串数组复制到Excel中的范围-无效的限定符

子名称上的 VBA“无效限定符”错误

Excel VBA错误:“编译错误:预期:语句结尾”

VBA、Excel。执行偏移和调整范围大小然后复制它的代码。“编译错误:无效的监视表达式”

Excel VBA Formula1编译错误

Excel vba编译错误-参数不是可选的,

VBA Excel宏公式编译错误-双引号

VBA Excel-需要编译错误对象

VBA Excel 中的编译错误“未定义变量”

带有string.copy的VBA无效限定符

包含数组的“无效限定符”错误。包含

Excel 2013 64位VBA语法错误“编译错误:“预期:语句结尾”

保存工作簿时出现新的Excel VBA编译错误

由于编译错误,VBA Excel无法运行:类型不匹配

如何修复编译错误:从Outlook使用Excel VBA时未定义用户定义类型?

为什么excel VBA中不等于“<>”符号显示编译错误

从VBA中的子调用函数-限定符错误

编译错误:Excel宏中的语法错误

excel + vba + 编译错误 AutoOpenRequiredWorkbook (myFileNameToOpen, myFilePath) 无法弄清楚为什么我会收到此错误

从Excel提取时发生编译错误

在某些C ++编译器上使用限制限定符的编译器错误

后期绑定编译错误:未定义引用Excel VBA中的Outlook mailitem的用户定义类型

具有2个对象参数的Excel VBA对象子调用给出了编译错误:预期=

错误:0:3:错误(#279)无效的布局限定符“位置”

编译器“错误:将'const something'作为'this'参数传递会丢弃限定符”

VBA Excel:无效的属性

在其他容器中使用boost :: container :: static_vector时,gcc编译错误“将'const s'绑定到类型's&'的引用会丢弃限定符”