Help with complex string sorting in Excel

Copper Contributor

Hope someone can help me.

So I have some data entries that come in at :

'Port 1 Magnitude Index:0'

'Port 1 Magnitude Index:1'

'Port 1 Magnitude Index:2'

'Port 1 Phase Index:0'

'Port 1 Phase Index:1'

'Port 1 Phase Index 2',

'Port 2 Magnitude Index:0'

'Port 2 Magnitude Index:1'

'Port 2 Magnitude Index:2'

'Port 2 Phase Index:0'

'Port 2 Phase Index:1'

'Port 2 Phase Index 2'

 

Is there a way to sort the data so I can get it formatted as:

'Port 1 Magnitude Index:0'

'Port 1 Phase Index:0'

'Port 2 Magnitude Index:0'

'Port 2 Phase Index:0'

'Port 1 Magnitude Index:1'

'Port 1 Phase Index:1'

'Port 2 Magnitude Index:1'

'Port 2 Phase Index:1'

'Port 1 Magnitude Index:2'

'Port 1 Phase Index:2'

'Port 2 Magnitude Index:2'

'Port 2 Phase Index:2'

 

In the case that I am given, the data loops through Port 1 Magnitude (at all index) then Port 1 Phase, then Port 2 Magnitude, and then Port 2 Phase. But I'm trying to sort the data by viewing all of 'Index:0' first and then 'Index:1' etc.

 

Can anyone help?

3 Replies

@LazyShadow 

Is this correct?

'Port 1 Phase Index 2',

without : before the 2, and with a comma at the end?

@LazyShadow 

Are there always 12 lines of data with the original data listed as in your example? This would mean that the data that contains "Index 0" always is in rows 1, 4, 7 and 10 (and so on with "Index 1" and "Index 2"). If so you can try the attached file. Click the button in cell F2 to start the macro. 

Sub index()

Dim i As Integer
Dim z As Integer
Dim w As Integer
Dim v As Integer

For i = 1 To 3
For z = 1 To 4

Cells(z + w, 4).Value = Cells(i + v, 1).Value
v = v + 3

Next z

v = 0
w = w + 4
Next i

End Sub