Function GetWordsOccurrences(ByVal Text As String) As Variant() Dim re As New RegExp Dim ma As Match Dim col As New Collection Dim ndx As Long re.Pattern = "\b\w+\b" re.Global = True ReDim res(1, 100) As Variant On Error Resume Next For Each ma In re.Execute(Text) ndx = col.Count + 1 col.Add ndx, ma.Value If Err = 0 Then If ndx > UBound(res, 2) Then ReDim Preserve res(1, ndx + 99) As Variant End If res(0, ndx) = ma.Value res(1, ndx) = 1 Else Err.Clear ndx = col(ma.Value) res(1, ndx) = res(1, ndx) + 1 End If Next If col.Count Then ReDim Preserve res(1, col.Count) As Variant GetWordsOccurrences = res End If
End Function |