+ Reply to Thread
Results 1 to 7 of 7

Declaring multiple multi-dimensional arrays (jagged arrays) - compile error?

  1. #1
    Registered User
    Join Date
    12-26-2012
    Location
    San Francisco
    MS-Off Ver
    Excel 2010
    Posts
    2

    Declaring multiple multi-dimensional arrays (jagged arrays) - compile error?

    Hi -

    I'm trying to declare multiple dynamic arrays, with each dynamic array having the same dimensions.

    (I'm taking data from two separate sheets that have the same dimensions but different values, and want to subtract one from the other).

    So Array1 has two dimensions of unspecified size
    and Array2 has two dimensions of the same unspecified size.

    I would think the declaration would be Dim Array(2)() as Variant (or whatever)

    Another option would be to declare a 3 dimensional array, Dim Array(2,,)

    I found these examples in the MSDN VBA guide. But I get a compile error when declaring these. From reading through a bunch of threads, it seems that this has something to do with the difference between VBA.net and VBA for Excel (I'm using the latter). Why there are two different types of VBA from the same company that don't recognize the same code, I have no idea. I'll chalk that up to yet another way Microsoft makes people's lives miserable.

    So any help would be appreciated. How do I declare an sequence of 2-dimensional arrays - Array1,Array2,Array3 -

    Where Array(1)(1,1) = the first cell in the array1
    and Array(2)(1,2) = the first cell in array2

    But important note, I don't know in advance the dimensions of the arrays, I only know I need 2 (or more) of them, all with the same dimensions but different values.

    Thanks for any help.

    Dave

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Declaring multiple multi-dimensional arrays (jagged arrays) - compile error?

    Please Login or Register  to view this content.
    Last edited by AB33; 12-20-2013 at 04:34 PM.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Declaring multiple multi-dimensional arrays (jagged arrays) - compile error?

    Dave

    How are you goint to populate the arrays?

    Also, how are you going to 'subtract' them?
    If posting code please use code tags, see here.

  4. #4
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270

    Re: Declaring multiple multi-dimensional arrays (jagged arrays) - compile error?

    To create 2 separate arrays you give them 2 different variable names ie. array1 and array2. To determine the size of the array you use the parenthesis.

    U can use ubound to find the end of the array, or if your data is continuous you can just check for empty values to stop. If you are doing dynamic then you are keeping track of each value being placed into the array so you would know when it ends.

    Here is an example
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    12-26-2012
    Location
    San Francisco
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Declaring multiple multi-dimensional arrays (jagged arrays) - compile error?

    OK, thanks everyone. This is helpful.

    As far as the question to how I'm going to subtract them:

    For x = 1 to [upper bound of x dimension]
    For y = 1 to [upper bound of y dimension]
    SubtractedArray(x,y) = Array1(x,y) - Array2(x,y)
    Next y
    Next x

    I'd still think it would be simpler if I could declare it like Dim Array(2)()() as [whatever] to signify that I know there are 2 arrays but don't know the x and y dimensions of them (and will define later in code).

    But AB33's idea will do the trick.

    Thanks for the quick reply.

    Dave

    Dave

  6. #6
    Forum Contributor
    Join Date
    04-24-2007
    Location
    NYC
    MS-Off Ver
    2k3/2k7/2010
    Posts
    270

    Re: Declaring multiple multi-dimensional arrays (jagged arrays) - compile error?

    well that depends where you want to hold the subtracted value, right now you created a 3rd array to hold the new value. you can easily put subtracted value back into one of the 2 arrays you started with if you want. If you make the 3rd array, you'll just have to take ubound of both arrays and use the largest dimension of both.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Declaring multiple multi-dimensional arrays (jagged arrays) - compile error?

    Dave

    What if the arrays have, as you indicated in your first post, different upper dimensions,?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Passing Multi-Dimensional Arrays
    By dldecler in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-28-2011, 02:03 PM
  2. Multi-Dimensional Arrays
    By ajocius in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-09-2009, 11:20 AM
  3. Writing Multi-Dimensional Arrays to Ranges
    By Caligula in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2008, 05:40 AM
  4. Multi-dimensional arrays
    By gti_jobert in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-06-2006, 12:45 AM
  5. [SOLVED] [SOLVED] Declaring 2 dimensional arrays
    By Dan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-17-2005, 04:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1