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

[TABLE="width: 366"]
<tbody>[TR]
[TD="width: 366"]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:

[TABLE="width: 366"]
<tbody>[TR]
[TD="width: 366"]SPLICE 3 CON 195 INTERLOCK 18 BROWN[/TD]
[/TR]
[TR]
[TD="width: 366"][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]
Any help is appreciated.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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