+ Reply to Thread
Results 1 to 14 of 14
  1. #1
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Finding the frequency of a string in other string(s)

    Received from an email:
    Quote Originally Posted by vkcham van
    I'm looking for a formula that will count specific word in a cell.

    Let say cell a1 has "WLLWWLLLLLLWWLLLWWWLLLWWLLWWLWLWLWLLLWWW"
    I want to know how many WWLL are in this cell, which is 4.

    Thank you - sam
    I wrote this custom function to provide the answer. It is used like so:

    =COUNTSTRING(A1, "WWLL")
    =COUNTSTRING(A1:A10, "WWLL")
    =COUNTSTRING(A1, B1)
    (B1 holds the text string WWLL)

    The first parameter is a cell or range of cells. The second parameter is the string to search for as a single cell reference or text string.

    Code:
    Function COUNTSTRING(RNG As Range, MyStr As String) As Long
    'JBeaucaire  (12/8/2009)
    'Count the frequency of a string in other strings
    Dim cell As Range, MyCnt As Long, i As Long
    If MyStr = "" Then GoTo ErrorExit
    
        For Each cell In RNG
            For i = 1 To (Len(cell) - Len(MyStr) + 1)
                If Mid(cell, i, Len(MyStr)) = MyStr Then MyCnt = MyCnt + 1
            Next i
        Next cell
    
    COUNTSTRING = MyCnt
    Exit Function
    
    ErrorExit:
        COUNTSTRING = 0
    End Function
    ==========
    How to install the User Defined Function:

    1. Open up your workbook
    2. Get into VB Editor (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste in your code (given above)
    5. Get out of VBA (Press Alt+Q)
    6. Save your sheet

    The function is installed and ready to use in cell as shown above.
    Last edited by JBeaucaire; 12-08-2009 at 09:09 PM. Reason: Fixed the "For i =" line of code
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  2. #2
    Forum Moderator pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,141

    Re: Finding the frequency of a string in other string(s)

    hi JB

    excelent

    we need a "Free code Section"
    Last edited by pike; 12-08-2009 at 02:00 AM.
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    JBeaucaires Excel Files

    VBA for smarties - snb

  3. #3
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Finding the frequency of a string in other string(s)

    Moved to Tips & Tutorials.

    Worth adding perhaps that the same could be achieved natively with:

    =(LEN(B1)-LEN(SUBSTITUTE(A1,B1,""))/LEN(B1)

    where A1 is original string and B1 string of interest

    And for range

    =SUMPRODUCT((LEN(A1:A10)-LEN(SUBSTITUTE(A1:A10,B1,""))))/LEN(B1)

    (the latter could be applied via a single Evaluate call in UDF terms based on address of range specified in the args)

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Finding the frequency of a string in other string(s)

    I had to move the parens to get Don's to work, but very slick indeed! Better than a UDF! Which is why I posted, wondered what the gang would come up with...

    =(LEN(A1)-LEN(SUBSTITUTE(A1,B1,"")))/LEN(B1)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Finding the frequency of a string in other string(s)

    thanks JB - yes I missed the closing parenthesis (never code on the fly!)

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Finding the frequency of a string in other string(s)

    I tried to get Don's formula to work inside the UDF and no dice. Anybody spot the error?
    Code:
    Function COUNTSTRING2(RNG As Range, MyStr As String) As Long
    'Count the frequency of a string in other strings
    
        COUNTSTRING2 = Evaluate("=SUMPRODUCT((LEN(" & RNG & ")-LEN(SUBSTITUTE(" & RNG & "," & MyStr & ",""""))))/LEN(" & MyStr & ")")
    
    End Function
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  7. #7
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Finding the frequency of a string in other string(s)

    You need to encase your string variable within quotations and also when using Evaluate you should use .Address

    One other point, when using Evaluate widely I'd say it's generally best to qualify the object against which you wish for it to be applied.

    Code:
    COUNTSTRING2 = RNG.Parent.Evaluate("SUMPRODUCT((LEN(" & RNG.Address & ")-LEN(SUBSTITUTE(" & RNG.Address & ",""" & MyStr & """,""""))))/LEN(""" & MyStr & """)")

  8. #8
    Forum Moderator teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    2003 & 2010
    Posts
    10,042

    Re: Finding the frequency of a string in other string(s)

    why do you have that closing parens at the end? does MyStr open it?

    MyStr & ")")
    teylyn
    Microsoft MVP - Excel
    At Excelforum, you can say "Thank you!" by clicking the icon below the post.

    Avoid pie charts with more than two data points. Why? See here (pdf, 559 kb). The only acceptable pie chart is here.

  9. #9
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Finding the frequency of a string in other string(s)

    It is closing the LEN function

  10. #10
    Registered User
    Join Date
    01-23-2009
    Location
    maine us
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Finding the frequency of a string in other string(s)

    Originally Posted by vkcham van
    I'm looking for a formula that will count specific word in a cell.

    Let say cell a1 has "WLLWWLLLLLLWWLLLWWWLLLWWLLWWLWLWLWLLLWWW"
    I want to know how many WWLL are in this cell, which is 4.

    Thank you - sam

    I wrote this custom function to provide the answer. It is used like so:

    =COUNTSTRING(A1, "WWLL")
    =COUNTSTRING(A1:A10, "WWLL")
    =COUNTSTRING(A1, B1) (B1 holds the text string WWLL)

    I copy all the code. I'm trying to use the countstring formula but it doesn't work for me. . I get this "#NAME?" I have "WLLWWLLLLLLWWLLLWWWLLLWWLLWWLWLWLWLLLWWW" in cell A1. Any ideas why?

  11. #11
    Forum Moderator teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    2003 & 2010
    Posts
    10,042

    Re: Finding the frequency of a string in other string(s)

    Bigdaddy, please post questions in one of the question forums. This is not one. You can link to this thread, to provide the context.

    Please make sure that when you quote, you use quote tags like this [quote] the quoted text[/quote], so people can actually identify where your contribution starts.

    cheers
    Last edited by teylyn; 12-08-2009 at 07:11 PM.
    teylyn
    Microsoft MVP - Excel
    At Excelforum, you can say "Thank you!" by clicking the icon below the post.

    Avoid pie charts with more than two data points. Why? See here (pdf, 559 kb). The only acceptable pie chart is here.

  12. #12
    Registered User
    Join Date
    01-23-2009
    Location
    maine us
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Finding the frequency of a string in other string(s)

    I seem to have gotten it to work... I open a new worksheet and the fuction worked. Thank you very much.

  13. #13
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Finding the frequency of a string in other string(s)

    Teylyn, Bigdaddy is an alias for the guy who sent the original email.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  14. #14
    Forum Moderator pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,141

    Re: Finding the frequency of a string in other string(s)

    just a thought
    Code:
    Function jtest(Rng$, Mystr$) As Double
        Dim RegEx As Object, Matches As Object
        Set RegEx = CreateObject("vbscript.regexp")
        With RegEx
            .Global = True: .IgnoreCase = True: .Pattern = "(" & Rng & ")"
            Set Matches = .Execute(Mystr)
                   jtest = Matches.Count
           End With
    End Function
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    JBeaucaires Excel Files

    VBA for smarties - snb

Thread Information

Users Browsing this Thread

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

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.2.0