+ Reply to Thread
Results 1 to 14 of 14

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

  1. #1
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    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.

    Please Login or Register  to view this content.
    ==========
    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 Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    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.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    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 Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    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)

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    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 Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    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?
    Please Login or Register  to view this content.

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    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.

    Please Login or Register  to view this content.

  8. #8
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    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 & ")")

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    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 Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    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.

  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 Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

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

    Teylyn, Bigdaddy is an alias for the guy who sent the original email.

  14. #14
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

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

    just a thought
    Please Login or Register  to view this content.

+ Reply to Thread

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.6.0 RC 1