Avatar of Saqib Husain
Saqib Husain
Flag for Pakistan asked on

Splitting text to words

Can someone give me VBA code to go through a list of titles (in column C) and split them into words onto column A of a new sheet called words. If the sheet exists then first delete it.
VBAMicrosoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Subodh Tiwari (Neeraj)

8/22/2022 - Mon
David Johnson, CD

an example spreadsheet with what you have and what you want would be nice.
Saqib Husain

ASKER
David Johnson, CD

Text to columns will do the splitting for you
https://www.spreadsheetsmadeeasy.com/how-to-split-cells-in-excel/
Your help has saved me hundreds of hours of internet surfing.
fblack61
Saqib Husain

ASKER
Did you check out the Words sheet?
byundt

The unique words will be put in column A using the following:
Sub WordMaster()
Dim v As Variant, vSplit As Variant, vWords As Variant, vTitles As Variant
Dim dic As Object
Dim wsWords As Worksheet
Dim rgTitles As Range, rgWords As Range
Dim i As Long, j As Long, k As Long, nTitles As Long, nWords As Long

Set dic = CreateObject("Scripting.Dictionary")
With ActiveSheet
    Set rgTitles = Intersect(.UsedRange, .Columns("C"))
End With

On Error Resume Next
Set wsWords = Worksheets("Words")
If wsWords Is Nothing Then
    Set wsWords = Worksheets.Add(After:=ActiveSheet)
    wsWords.Name = "Words"
End If

wsWords.Columns("A").ClearContents

vTitles = rgTitles.Value
nTitles = UBound(vTitles)

For i = 1 To nTitles
    If vTitles(i, 1) <> "" Then
        vSplit = Split(vTitles(i, 1), " ")
        For Each v In vSplit
            If Not dic.exists(v) Then
                j = j + 1
                dic.Add v, j
            End If
        Next
    End If
Next
On Error GoTo 0

nWords = j
ReDim vWords(1 To nWords)
For j = 1 To nWords
    vWords(j) = dic.Keys()(j - 1)
Next

wsWords.Range("A1").Resize(nWords, 1).Value = Application.Transpose(vWords)
End Sub

Open in new window

Split-text-to-words.xlsm
Subodh Tiwari (Neeraj)

Hi Saquib,

You may alos try something like this....

Sub getWords()
Dim wsData      As Worksheet
Dim wsWords     As Worksheet
Dim LR          As Long
Dim i           As Long
Dim j           As Long
Dim x           As Variant
Dim arrWords    As Variant
Dim word        As Variant
Dim dict        As Object


Application.ScreenUpdating = False


Set wsData = Worksheets("Sheet1")


On Error Resume Next
Set wsWords = Worksheets("Words")
wsWords.Cells.Clear
On Error GoTo 0


If wsWords Is Nothing Then
    Set wsWords = Worksheets.Add(after:=wsData)
    wsWords.Name = "Words"
End If


LR = wsData.Cells(Rows.Count, "C").End(xlUp).Row
x = wsData.Range("C2:C" & LR).Value


Set dict = CreateObject("Scripting.Dictionary")
ReDim arrWords(1 To 1048576, 1 To 1)


For i = 1 To UBound(x, 1)
    If x(i, 1) <> "" Then
        For Each word In Split(x(i, 1), " ")
            If Not dict.exists(word) Then
                j = j + 1
                dict.Item(word) = ""
                arrWords(j, 1) = word
            End If
        Next word
    End If
Next i
wsWords.Range("A1").Resize(j, 1) = arrWords


Application.ScreenUpdating = True
End Sub

Open in new window


Split-text-to-words.xlsm

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Saqib Husain

ASKER
Thanks both.

Both work but are case sensitive, thus I am getting duplicates. Need to compare case insensitive.

It would be great if you could also add a second column giving the count of each word.
SOLUTION
Subodh Tiwari (Neeraj)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
byundt

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Saqib Husain

ASKER
Hi, both.

Thanks a lot. Both codes worked well as requested. Brad's code had an additional feature to handle the punctuations which was very helpful. I had to add parenthesis to it too. 
Subodh Tiwari (Neeraj)

You're welcome Saqib!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy