How to split string using VBA?

dmadhup

Board Regular
Joined
Feb 21, 2018
Messages
146
Office Version
  1. 365
For example, I have following string

SPLICE 3_CON 195_INTERLOCK_18_BROWN

I am trying to write VBA code to split it into five and put in different columns like:

SPLICE 3 CON 195 INTERLOCK 18 BROWN

<tbody>
</tbody>

<tbody>
</tbody>
Any help is appreciated.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Something like
Code:
Range("B2").Resize(, UBound(Split(Range("A2"), "_")) + 1).Value = Split(Range("A2"), "_")
 
Upvote 0
This macro assumes you have headers in row 1 and that you strings are in column A.
Code:
Sub SplitString()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim lColumn As Long
    Dim rng As Range
    Dim vString As Variant
    Dim i As Long
    For Each rng In Range("A2:A" & LastRow)
        vString = Split(rng, "_")
        For i = LBound(vString) To UBound(vString)
            lColumn = ActiveSheet.Cells(rng.Row, Columns.Count).End(xlToLeft).Column + 1
            Cells(rng.Row, lColumn) = vString(i)
            lColumn = lColumn + 1
        Next i
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,178
Members
449,071
Latest member
cdnMech

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