+ Reply to Thread
Results 1 to 7 of 7

Counting words in multiple sentences

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Counting words in multiple sentences

    Hi all,
    Please look at the 2 files attached - "lice" and "lice_desired_results".
    "lice" has a single column with sentences (simple text).
    I need a macro that will count each and every word in all sentences and write in columns B and C the total amount of times that each word appear in all sentences (please see the file "lice_desired_results" to see what I mean.)

    Please note:
    1. I need a macro based solution, NOT A FORMULA.
    2. the solution should not be case sensitive, Lice=lice and so on....

    Thanks,
    Sami
    Attached Files Attached Files
    Last edited by sami770; 11-16-2014 at 09:34 AM.

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Counting words in multiple sentences

    Try this for results in columns "B" & " C".
    Sub MG16Nov10
    Dim Rng As Range, Dn As Range, n As Long
    Dim Sp As Variant
    Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
        With CreateObject("scripting.dictionary")
        .CompareMode = vbTextCompare
    For Each Dn In Rng
        Sp = Split(Dn.Value, " ")
            For n = 0 To UBound(Sp)
                If Not .Exists(Sp(n)) Then
                    .Add Sp(n), 1
                Else
                    .Item(Sp(n)) = .Item(Sp(n)) + 1
                End If
            Next n
    Next
    Range("B2").Resize(.Count, 2) = Application.Transpose(Array(.Keys, .items))
    End With
    End Sub
    Regards Mick

  3. #3
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: Counting words in multiple sentences

    Thanks for the nice macro, Mick.
    It does the job!
    When I looked at the results I realized that it will be a lot more useful if:

    1. The macro will filter out the question mark sign (?)
    at the moment LICE and LICE? are counted as two different words, I need to remove the ? sign from the calculations and count the total amount of instances of each word into a single result.
    2. there are 29 words that needs to be excluded from the results, see text file attached.

    Thanks again,
    Sami


    Quote Originally Posted by MickG View Post
    Try this for results in columns "B" & " C".
    Sub MG16Nov10
    Dim Rng As Range, Dn As Range, n As Long
    Dim Sp As Variant
    Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
        With CreateObject("scripting.dictionary")
        .CompareMode = vbTextCompare
    For Each Dn In Rng
        Sp = Split(Dn.Value, " ")
            For n = 0 To UBound(Sp)
                If Not .Exists(Sp(n)) Then
                    .Add Sp(n), 1
                Else
                    .Item(Sp(n)) = .Item(Sp(n)) + 1
                End If
            Next n
    Next
    Range("B2").Resize(.Count, 2) = Application.Transpose(Array(.Keys, .items))
    End With
    End Sub
    Regards Mick
    Attached Files Attached Files

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Counting words in multiple sentences

    Try this:-
    Nb:- Add to "Excluded" words list as required.
    Sub MG16Nov11
    Dim Rng As Range, Dn As Range, n As Long
    Dim Sp As Variant, nn As Integer
    Dim Wds As Variant, Dic As Object, Qn As String
    
    Wds = Array("which", "who", "arent", "why", "isn't", "how", "what", "when", "where", "which", "who", _
    "whom", "whose", "wouldn't", "would", "won't", "will", "shouldn't", "should", "couldn't", "must", "might", _
    "could", "may", "can't", "didn't", "can", "Do", "Does")
    
    Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
        With CreateObject("scripting.dictionary")
          .CompareMode = vbTextCompare
            For n = 0 To UBound(Wds): .Item(Wds(n)) = Empty: Next n
                Set Dic = CreateObject("scripting.dictionary")
                    Dic.CompareMode = vbTextCompare
    
    For Each Dn In Rng
        Sp = Split(Dn.Value, " ")
            For n = 0 To UBound(Sp)
                Qn = IIf(Right(Sp(n), 1) = "?", Left(Sp(n), Len(Sp(n)) - 1), Sp(n))
                If Not .exists(Qn) Then
                    If Not Dic.exists(Qn) Then
                        Dic.Add Qn, 1
                    Else
                        Dic.Item(Qn) = Dic.Item(Qn) + 1
                    End If
                End If
            Next n
    Next
    Range("B2").Resize(.Count, 2) = Application.Transpose(Array(Dic.Keys, Dic.items))
    End With
    End Sub
    Regards Mick
    Last edited by MickG; 11-16-2014 at 11:33 AM.

  5. #5
    Forum Contributor
    Join Date
    05-10-2011
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    145

    Re: Counting words in multiple sentences

    BINGO!
    Thanks a lot, Mick!
    Sami

    Quote Originally Posted by MickG View Post
    Try this:-
    Nb:- Add to "Excluded" words list as required.
    Sub MG16Nov11
    Dim Rng As Range, Dn As Range, n As Long
    Dim Sp As Variant, nn As Integer
    Dim Wds As Variant, Dic As Object, Qn As String
    
    Wds = Array("which", "who", "arent", "why", "isn't", "how", "what", "when", "where", "which", "who", _
    "whom", "whose", "wouldn't", "would", "won't", "will", "shouldn't", "should", "couldn't", "must", "might", _
    "could", "may", "can't", "didn't", "can", "Do", "Does")
    
    Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
        With CreateObject("scripting.dictionary")
          .CompareMode = vbTextCompare
            For n = 0 To UBound(Wds): .Item(Wds(n)) = Empty: Next n
                Set Dic = CreateObject("scripting.dictionary")
                    Dic.CompareMode = vbTextCompare
    
    For Each Dn In Rng
        Sp = Split(Dn.Value, " ")
            For n = 0 To UBound(Sp)
                Qn = IIf(Right(Sp(n), 1) = "?", Left(Sp(n), Len(Sp(n)) - 1), Sp(n))
                If Not .exists(Qn) Then
                    If Not Dic.exists(Qn) Then
                        Dic.Add Qn, 1
                    Else
                        Dic.Item(Qn) = Dic.Item(Qn) + 1
                    End If
                End If
            Next n
    Next
    Range("B2").Resize(.Count, 2) = Application.Transpose(Array(Dic.Keys, Dic.items))
    End With
    End Sub
    Regards Mick

  6. #6
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Counting words in multiple sentences

    You're welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Search box in Excel to search key words in sentences in multiple worksheets
    By fernandoii676 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-05-2012, 01:42 PM
  2. Replies: 2
    Last Post: 10-19-2012, 11:11 AM
  3. List out words from sentences
    By ap_naveen in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-21-2010, 06:58 AM
  4. Split sentences into words
    By sparx in forum Excel General
    Replies: 5
    Last Post: 02-22-2006, 03:51 PM
  5. [SOLVED] counting specific words:unique sentences
    By jen in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-24-2005, 08:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1