Marc L
Excel Ninja
Hi cacos !
I saw another way to count words but 'cause I was thinking counting comments is more logical,
I didn't explore it …
So in that particular case of many rows with same few comments, this is an evolution of my speedy function !
In fact a 2 in 1 function counting either comments or words of your sample file in less than 0.2 second ‼
(on a test computer just after boot time with no data in cache !)
Try procedure CountWords :
Code:
Sub DictionaryHashComments(Optional Words As Boolean)
Dim Dict As New Dictionary
' Set Dict = CreateObject("Scripting.Dictionary")
Sheet2.UsedRange.Clear
ReDim CT&(0)
AR = Sheet1.Range("I2", Sheet1.[I2].End(xlDown)).Value
For R& = 1 To UBound(AR)
HV = Dict.HashVal(AR(R, 1))
If Not Dict.Exists(HV) Then
ReDim Preserve CT(UBound(CT) + 1)
CT(UBound(CT)) = R
End If
Dict.Item(HV) = Dict.Item(HV) + 1
Next
Application.ScreenUpdating = False
Sheet2.Activate
If Words Then
CC = Dict.Items
Dict.RemoveAll
ERASECHAR = [{",",".",";"}]
KEEPSPACE = [{"—","'s "}]
For R = 1 To UBound(CT)
T$ = LCase$(Application.Clean(AR(CT(R), 1)))
For Each C In ERASECHAR: T = Replace$(T, C, ""): Next
For Each C In KEEPSPACE: T = Replace$(T, C, " "): Next
For Each C In Split(T): Dict.Item(C) = Dict.Item(C) + CC(R - 1): Next
Next R
With [C2].Resize(Dict.Count, 2)
.Value = Application.Transpose(Array(Dict.Items, Dict.Keys))
.Sort [D2], xlAscending, Header:=xlNo, MatchCase:=False
End With
Else
[C2].Resize(Dict.Count).Value = Application.Transpose(Dict.Items)
For R = 1 To UBound(CT): Cells(R + 1, 4).Value = Application.Clean(AR(CT(R), 1)): Next
End If
Application.ScreenUpdating = True
Dict.RemoveAll
Set Dict = Nothing
End Sub
Sub CountComments()
DictionaryHashComments
End Sub
Sub CountWords()
DictionaryHashComments True
End Sub
And you can still CountComments !
Enjoy & Like it ‼