Find out how many times a value is present in a cell [formulas]

Posted on July 19th, 2016 in Excel Howtos , Learn Excel - 20 comments

Here is an interesting problem to start your day.

Let’s say you work as DNA sequencing engineer at The Enterprise. And you just unlocked the sequence that is responsible for all male problems. The early onset of baldness. The sequence code is AAAA. And you want to find out how many times this sequence is found in a sample of DNA strings, in the range B6:B19. Essentially you want this:

count-occurances-using-formula

So how do you write the formula?

Counting occurrences using Excel formulas

We can use LEN() and SUBSTITUTE() formulas to solve this problem.

Let’s say your target to find is in $C$3 and the full sequence is in B6. We can use below formula to find how many times C3 is present in B6.

=(LEN(B6) - LEN(SUBSTITUTE(B6,$C$3,""))/LEN($C$3)

How does this formula work?

Let’s go inside out (said male baldness to the head):

SUBSTITUTE(B6,$C$3,””): This replaces all occurrences of C3 in B6 with empty string.

LEN(SUBSTITUTE(…)): This will return the length of new text after replacing all C3s in B6.

LEN(B6): this simply gives us the length of original text

LEN(B6)-LEN(SUBSTITUTE(…)): Will give us the count of total letters replaced.

(LEN(..)-LEN(..))/LEN($C$3):  Tells us how many times C3 is present in B6.

So there you go.

Download Example Workbook:

Click here to download the example workbook. Play with the formulas to learn more.

A challenge for you – Find total occurrence count with single formula:

Let’s say you want to find out how many times $C$3 is present in a range – B6:B19 with one formula. How would you write it?

Please post your answers in comments section.

Related formulas:

Check out below examples to learn more.

PS: Thanks to Simran who emailed me this question.

PPS: The writer is not balding. He still sports plenty of pointy hair 🙂

Written by Chandoo
Tags: , , , , , ,
Home: Chandoo.org Main Page
? Doubt: Ask an Excel Question

20 Responses to “Find out how many times a value is present in a cell [formulas]”

  1. Colin says:

    Interesting post - thanks. Is there a way to use conditional formatting for the range B6:B19, not to highlight the cell, but to only highlight what is in cell C3?

    • Pavel says:

      I thing, there is no way to use conditional formatting. Tray VBA:
      Sub Makro2()
      Dim start As Long
      Dim sText As String
      Dim rCell As Range
      Dim rRng As Range
      Set rRng = Range("B6:B19")
      sText = Range("C3").Value
      For Each rCell In rRng.Cells
      For start = 1 To Len(rCell) - 3
      If Mid(rCell.Value, start, 4) = sText Then
      With rCell.Characters(start:=start, Length:=4).Font
      .FontStyle = "bold"
      .Color = -16776961
      End With
      End If
      Next start
      Next rCell
      End Sub

      • Colin says:

        Hi Pavel, thanks for the code, appreciate it. I was hoping there would be a way of doing it through the conditional formatting, but suspected it could not be done.

  2. Pavel says:

    Array formula
    ={Sum((LEN(B6:B19) - LEN(SUBSTITUTE(B6:B19,$C$3,""))/LEN($C$3)}

  3. Juanma says:

    To find how many times $C$3 is present in a range – B6:B19 with one formula, I am thinking an array formula would be the answer, something like this:

    {=SUM(LEN($B$6:$B$19)-LEN(SUBSTITUTE($B$6:$B$19,$C$3,"")))/LEN($C$3)}

  4. Jay says:

    But what if you're searching for "AAAA" and the cell that you're searching includes the string "AAAAA" (one additional "A")? The equation as presented would only count "AAAAA" as one case of "AAAA" rather than two. Aren't there two four-letter "A" strings in a five-letter string of "A's"? There's the first four "A's" (characters 1-4) and the last four "A's" (characters 2-5). Is there a way to do this?

    • Jay says:

      While my first comment awaits moderation, I'll toss in this observation as well... I just realized that if the cell being analyzed is "AAAA" and you're looking for a text string of "AA", that the solution as presented will give you a count of 2. But if the cell being analyzed is "AAAAA", the solution will also give you 2. Add one more "A" to the cell being analyzed, making it "AAAAAA" and the result will be 3. On the other hand, the way I'd count those different cases would give me results of 3, 4, and 5, respectively. Is there anyway to address this?

  5. Haz says:

    Let's say you have the sequence AAAAAA, and you want to count it as 3 different strings instead of 1

    =SUMPRODUCT((MID(B6,ROW(OFFSET($A$1,,,LEN(B6)-LEN($C$3)+1)),LEN($C$3))=$C$3)+0)

  6. Modeste says:

    Hi folks...

    =SOMMEPROD((NBCAR(B6:B19)-NBCAR(SUBSTITUE(B6:B19;$C$3;"")))/NBCAR($C$3))

    =SUMPRODUCT((LEN(B6:B19)-LEN(SUBSTITUE(B6:B19,$C$3,"")))/LEN($C$3))

  7. David says:

    =(SUM(LEN(B6:B19))-SUM(LEN(SUBSTITUTE(B6:B19,C3,""))))/LEN(C3)
    CSE for array formula

    Doh. Pavel and Juanma beat me to it.

  8. Raj says:

    Total Occurances in one formula
    =(LEN(CONCATENATE(B6,B7,B8,B9,B10,B11,B12,B13,B14,B15,B16,B17,B18,B19))-LEN(SUBSTITUTE(CONCATENATE(B6,B7,B8,B9,B10,B11,B12,B13,B14,B15,B16,B17,B18,B19),$C$3,"")))/LEN($C$3)

    • Pavel says:

      Raj your result could be different from the other, but I don’t know, which is wrong. Let says in cell B6 is “…TACAA” and in cell B7 “AACHGD….”, so your solution find the string “AAAA” divided into this two cells but the other solutions, count with me, don’t.

  9. Rudra M Sharma says:

    I just modified original formula to:

    =SUMPRODUCT((LEN(B6:B19)- LEN(SUBSTITUTE(B6:B19,$C$3,"")))/LEN($C$3))

  10. Rob says:

    Thanks for that.

    Had already created a file for my son, who is doing a biology degree, that shows the location of the value you are searching for. Uses the FIND command & then repeats to show all occurrences.

    Useful addition to that to give a quick summary of the count.

    Thanks.

  11. Xiq says:

    =SUMPRODUCT(LEN(B5:B18)-LEN(SUBSTITUTE(B5:B18,$C$2,"")))/LEN(C2)

  12. PINOY-exceler says:

    =SUMPRODUCT(--(MID(A4,ROW(INDIRECT("1:"&LEN(A4))),LEN(B1))=B1))

Leave a Reply