EXCEL VBA 错误:“编译错误:预期数组”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17255458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:56:38  来源:igfitidea点击:

EXCEL VBA Error: "Compile Error: Expected Array"

arraysexcelvbacompiler-errors

提问by Elias

Can anyone help me?

谁能帮我?

I have been getting a compile error (...: "Expected Array") when dealing with arrays in my Excel workbook.

在处理 Excel 工作簿中的数组时,我遇到了编译错误(...:“预期数组”)。

Basically, I have one 'mother' array (2D, Variant type) and four 'baby' arrays (1D, Double type). The called subroutine creates the publicly declared arrays which my main macro ends up using for display purposes. Unfortunately, the final of the baby arrays craps out (giving the "Compile Error: Expected Array"). Strangely, if I remove this final baby array ('final' - as in the order of declaration/definition) the 2nd to last baby array starts crapping out.

基本上,我有一个 'mother' 数组(2D,Variant 类型)和四个 'baby' 数组(1D,Double 类型)。被调用的子例程创建公开声明的数组,我的主宏最终将其用于显示目的。不幸的是,最后一个婴儿数组失败了(给出了“编译错误:预期数组”)。奇怪的是,如果我删除这个最后的婴儿数组('final' - 按照声明/定义的顺序),倒数第二个婴儿数组就会开始废话。

Here is my code:

这是我的代码:

 Public Mother_Array() as Variant, BabyOne_Array(), BabyTwo_Array(), BabyThree_Array(), BabyFour_Array() as Double 'declare may other variables and arrays, too

Sub MainMacro()
    'do stuff

   Call SunRaySubRoutine(x, y)

    'do stuff

    Range("blah") = BabyOne_Array: Range("blahblah") = BabyTwo_Array
    Range("blahbloh" = BabyThree_Array: Range("blahblue") = BabyFour_Array

End Sub

Sub SunRaySubRoutine(x,y)
    n = x * Sheets("ABC").Range("A1").Value + 1

    ReDim Mother_Array(18, n) as Variant, BabyOne_Array(n), BabyTwo_Array(n) as Double
    ReDim BabyThree_Array(n), BabyFour_Array(n) as Double

    'do stuff

    For i = 0 to n

        BabyOne_Array(i) = Mother_Array(0,i)
        BabyTwo_Array(i) = Mother_Array(2,i)
        BabyThree_Array(i) = Mother_Array(4,i)
        BabyFour_Array(i) = Mother_Array(6,i)
    Next        

End Sub

I have tried to declare all arrays as the Variant type, but to no avail. I have tried to give BabyFour_Array() a different name, but to no avail.

我试图将所有数组声明为 Variant 类型,但无济于事。我试图给 BabyFour_Array() 一个不同的名字,但无济于事。

What's really strange is that even if I comment out the part which makes the BabyFour_Array(), the array still has zero values for each element.

真正奇怪的是,即使我注释掉了构成 BabyFour_Array() 的部分,数组的每个元素仍然具有零值。

What's also a bit strange is that the first baby array never craps out (although, the 2nd one crapped out once (one time out of maybe 30).

还有一点有点奇怪的是,第一个婴儿数组从来没有出现过(尽管第二个数组出现过一次(可能是 30 次中的一次)。

BANDAID: As a temporary fix, I just publicly declared a fifth dummy array (which doesn't get filled or Re-Dimensioned). This fifth array has no actual use besides tricking the system out of having the "Compile Error: Expected Array".

BANDAID:作为临时修复,我只是公开声明了第五个虚拟数组(它不会被填充或重新调整尺寸)。这第五个数组除了诱使系统摆脱“编译错误:预期数组”之外没有实际用途。

Does anyone know what's causing this "Compile Error: Expected Array" problem with Excel VBA?

有谁知道是什么导致 Excel VBA 出现“编译错误:预期数组”问题?

Thanks,

谢谢,

Elias

埃利亚斯

回答by Doug Glancy

In your global declarations you are only declaring the last baby array as Double. You're declaring the first three as arrays of Variants. But in the subroutine you are Redimmingbabies one and three as Variants, and two and four as Doubles.

在您的全局声明中,您只将最后一个婴儿数组声明为Double. 您将前三个声明为Variants. 但是在子程序中,您将Redimming婴儿一和三作为变体,二和四作为双打。

See this Chip Pearson pageand scroll down to "Pay Attention To Variables Declared With One Dim Statement."

请参阅Chip Pearson 页面并向下滚动到“注意用一个模糊语句声明的变量”。

In VBA when you declare something like:

在 VBA 中,当您声明如下内容时:

Dim x, y, z as Long

only z is a Long, the rest are Variants. The correct form is:

只有 z 是 a Long,其余都是Variants。正确的形式是:

Dim x as Long, Y as Long, Z as Long(or Double arrays in your case.)

Dim x as Long, Y as Long, Z as Long(或在您的情况下使用双数组。)

You can see how this would cause the behavior you describe, i.e., the last one errors, but the others don't.

您可以看到这将如何导致您描述的行为,即,最后一个错误,但其他错误则不然。