Populating a Multidimensional Array of Unknown Size

foy1der

New Member
Joined
Apr 13, 2010
Messages
15
I would like to create a multidimensional array but I don't know what the size should be. The size of each dimension will be different and the number of dimensions will depend on a variable defined earlier in the code.

Initially, I thought that I could create a create an array of arrays, but this hasn't seemed to work.

Here is my code.
Code:
    'Find members to load rate
    Range("A1").End(xlDown).Select
    size = Selection.Count - 1
    ReDim membernames(size)
    ReDim member_lengths(size)
    i = 0
    Do While i <= size
        membernames(i) = Range("A" & i + 1).Value
        i = i + 1
    Loop
    i = 0
    
    'calculate the length of each member
    Do While i <= size
        'get names of points
        ret = SapModel.FrameObj.GetPoints(membernames(i), Point1, Point2)
        ret = SapModel.PointObj.GetCoordCartesian(Point1, x1, y1, z1)
        ret = SapModel.PointObj.GetCoordCartesian(Point2, x2, y2, z2)
        Length = ((x1 - x2) ^ 2 + (y1 - y2) ^ 2 + (z1 - z2) ^ 2) ^ (1 / 2)
        member_lengths(i) = Length
    Loop
    
    'determine number of sections for each member
    'members will be broken into pieces that are less than one foot in length
    ReDim number_sections(size)
    i = 0
    Do While i <= size
        number_sections(i) = WorksheetFunction.Ceiling(member_lengths(i), 1)
        i = i + 1
    Loop
    
    'break members into sections
    ReDim section_size(size)
    i = 0
    Do While i <= size
        section_size(i) = member_lengths(i) / number_sections(i)
        i = i + 1
    Loop
    
    i = 0
    'create array denoting member points of interest
    ReDim divided_members(size)
    Do While i <= size
        j = 0
        ReDim temp(number_sections(i))
        Do While j <= number_sections(i)
            temp(j) = j * section_size(i)
            j = j + 1
        Loop
        divided_members(i) = temp
        i = i + 1
    Loop
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi

I did not go through your code that has lots of variables and functions that I have no idea of what they mean, but I think your problem boils down to understanding the correct syntax to create and load a jagged array dynamically.

I post an example of how to create a jagged array, statically and dynamically.

Ex.
You want to create a jagged array where

- the first dimension has 3 elements

- the first element in the first dimension is an array with 4 elements with the values 1,2,3,4
- the second element in the first dimension is an array with 2 elements with the values 10,20
- the third element in the first dimension is an array with 3 elements with values 100,200,300

2 options

Option 1 - you know the structure of the jagged array and all the values beforehand. Sub JaggedArray1()

Option 1 - you don't know the structure of the jagged array beforehand. You execute some code to get how many elements in each dimension and you dimension and load the jagged array dynamically. Sub JaggedArray2()

Remark: this is a simple example, just meant to be a starting point to understanding the syntax.

HTH

Insert a general module and paste:

Code:
Option Explicit
Option Base 1
 
Sub JaggedArray1()
Dim vJagArr As Variant
 
vJagArr = Array( _
    Array(1, 2, 3, 4), _
    Array(10, 20), _
    Array(100, 200, 300))
 
' display 2 elements for testing
MsgBox "vJagArr(2)(1): " & vJagArr(2)(1) & ", vJagArr(3)(2): " & vJagArr(3)(2)
End Sub
 
Sub JaggedArray2()
Dim vJagArr As Variant, v() As Long
Dim lDim1 As Long, lDim11 As Long, lDim12 As Long, lDim13 As Long
 
' Initialisation of the total number of elements in each dimention
' that your code will get somehow
lDim1 = 3
lDim11 = 4
lDim12 = 2
lDim13 = 3
 
' allocate space for the first dimension
ReDim vJagArr(1 To lDim1)
 
' allocate space for the second dimensions
ReDim v(1 To lDim11)
vJagArr(1) = v
 
ReDim v(1 To lDim12)
vJagArr(2) = v
 
ReDim v(1 To lDim13)
vJagArr(3) = v
 
' Initialise the values in the array
vJagArr(1)(1) = 1: vJagArr(1)(2) = 2: vJagArr(1)(3) = 3: vJagArr(1)(4) = 4
vJagArr(2)(1) = 10: vJagArr(2)(2) = 20
vJagArr(3)(1) = 100: vJagArr(3)(2) = 200: vJagArr(3)(3) = 300
 
' display 2 elements for testing
MsgBox "vJagArr(2)(1): " & vJagArr(2)(1) & ", vJagArr(3)(2): " & vJagArr(3)(2)
End Sub
 
Upvote 0
I guess I wasn't very clear when I stated my problem. Your code is similar to what I am looking for, but it's not quite there. What I need is an array of n-dimensions. Alternatively, I could create n arrays.
Either way, I don't know how large each dimension of the array is. That isn't a big deal to fix, I can easily define an array with a size that is based on a predefined variable.

The trouble really comes in when I have the following situation:
Code:
dim array(subarray1(size1) , subarray2(size2) , ... , subarrayN(sizeN)

I don't know how to tell vb how many subarrays I have.
 
Upvote 0
The trouble really comes in when I have the following situation:
Code:
dim array(subarray1(size1) , subarray2(size2) , ... , subarrayN(sizeN)

Sorry, I don't understand. Isn't it what my code does, with 3 subarrays each one with a different number of elements? Like in pseudo code


Code:
dim array(subarray1(4) , subarray2(2) ,subarrayN(3)
 
Upvote 0
After I thought about your code I realized all I had to do was impliment that into a loop structure and it should work. Should. I'll post back when something new comes up.
 
Upvote 0
So, this is what I came up with:

Rich (BB code):
    i = 0
    'create array denoting member points of interest
    ReDim divided_members(size)
    Do While i <= size
        ReDim temp(number_sections(i))
        divided_members(i) = temp
    Loop
        
    i = 0
    'populate array denoting member points of interest
    Do While i <= size
        Do While j <= number_sections(i)
            divided_members(i)(j) = j * section_size(i)
            j = j + 1
        Loop
        i = i + 1
    Loop

The trouble I'm having now is that I get a compile error: type mismatch at the bolded location. Forgot to mention, temp() and divided_members() are both defined as double.
 
Last edited:
Upvote 0
Hi

You cannot define divided_members as Double(). This would mean that divided_members is an array of doubles, which is not the case, divided_members is an array of arrays.

Try dimensioning divided_members as Variant.

On the other hand temp can be a Double(), because, in fact, each of these subarrays will hold doubles.
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,866
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top