Forum Discussion
Formula to split cell values to multiple cells based on condition
- Dec 14, 2018
You should just be able to select the whole range you want to split and then run it. I tried it with multiple cells selected when I wrote it.
Or just use this, change Set SplitRange = Range("A1:A1000") as appropriate to the range you want to split the strings from.
Sub SplitString()
Dim Cell As Range
Dim i As Integer
Dim Position As Integer
Dim Search As String
Dim Start As Integer
Dim SplitRange As Range
Search = "A0"
Set SplitRange = Range("A1:A1000")
For Each Cell In SplitRange
i = Cell.Column + 1
Start = 1
Do
Position = InStr(Start, Cell.Text, Search, vbTextCompare)
If Position > 0 Then
Start = Position + 1
Cells((Cell.Row), i).Value = Mid(Cell.Text, Position, 6)
i = i + 1
End If
Loop While Position > 0
Next Cell
End Sub
Are the strings you want to pull out always going to be 6 characters? If not what the characters do we want to exclude? Just "(" , ")" , "/" and "*" ?
If it is always 6 characters then here is VBA solution based on selecting the range containing the cells to be split if that works for you. If it is not always 6 characters let me know what the seperator characters need to be and I will rejig this.
Sub SplitString()
Dim Cell As Range
Dim i As Integer
Dim Position As Integer
Dim Search As String
Dim Start As Integer
Search = "A0"
For Each Cell In Selection
i = Cell.Column + 1
Start = 1
Do
Position = InStr(Start, Cell.Text, Search, vbTextCompare)
If Position > 0 Then
Start = Position + 1
Cells((Cell.Row), i).Value = Mid(Cell.Text, Position, 6)
i = i + 1
End If
Loop While Position > 0
Next Cell
End Sub
Thank you and it is working for the first cell. How the code can automatically check the whole column and split?
Much appreciated in advance.
Regards,
Senthil
- JWR1138Dec 14, 2018Iron Contributor
You should just be able to select the whole range you want to split and then run it. I tried it with multiple cells selected when I wrote it.
Or just use this, change Set SplitRange = Range("A1:A1000") as appropriate to the range you want to split the strings from.
Sub SplitString()
Dim Cell As Range
Dim i As Integer
Dim Position As Integer
Dim Search As String
Dim Start As Integer
Dim SplitRange As Range
Search = "A0"
Set SplitRange = Range("A1:A1000")
For Each Cell In SplitRange
i = Cell.Column + 1
Start = 1
Do
Position = InStr(Start, Cell.Text, Search, vbTextCompare)
If Position > 0 Then
Start = Position + 1
Cells((Cell.Row), i).Value = Mid(Cell.Text, Position, 6)
i = i + 1
End If
Loop While Position > 0
Next Cell
End Sub