VBA if 语句中的编译错误

VBA Compile error inside if statement

If IsArray(payCsv(pay_id)) = False Then
    'create tempArray
    lc = 0
    Debug.Print "create array"
End If

If IsArray(payCsv(pay_id)) = True Then
    Debug.Print " array exists, we should be able to get ubound"
    lc = UBound(payCsv(0)) - LBound(payCsv(0))
    l = l + 1
End If

我正在使用上面的代码来确定我是否可以在我的二维数组上使用 Ubound(即,如果创建了第二维,则获取长度(ubound - lbound)。

但是,我收到编译错误,即使条件 2 为假,它也无法识别代码不相关。

我正在测试一个数组,结果是如果我注释掉 "lc = UBound(payCsv(0)) - LBound(payCsv(0))" 就是 "create array"。

如果我把这一行留在那里,我会得到错误 "compile error - expected array"

这是 VBA 中的错误吗?

如果要访问数组第2维的UBound,格式如下:

UBound(payCSV, 2)

此功能上的 MSDN page 可能会有帮助。

当您像现在这样访问 payCSV(0) 时,代码假定您想要 payCSV 数组的第一个维度中的第一个元素。

也许你想试试这个?

If IsArray(payCsv(pay_id)) = False Then
    'create tempArray
    lc = 0
    Debug.Print "create array"
Else
    Debug.Print " array exists, we should be able to get ubound"
    lc = UBound(payCsv, 2) - LBound(payCsv, 2)
    l = l + 1
End If