+ Reply to Thread
Results 1 to 13 of 13

count 2 or more words in a cell

  1. #1
    Registered User
    Join Date
    02-17-2008
    Posts
    51

    count 2 or more words in a cell

    I am trying search & count 2 or more words within a cell using a worksheet function. See below example:

    The texts are in A1 (only)
    Entered by John at 02:19 PM on 02/13/2007
    Entered by Smith at 02:20 PM on 02/13/2007
    Entered by John at 02:30 PM on 02/13/2007
    Entered by John at 02:21 PM on 02/12/2007
    Entered by John at 02:22 PM on 02/11/2007

    The words I am looking for are "John" and "02/13/2007" and the count should be 2. I bolded what should be counted.Can you help me?

    Your help on this is appreciated.

    Statsman

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Here's one rather crude way.

    C1: =SEARCH("john*02/13/2007",A1)
    C2: =SEARCH("john*02/13/2007",$A$1,C1+1)
    Copy C2 down to C4
    D1: =COUNT(C1:C4)

    HTH

    rylo

  3. #3
    Registered User
    Join Date
    02-17-2008
    Posts
    51

    This is great!

    Thank you very much! You have been very helpful!

    Statsman

  4. #4
    Registered User
    Join Date
    02-17-2008
    Posts
    51

    counting 2 words in 1 or more cells

    I tested the function and it works great until I added another cell with the same search criteria but different data.

    The texts are in A1 & A2 (The words can be in a cell or range of cells).
    Cell A1
    Entered by John at 02:19 PM on 02/13/2007
    Entered by Smith at 02:20 PM on 02/13/2007
    Entered by John at 02:30 PM on 02/13/2007
    Entered by John at 02:21 PM on 02/12/2007
    Entered by John at 02:22 PM on 02/11/2007

    Cell A2
    Entered by John at 03:19 PM on 02/13/2007
    Entered by John at 04:19 PM on 02/13/2007
    Entered by Smith at 02:20 PM on 02/12/2007

    The count should be 4.

    Thanks again for your help!
    Statsman

  5. #5
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Statsman

    Now you are changing the rules.

    Only way I can think to do this is build your own function.

    Put the code below in a general module in the relevant workbook.
    Please Login or Register  to view this content.
    [NOTE: For those that are interested, I know there is no comments, and the choice of variable names is not particularly spectacular!!!!]

    Now in your spreadsheet, say B1, enter the formula
    Please Login or Register  to view this content.
    and it should return 4. Make it
    Please Login or Register  to view this content.
    and it will return 2.

    If you want to go down this track, then you will have to make the search string in the form I have used in the function.

    HTH

    rylo
    Last edited by rylo; 02-18-2008 at 10:29 PM.

  6. #6
    Registered User
    Join Date
    02-17-2008
    Posts
    51

    count 2 or more words in cell

    Thanks Rylo!

    I am not familiar with customized functions but I tried it and it works well. However, there is one thing that I noticed. When I changed john in cell A2 with a different name, it is giving me an error #value instead of 0. The count should be 2 in cell A1 and 0 in cell A2 (the total should be 2 combining both cells). I used the ISError function to fix this but it slows down the system.Is there a way how to correct this error (#value) with/without using the customized function?

    I appreciate again your help Rylo!
    Last edited by Statsman; 02-19-2008 at 12:01 PM.

  7. #7
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Statsman

    I've modified the function so that it doesn't need the iserror test.

    Please Login or Register  to view this content.
    However, I can't think of any way to do this without using a UDF. Or rather, the original way could be expanded so that there was a block for each cell, then add all the outputs. However, I don't see that as a viable alternative.

    From what I can see, you have a highly specific request. You may be able to speed the spreadsheet up by having a macro do the calculation on request, but that brings a completely different set of problems.

    Maybe someone else will come up with something if this isn't acceptable.

    rylo

  8. #8
    Registered User
    Join Date
    02-17-2008
    Posts
    51

    Count 2 or more words in a cell or range of cells

    Hi Rylo!

    I greatly appreciate for you helping me on this. Your code is acceptable and it actually works great.

    However, the more tests I make, there is a problem I encounter. The sample below should count only 1 but when I used the function, it counts 2. I bolded where is the additional count happening.

    Cell A1
    Entered by John at 02:19 PM on 02/13/2007
    Entered by John at 02:20 PM on 02/12/2007
    Entered by Smith at 02:30 PM on 02/13/2007
    Entered by Smith at 02:21 PM on 02/12/2007
    Entered by Andrea at 02:22 PM on 02/11/2007

    Is there a workaround on this?

    Thanks again Rylo for your help!

  9. #9
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Statsman

    You beat me too it. I thought of this problem last night and been mulling on a solution.

    Please Login or Register  to view this content.
    Input as
    Please Login or Register  to view this content.
    Let me know how it goes.

    rylo

  10. #10
    Registered User
    Join Date
    02-17-2008
    Posts
    51

    count 1 or more words in a cell

    it workkkkkkkssssss! You are amazing!I am really impressed despite of this complicated issues (for me anyway) you always have the workaround. Thanks again

  11. #11
    Registered User
    Join Date
    02-17-2008
    Posts
    51

    Unhappy count 1 or more words in a cell

    After testing the function with so may data, I noticed that it is taking a lot of time to execute and get the result. It often slows down the system and sometimes crashes.

    Here is an idea that i believe will resolve my dilemna:
    1. Remove all spaces
    2. Delete unwanted chars such as □
    3. Remove unwanted texts, in this case the time (I underlined and bolded these texts after word "at" and before "by")

    A1 (Before)
    Entered on 02/13/2008 at 02:00 PM by John:
    Entered on 02/13/2008 at 02:00 PM by Smith:
    Entered on 02/11/2008 at 02:00 PM by John:
    Entered on 02/1(/2008 at 02:00 PM by John:

    A1 (After)
    Enteredon02/13/2008atbyJohn:Enteredon02/13/2008atbySmith:Enteredon02/11/2008atbyJohn:Enteredon02/11/2008atbyJohn:

    I was able to create a formula for 1 & 2, but I got stuck in #3. Is there a function that removes the unwanted texts after and before a specific word?

    Pretending the criteria is located in K3 which is "Enteredon02/13/2008atbyJohn:", I can count the number of times K3 value occured in cell A1 using the function =SUM(LEN($a$1)-LEN(SUBSTITUTE($a$1,K3,"")))/LEN(K3).

    You help again is appreciated.

    Statsman

  12. #12
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    OK, try this

    1) A function
    Please Login or Register  to view this content.
    2) Data in A1
    3) E1: =myfunc(A1)
    4) J1: 02/13/2008John
    5) H1: =SUM(LEN($E$1)-LEN(SUBSTITUTE($E$1,J1,"")))/LEN(J1)

    See how that goes.

    rylo

  13. #13
    Registered User
    Join Date
    02-17-2008
    Posts
    51

    Thumbs up count 1 or more words in a cell

    This is excellent! the function's execution is faster. You are the greatest!

+ 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