split a large array into smaller array

brodaddy2002

Board Regular
Joined
Jan 21, 2013
Messages
67
I am trying to separate a 2D array of integers into multiple 1D array of integers. Does anyone know how to do that? I looked into the split, join functions, but they work with strings. I guess that I could convert my integers to strings, and put some sort of delimiter in it. The main reason I would like to do this is because of the worksheetfunction.sum(arg1). My individual arrays have to some up to something.

The only other option I can see is to use a dictionary or collection with the arrays as containers. That would probably work as well, but I just wanted to hear your thoughts on getting splitting up a 2D array into smaller array. I have been able to do this using pointers in C, but am not sure if it can be done in VBA.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
use a 1D array of 1D arrays

Code:
Sub test()


    Dim my2DArray(1 To 5) As Variant
    
    my2DArray(1) = Array(1, 2, 3, 4, 5)
    my2DArray(2) = Array("abc", "xyz")
    my2DArray(3) = Array(22, 33, "qwerty")
    my2DArray(4) = Array(1, 2, 3)
    my2DArray(5) = Array(9, 8, 7)
    
End Sub
 
Upvote 0
Here's an example of how to load an existing 2D Array into a 1D array of 1D arrays.

Code:
Sub Example2DArrayTo1DArrayOf1DArrays()

Dim Input_Rng, Temp(), Output()

'load an example 2D Array A1:E3
Input_Rng = Range("A1").Resize(3, 5).Value

'split out rows
ReDim Output(1 To UBound(Input_Rng))

For i = 1 To UBound(Input_Rng)
    ReDim Temp(1 To UBound(Input_Rng, 2))
    For j = 1 To UBound(Input_Rng, 2)
        Temp(j) = Input_Rng(i, j)
    Next j
    Output(i) = Temp
Next i

'show the format to identify one value
MsgBox Output(3)(2)
End Sub
 
Upvote 0
I am trying to separate a 2D array of integers into multiple 1D array of integers. Does anyone know how to do that? I looked into the split, join functions, but they work with strings. I guess that I could convert my integers to strings, and put some sort of delimiter in it. The main reason I would like to do this is because of the worksheetfunction.sum(arg1). My individual arrays have to some up to something.
Which direction do you want to slice the 2D array... row-wise or column-wise?

Would it be okay to give you code that produced the sums you are looking for, or do you need the 1D array "slices" for some other purpose?
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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