简体   繁体   中英

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.

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.

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.

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.

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).

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".

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

Thanks,

Elias

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 Redimming babies one and three as Variants, and two and four as Doubles.

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

In VBA when you declare something like:

Dim x, y, z as Long

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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