Reputation: 335
So I have a list of combinations as mentioned below which I needed to be split with an alphabet and a number following it.
I want it to be split as below using MS Excel; Text to Columns Delimit function or any other efficient function-
I tried using Fixed Width which doesn't work if there are 2 or more digits numeric value after the alphabetical character. Is there a way to do this as I have to repeat this for a 13000 combinations?
Upvotes: 0
Views: 169
Reputation: 1271
I found a way but is going to be resource intensive.
First have in a sheet all your strings in a column, and make sure you have columns free right to that column.
Then make a new, empty, first row, and put in the letters R, C, P, D, O, S, since you said they are always in that order. These are the recognition letters, needed for the formula.
Now under each letter, right of each string, put in the formula
=C$1&MAX(IFERROR(VALUE(MID($A2,SEARCH(C$1,$A2)+1,2)),0),VALUE(MID($A2,SEARCH(C$1,$A2)+1,1)),0)
This works only if the numbers are no longer then 2 digits, for three it would be
=C$1&MAX(IFERROR(VALUE(MID($A2,SEARCH(C$1,$A2)+1,3)),0),IFERROR(VALUE(MID($A2,SEARCH(C$1,$A2)+1,2)),0),VALUE(MID($A2,SEARCH(C$1,$A2)+1,1)),0)
For four, keep on adding an IFERROR() with the )),2) )),3) increasing like above.
Once all the formulas have evaluated, you can copy and paste as values if you want to get rid of the formulas.
Example sheet (Dutch Excel):
Upvotes: 1
Reputation: 152585
Since the Text are constant, create a header row of the constants and with your values starting in A1, put this formula in B2 and copy over and down:
=MID($A2,FIND(B$1,$A2),IF(ISNUMBER(--MID($A2,FIND(B$1,$A2)+2,1)),3,2))
Upvotes: 3
Reputation:
Insert a delimiter character where ever the pattern is found then split the modified string into new columns.
Option Explicit
Sub makeSplit()
Dim i As Long, j As Long, str As String, arr As Variant
'set the worksheet
With Worksheets("sheet5")
'step throughj the cells in column A
For i = .Cells(.Rows.Count, "A").End(xlUp).Row To 2 Step -1
'put the cell value into a variable
str = .Cells(i, "A").Value2
'step through the characters in the sting backwards
For j = Len(str) - 1 To 3 Step -1
'is the current character alphabetic and the previous character a digit?
If Not IsNumeric(Mid(str, j, 1)) And IsNumeric(Mid(str, j - 1, 1)) Then
'insert a space
str = Left(str, j - 1) & Space(1) & Mid(str, j)
End If
Next j
'split the string using the space as delimiter
.Cells(i, "A").Resize(1, UBound(Split(str, Space(1))) + 1) = Split(str, Space(1))
Next i
End With
End Sub
Upvotes: 1