VBA to count number of words in cell

Godders199

Active Member
Joined
Mar 2, 2017
Messages
313
Office Version
  1. 2013
Hello, I am trying to find the vba to count the number ofwords in a cell, and have it run down to the last row.

Manually I have worked out the formula which is which iscurrently in column AB
=LEN(TRIM(P2))-LEN(SUBSTITUTE(P2," ",""))+1
I have been searching the internet, and found a code likethis, but playing about I can not get it to work

Sheets("submissions").Select
Dim dic As Object, cell As Range, temp As Variant
Dim i As Long
Set dic = CreateObject("scripting.dictionary")
With dic
For Each cell InRange("p1:p" & Cells(Rows.count, "A").End(xlUp).Row)
.RemoveAll
IfLen(cell.Value) > 0 Then
temp =Split(cell.Value, " ")
For i = 0To UBound(temp)
If Not.Exists(temp(i)) Then .Add temp(i), temp(i)
Next i
cell.Value= Join(.Keys, " ")
End If
Next cell
End With

Just wondered if anyone has any examples of code they could letme see and amend to fit my requirements.

Thanks

 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Do you simply want to get a count of the words in each cell?
 
Upvote 0
Try this code
Code:
Sub CountWords()


Dim TotWds As Long
Dim cel As Range


For Each cel In Range("p1:p" & Cells(Rows.Count, "A").End(xlUp).Row)
M = Split(cel, " ")
TotWds = TotWds + UBound(M) + 1
Next cel


End Sub
 
Upvote 0
If you want the total number of words in each cell try
Code:
Sub Godders199a()
   Dim Cl As Range
   
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      Cl.Offset(, 1) = UBound(Split(Cl)) + 1
   Next Cl
End Sub
If you want the total number of unique words per cell try
Code:
Sub Godders199b()
   Dim Cl As Range
   Dim Tmp As Variant
   Dim i As Long
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         Tmp = Split(Cl)
         For i = 0 To UBound(Tmp)
            .Item(Tmp(i)) = .Item(Tmp(i)) + 1
         Next i
         Cl.Offset(, 1) = .Count
         .RemoveAll
      Next Cl
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
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