fbpx
Search
Close this search box.

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

Share

Facebook
Twitter
LinkedIn

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 🙂

Facebook
Twitter
LinkedIn

Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Excel School made me great at work.
5/5

– Brenda

Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

Weighted Average in Excel with Percentage Weights

Weighted Average in Excel [Formulas]

Learn how to calculate weighted averages in excel using formulas. In this article we will learn what a weighted average is and how to Excel’s SUMPRODUCT formula to calculate weighted average / weighted mean.

What is weighted average?

Wikipedia defines weighted average as, “The weighted mean is similar to an arithmetic mean …, where instead of each of the data points contributing equally to the final average, some data points contribute more than others.”

Calculating weighted averages in excel is not straight forward as there is no built-in formula. But we can use SUMPRODUCT formula to easily calculate them. Read on to find out how.

21 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)}

    • Harish says:

      Hi,
      I am working on trend analysis to find different types of issues logged.
      I have taken extract of them into excel and now trying to find out what are different type of issues reported. Doing manually is much time taking activity.
      Is there a way to do this in excel? Code or formula which can read through a column in excel sheet and report what are different words there?
      Thanks in advance!

  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