Split text to multiple columns using VBA code

Kenor

Board Regular
Joined
Dec 8, 2020
Messages
116
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I would like to create VBA code for split a long text (items) into multiple columns as below picture (How to split the items) by using column button (Split Text)

I used the code below but it didn't work while I was using it.

Sub splitText()
'splits Text active cell using ALT+10 char as separator
Dim splitVals As Variant
Dim totalVals As Long

splitVals = Split(ActiveCell.Value, Chr(10))
totalVals = UBound(splitVals)

Range(Cells(ActiveCell.Row, ActiveCell.Column + 1), Cells(ActiveCell.Row, ActiveCell.Column + 1 + totalVals)).Value = splitVals
End Sub


My target is as per picture of Result after split.

Anyone can help me.

Thanks
 

Attachments

  • How to split the items.PNG
    How to split the items.PNG
    38.8 KB · Views: 496
  • Result after split.PNG
    Result after split.PNG
    37.2 KB · Views: 490

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Your Separator is chr(32), and you should insert the splitting code in a loop. May be this:
VBA Code:
Sub RSplit()
    For I = 2 To Cells(Rows.Count, 3).End(xlUp).Row
        mysplit = Split(Cells(I, 3).Value & " #", " ", , vbTextCompare)
        Cells(I, 3).Resize(1, UBound(mysplit)).Value = mysplit
    Next I
End Sub
Try...
 
Upvote 0
Solution
Your Separator is chr(32), and you should insert the splitting code in a loop. May be this:
VBA Code:
Sub RSplit()
    For I = 2 To Cells(Rows.Count, 3).End(xlUp).Row
        mysplit = Split(Cells(I, 3).Value & " #", " ", , vbTextCompare)
        Cells(I, 3).Resize(1, UBound(mysplit)).Value = mysplit
    Next I
End Sub
Try...

Thanks a lot...its works!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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