Split text to multiple columns using VBA code

Kenor

New Member
Joined
Dec 8, 2020
Messages
46
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: 13
  • Result after split.PNG
    Result after split.PNG
    37.2 KB · Views: 12

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Anthony47

Well-known Member
Joined
Mar 29, 2006
Messages
2,147
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...
 
Solution

Kenor

New Member
Joined
Dec 8, 2020
Messages
46
Office Version
  1. 2016
Platform
  1. Windows
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,130,083
Messages
5,639,977
Members
417,120
Latest member
Pavithra devi

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
Top