Macro to split text to columns by maximum number of words in a cell

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,210
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
bit of a Saturday morning challenge for you
In Column K I have a list of cells that contain search engine sentences.
So Row 1 is the Header "Search Words"
and it can be any amount of rows.

So What i'm looking for is a macro that can split up my words into their own columns.
now the problem is I don't know how many words this is as there can be just 1 or several,
So heres what I'd like please help if you can.

A Macro that when run seperates column K using the text to columns method spliting at spaces,
Then I simply need it to tell me how many columns where used and what the last column is
for example msgbox" Your data was split acrros 21 columns with the last column being AA!" for example

please help if you can


Thanks
Tony
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
maybe close, but the message needs tweaking. Let me know (no point in fixing the message if I'm off base).
VBA Code:
Sub splitIntoColumns()
Dim rng As Range
Dim Lrow As Long, i As Integer, Lcol As Long
Dim msg As String

Lrow = Cells(Rows.count, "K").End(xlUp).Row
msg = "Your data was spread across "

For i = 2 To Lrow
    With Sheets("001")
        Set rng = .Range("K" & i)
        rng.TextToColumns Destination:=rng, DataType:=xlDelimited, _
        ConsecutiveDelimiter:=True, _
        Space:=True
        Lcol = .Cells(i, .Columns.count).End(xlToLeft).Column
        msg = msg & Lcol - 10 & "." & vbCrLf
        msg = msg & "Last column is " & .Cells(i).Address
        MsgBox msg
    End With
Next

End Sub
 
Upvote 0
Just noticed that new last column part of the message needs fixing as well. As mentioned, I'll wait to re-post code. One reason is that I suspect you'll change your mind about a message box prompt for each row if there's hundreds of rows being processed.
 
Upvote 0
Perhaps this is what you want?

VBA Code:
Sub SplitWords()
  Dim lc As Long
 
  With Range("K2", Range("K" & Rows.Count).End(xlUp))
    .TextToColumns Destination:=.Offset(, 1), DataType:=xlDelimited, Space:=True, Other:=False
    lc = .EntireRow.Find(What:="*", SearchOrder:=xlByColumns, Searchdirection:=xlPrevious).Column
    MsgBox "Columns used: " & lc - .Column & vbLf & "Last column: " & Split(Cells(1, lc).Address, "$")(1)
  End With
End Sub

My sample data in col K, TTC to the right of that and MsgBox as shown.

1671332794925-png.81137
 

Attachments

  • 1671332794925.png
    1671332794925.png
    9.9 KB · Views: 44
Upvote 0
Solution
Hi everyone,
Firstly, Thank you to Micron, some very nice ideas there and I apreciate you help,
Peter_SSs, this work first time and was exactly what I needed thank you. :-)
Tony
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,114
Members
452,302
Latest member
TaMere

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