VBA split array into 2 arrays

flanna

New Member
Joined
Jan 30, 2016
Messages
34
Is there an easy way to split an array into 2 smaller arrays.
For example:
FullArray(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
Split this array into 2 arrays with first array containing the first 10 items and the second array containing the remaining items.
SizePart1 = 10
SizePart2 = UBound(FullArray) - SizePart1

The 2 new arrays would be like this;
ArrayPart1(1,2,3,4,5,6,7,8,9,10)
ArrayPArt2(11,12,13,14,15)​
All arrays are one dimensional and contain only numbers. They are VBA arrays, not Excel columns.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi,

The obvious way is to create two arrays of the correct size then use a For/Next loop to populate them.

If you want to be more adventurous and if you are using Windows then you could try an ArrayList object. This does some things like a conventional array, it does not do some things that an array does and it does some things that an array can't do - sub-setting and sorting, for instance. Try this:
Code:
Sub GetRange()

    Dim myAL As Object
    Dim mySubAL As Object
    Set myAL = CreateObject("System.Collections.ArrayList")
    Set mySubAL = CreateObject("System.Collections.ArrayList")

    myAL.Add ("The")
    myAL.Add ("quick")
    myAL.Add ("brown")
    myAL.Add ("fox")
    myAL.Add ("jumped")
    myAL.Add ("over")
    myAL.Add ("the")
    myAL.Add ("lazy")
    myAL.Add ("dog")
    
    
    Set mySubAL = myAL.GetRange(2, 4)
    
    MsgBox Join(myAL.ToArray(), vbNewLine)
    MsgBox Join(mySubAL.ToArray(), vbNewLine)
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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