VBA to Separate Values

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hi.
Please, in case is possible, I have a dynamic array five columns, the first column is only two words alternated and the other four columns are numbers (grades).
what I need?, I need to separate the rows of math from the row of geography and convert in two different arrays.
Thank you for reading this.
math40802060
geog50505050
math79301968
geog87401054
math65789140
geog32455871
math21123060
geog54238039
math54565860
geog87899193
math19324558
geog37659330
math77989140

<tbody>
</tbody>
So, I would like to separate math from geography. thanks.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
If your data is in A1:E13, you would fill across and down starting in G1 with:

Code:
=INDEX(A:A,ROW(A1)*2-1)

Fill that across from, say, G1:K1 and then down. Then the math is no longer found, if you've filled down all 13 rows, you'll get 0s.
You could remove them in the formula or format the cells not to show 0s.

In M1:Q1, a similar formula:

Code:
=INDEX(A:A,ROW(A1)*2)

Hope this helps or at least gives you an idea of an approach to get what you want.
 
Last edited:
Upvote 0
Kweaver, I really Appreciate your input, Thank you. I am really interesting about to see how this kind of ideas are implemented on the Golden Language VBA.Thank you for your time. My apology.
 
Upvote 0
Hi

If I understand correctly this is a vba question.

There are no vba native functions to deal with splitting an array.

In the case you posted, what you can do is to use a worksheet function.

Assuming the name of your array is vArray, try:

Code:
,,,
Dim vArray1 As Variant, vArray2 As Variant
Dim lN As Long ' Number of elements in the array
...

lN = UBound(vArray) - LBound(vArray) + 1

vArray1 = Application.Index(vArray, Evaluate("2*row(1:" & ((lN + 1) \ 2) & ")-1"), Array(1, 2, 3, 4, 5))
vArray2 = Application.Index(vArray, Evaluate("2*row(1:" & (lN \ 2) & ")"), Array(1, 2, 3, 4, 5))
...
 
Last edited:
Upvote 0
A possible VBA could be:

Code:
Sub SeparateRows()
Dim LR As Long, zM As Integer, zG As Integer, i As Integer
LR = Cells(Rows.Count, "A").End(xlUp).Row
zM = 1
zG = 1
For i = 1 To LR
  If Range("A" & i) = "math" Then
    Range("S" & zM & ":W" & zM) = Range("A" & i & ":E" & i).Value
    zM = zM + 1
   Else ' Range("A" & i) = "geog" Then
    Range("Y" & zG & ":AC" & zG) = Range("A" & i & ":E" & i).Value
    zG = zG + 1
  End If
Next i
End Sub

Of course, adjust where you want the results (I put them in S:W and Y:AC)
 
Upvote 0
Thank you pgc 01, It is good to know the limits of the Golden language VBA.
 
Upvote 0
Hi

If I understand correctly this is a vba question.

There are no vba native functions to deal with splitting an array.

In the case you posted, what you can do is to use a worksheet function.

Assuming the name of your array is vArray, try:

Code:
,,,
Dim vArray1 As Variant, vArray2 As Variant
Dim lN As Long ' Number of elements in the array
...

lN = UBound(vArray) - LBound(vArray) + 1

vArray1 = Application.Index(vArray, Evaluate("2*row(1:" & ((lN + 1) \ 2) & ")-1"), [B][COLOR="#FF0000"]Array(1, 2, 3, 4, 5)[/COLOR][/B])
vArray2 = Application.Index(vArray, Evaluate("2*row(1:" & (lN \ 2) & ")"), [B][COLOR="#FF0000"]Array(1, 2, 3, 4, 5)[/COLOR][/B])
...
If you wanted to save a small amount of typing, you could replace what I highlighted in red with this...

[{1,2,3,4,5}]
 
Upvote 0
kweaver The part I like most about your answer is that I can see the clear logical path you took to resolve the problem, and use just a few line as possible, exactly for the Chapter I am now about FOR NEXT loop. Great job.
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,205
Members
448,874
Latest member
Lancelots

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