+ Reply to Thread
Results 1 to 3 of 3

Count words in a cell & output number of occurences of each word in adjecent cell.

Hybrid View

  1. #1
    Registered User
    Join Date
    05-02-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    21

    Count words in a cell & output number of occurences of each word in adjecent cell.

    Hi Experts,

    I am looking for a code which can count words in a cell and return output as number of occurences of each word in the adjecent cell.

    Eg:
    Cell A1 contains : a, a, b, c, d, b

    After executing the code I need output in B1 as:
    a(2), b(2), c(1), d(1)

    I have found code on internet & have modified the same to my requierment. With below code I am getting output in different rows instead of getting the same on one single cell.

    
    Sub CountString()
        Dim vArray As Variant
        Dim lLoop As Long
         
        With CreateObject("Scripting.Dictionary")
                vArray = Split(ActiveCell.Value, ", ")
                For lLoop = LBound(vArray) To UBound(vArray)
                    If Not .exists(vArray(lLoop)) Then
                        .Add vArray(lLoop), 1
                    Else
                        .Item(vArray(lLoop)) = .Item(vArray(lLoop)) + 1
                    End If
                Next lLoop
            Range("B1").Resize(.Count).Value = Application.Transpose(.keys)
            Range("C1").Resize(.Count).Value = Application.Transpose(.items)
        End With
    End Sub
    Please help.

    Regards
    kprasad

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Count words in a cell & output number of occurences of each word in adjecent cell.

    Sub test()
        Dim e, txt As String
        If ActiveCell.Value = "" Then Exit Sub
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            For Each e In Split(ActiveCell.Value, ",")
                .Item(Trim$(e)) = .Item(Trim$(e)) + 1
            Next
            For Each e In .keys
                txt = txt & ", " & e & "(" & .Item(e) & ")"
            Next
        End With
        ActiveCell(, 2).Value = Mid$(txt, 3)
    End Sub

  3. #3
    Registered User
    Join Date
    05-02-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: Count words in a cell & output number of occurences of each word in adjecent cell.

    Wow!!! Thanks jindon.... It works really well.

    Thanks Again,

    kprasad

+ 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. Replies: 6
    Last Post: 10-18-2016, 08:15 AM
  2. Replies: 4
    Last Post: 10-20-2010, 10:28 AM
  3. finding a set word from a number of words in a cell
    By Duckie in forum Excel General
    Replies: 9
    Last Post: 02-07-2010, 06:05 AM
  4. Replies: 2
    Last Post: 01-07-2010, 03:21 PM
  5. Replies: 6
    Last Post: 11-11-2009, 09:00 AM
  6. Replies: 4
    Last Post: 04-07-2008, 04:38 PM
  7. [SOLVED] How do I count the number of words in a cell?
    By Phil in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-02-2006, 02:20 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