Split an Array in VBA

flanna

New Member
Joined
Jan 30, 2016
Messages
34
I need to do something like this that works in VBA for Excel:
Code:
HowMany As Single
FromEnd As Single
LargeArray As Variant
NewArray As Variant
HowMany = 10
FromEnd = 5
LargeArray =(1,2,3,,,10000)
NewArray = LargeArray.split(HowMany, FromEnd)
That doesn't work in VBA.
What I want is to take a large, one dimensional array of numbers and make a new array that contains the 10 numbers that are right before the last 5 numbers in the array.
Here's an example:
LargeArray(1,2,3,4,5,6,7,8,9,,,,,,,100,101,102,103,104,105,106,107,108,109,110)
Extracting from it this new array:
NewArray(101,102,103,104,105)
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I need to do something like this that works in VBA for Excel:
Code:
HowMany As Single
FromEnd As Single
LargeArray As Variant
NewArray As Variant
HowMany = 10
FromEnd = 5
LargeArray =(1,2,3,,,10000)
NewArray = LargeArray.split(HowMany, FromEnd)
That doesn't work in VBA.
What I want is to take a large, one dimensional array of numbers and make a new array that contains the 10 numbers that are right before the last 5 numbers in the array.
Here's an example:
LargeArray(1,2,3,4,5,6,7,8,9,,,,,,,100,101,102,103,104,105,106,107,108,109,110)
Extracting from it this new array:
NewArray(101,102,103,104,105)
Oops, the NewArray should be (96,97,98,99,100,101,102,103,104,105)
 
Upvote 0
Hi flanna,

See if this comes close to what you want...

Code:
Sub test()


    Dim LargeArray() As Variant
    Dim NewArray() As String
    Dim NA As String
    Dim i As Long
    Dim HowMany As Long, FromEnd As Long


    HowMany = 3
    FromEnd = 6
    LargeArray = Array(1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20)
    For i = UBound(LargeArray) - FromEnd To UBound(LargeArray) - FromEnd + HowMany - 1
        NA = NA & LargeArray(i) & ","
    Next
    NewArray() = Split(NA)
    MsgBox NewArray(0)


End Sub

HTH

igold
 
Upvote 0
Oops, the NewArray should be (96,97,98,99,100,101,102,103,104,105)
Your sample set HowMany=10 and FromEnd=5... please explain what the FromEnd variable is from the end of (if it is from the end of the array, then you cannot get 10 elements from the array if you start drawing from there). Also, are you looking for NewArray to be a VBA one-dimensional array or an Excel two-dimensional array (like you get when you assign a range to a Variant variable)?
 
Upvote 0
Hi flanna,

See if this comes close to what you want...

Code:
Sub test()


    Dim LargeArray() As Variant
    Dim NewArray() As String
    Dim NA As String
    Dim i As Long
    Dim HowMany As Long, FromEnd As Long


    HowMany = 3
    FromEnd = 6
    LargeArray = Array(1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20)
    For i = UBound(LargeArray) - FromEnd To UBound(LargeArray) - FromEnd + HowMany - 1
        NA = NA & LargeArray(i) & ","
    Next
    NewArray() = Split(NA)
    MsgBox NewArray(0)


End Sub
Your code outputs 13,14,15... shouldn't it output 14,15,16 instead (6th element from the end)?
 
Upvote 0
The FromEnd argument is 0 based, i.e. if 105 is "5 FromEnd", then 110, the last entry, is "0 FromEnd"
and your HowMany augment is 1 based.

If you want your ResultArray to be 1-based, you could use code like

Code:
Dim ResultArray()
' ...

ReDim ResultArray(1 to HowMany)
For i = 1 to HowMany
    ResultArray(i) = BigArray(Ubound(BigArray) - (HowMany +1) - FromEnd + i)
Next i

Excel VBA doesn't have many functions that manipulate arrays quickly, brute force looping is often needed.
 
Upvote 0
The FromEnd argument is 0 based, i.e. if 105 is "5 FromEnd", then 110, the last entry, is "0 FromEnd"
and your HowMany augment is 1 based.

If you want your ResultArray to be 1-based, you could use code like

Code:
Dim ResultArray()
' ...

ReDim ResultArray(1 to HowMany)
For i = 1 to HowMany
    ResultArray(i) = BigArray(Ubound(BigArray) - (HowMany +1) - FromEnd + i)
Next i

Excel VBA doesn't have many functions that manipulate arrays quickly, brute force looping is often needed.
If we assume that these assignments...

HowMany = 10
FromEnd = 5

are reversed and should have actually been this...

HowMany = 5
FromEnd =10

then the OP's NewArray of 101, 102, 103, 104, 105 in Message #1 makes more sense... you start the new array at the 10th element from the end and count it and 4 more (making 5 total to match what I think the HowMany should have been). Given this, we can create the one-dimensional array (which start at element #1) without using any loops whatsoever...

Code:
[table="width: 500"]
[tr]
	[td]Sub test()
  
  Dim HowMany As Long, FromEnd As Long, Start As Long, ElementCount As Long
  Dim LargeArray As Variant, NewArray As Variant
  
  HowMany = 5
  FromEnd = 10
    
  ElementCount = 110
  LargeArray = Evaluate("ROW(1:" & ElementCount & ")")
  
  Start = UBound(LargeArray) - FromEnd + 1
  NewArray = Application.Transpose(Application.Index(LargeArray, Evaluate("ROW(" & Start & ":" & Start + HowMany - 1 & ")"), 1))

  [COLOR="#008000"]' Let's see the results (joined together for display purposes)[/COLOR]
  MsgBox Join(NewArray, ", ")
  
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Rick,

Your code outputs 13,14,15... shouldn't it output 14,15,16 instead (6th element from the end)?

I did struggle with that, but seeing how it is really dependent upon how the OP is doing the counting, I figured if he could figure out how to change the code to match his preference...

igold
 
Upvote 0
I did struggle with that, but seeing how it is really dependent upon how the OP is doing the counting...
See the mini-analysis I posted in my response to mikerickson in Message #7.
 
Last edited:
Upvote 0
Your sample set HowMany=10 and FromEnd=5... please explain what the FromEnd variable is from the end of (if it is from the end of the array, then you cannot get 10 elements from the array if you start drawing from there). Also, are you looking for NewArray to be a VBA one-dimensional array or an Excel two-dimensional array (like you get when you assign a range to a Variant variable)?
FromEnd is how many numbers from the end of the LargeArray the NewArray ends.
So with these variables: LargeArray(1,2,3,4,5,6,7,8,9,10) HowMany=3 FromEnd=2
NewArray would be (6,7,8). 3 numbers that finish 2 numbers from end of LargeArray.

NweArray would be a VBA one-dimensional array.
 
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