+ Reply to Thread
Results 1 to 4 of 4

Count specific values in a cell

Hybrid View

  1. #1
    Registered User
    Join Date
    01-06-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    8

    Count specific values in a cell

    How would I count how many underscores ( _ ) there are in a cell. For example,
    Cell contains:
    word_word_word_word_word
    Result would be 4.

    Is there a formula for this?

    Thanks

  2. #2
    Registered User
    Join Date
    01-06-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Count specific values in a cell

    Got it.

    Found a formula that counts words.
    =IF(LEN(TRIM(A1))=0,0,LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1)

    I replaced the space " " with my underscore "_"

  3. #3
    Registered User
    Join Date
    09-26-2009
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Count specific values in a cell

    The +1 at the end would give you the number of words and not the number of _'s... If you are looking for the number of _'s remove the +1 from the end of the formula.

  4. #4
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,717

    Re: Count specific values in a cell

    Are you trying to count underscores specifically or words given that every two words are separated by an undescore?

    That formula works specifically for spaces and won't always count accurately for underscores (because TRIM removes excess spaces but not excess underscores)

    If you just want to count underscores specifically you only need this

    =LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))

    so if you really want to count words and you are sure there's only a single underscore between each word, then the word count would be

    =LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))+(A1<>"")

    or if it's possible you might have excess underscores (e.g. 2 between words or some at the start or end) then count the words with this

    =LEN(TRIM(SUBSTITUTE(A1,"_"," ")))-LEN(SUBSTITUTE(TRIM(SUBSTITUTE(A1,"_"," "))," ",""))+(LEN(SUBSTITUTE(A1,"_",""))>0)

+ 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