简体   繁体   English

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

[英]EXCEL VBA Error: “Compile Error: Expected Array”

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). 基本上,我有一个'母'阵列(2D,Variant类型)和四个'baby'阵列(1D,Double类型)。 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. 奇怪的是,如果我删除这个最终的婴儿阵列('最终' - 按照声明/定义的顺序),第二个到最后一个婴儿阵列开始剔除。

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. 我试图将所有数组声明为Variant类型,但无济于事。 I have tried to give BabyFour_Array() a different name, but to no avail. 我试图给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). BANDAID:作为一个临时修复,我只是公开声明了第五个虚拟数组(没有填充或重新标注)。 This fifth array has no actual use besides tricking the system out of having the "Compile Error: Expected Array". 除了“编译错误:预期数组”之外,该第五个数组除了欺骗系统之外没有实际用途。

Does anyone know what's causing this "Compile Error: Expected Array" problem with Excel VBA? 有谁知道Excel VBA导致这个​​“编译错误:预期数组”问题是什么?

Thanks, 谢谢,

Elias 埃利亚斯

In your global declarations you are only declaring the last baby array as Double . 在全局声明中,您仅将最后一个婴儿数组声明为Double You're declaring the first three as arrays of Variants . 您将前三个声明为Variants数组。 But in the subroutine you are Redimming babies one and three as Variants, and two and four as Doubles. 但是在子程序中,你正在Redimming婴儿一和三Redimming为变体,将两个和四个RedimmingRedimming

See this Chip Pearson page and 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 . 只有z是Long ,其余都是Variants The correct form is: 正确的形式是:

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 (或您的Double数组。)

You can see how this would cause the behavior you describe, ie, the last one errors, but the others don't. 您可以看到这将导致您描述的行为,即最后一个错误,但其他错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM