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:
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.
- Array formula to count maximum text occurrences in a range
- How to count words in a cell using formulas
- How many times a list of values occurs in another list?
- VLOOKUP based on pattern
PS: Thanks to Simran who emailed me this question.
PPS: The writer is not balding. He still sports plenty of pointy hair 🙂
21 Responses to “Find out how many times a value is present in a cell [formulas]”
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?
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
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.
Array formula
={Sum((LEN(B6:B19) - LEN(SUBSTITUTE(B6:B19,$C$3,""))/LEN($C$3)}
Wow Pavel! I created the exact same array!
=SUM((LEN(B6:B19)-LEN(SUBSTITUTE(B6:B19,$C$3,"")))/LEN($C$3))
Cheers,
Kevin Lehrbass
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)}
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!
Have a read of:
http://chandoo.org/wp/2011/11/07/formula-forensics-002/
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?
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?
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)
Thanks Haz. I can't even begin to understand that, but I'll certainly use it!
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))
=(SUM(LEN(B6:B19))-SUM(LEN(SUBSTITUTE(B6:B19,C3,""))))/LEN(C3)
CSE for array formula
Doh. Pavel and Juanma beat me to it.
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)
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.
I just modified original formula to:
=SUMPRODUCT((LEN(B6:B19)- LEN(SUBSTITUTE(B6:B19,$C$3,"")))/LEN($C$3))
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.
=SUMPRODUCT(LEN(B5:B18)-LEN(SUBSTITUTE(B5:B18,$C$2,"")))/LEN(C2)
=SUMPRODUCT(--(MID(A4,ROW(INDIRECT("1:"&LEN(A4))),LEN(B1))=B1))
updated addresses for the values
=SUMPRODUCT(--(MID(b6,ROW(INDIRECT("1:"&LEN(b6))),LEN(c$3))=c$3))