Reputation: 43
<br>1<br>text <br>2<br>text <br>3<br>
<br>4<br>
<br>5<br>text <br>6<br>text <br>7<br>text <br>8<br>
What is the formula to count the number of times <br>*<br>
appears in this table A1:A3? Keep in mind there is a space in the front of each cell.
The total count should be 8. =COUNTIF(A1:A3," <br>*<br>")
only returns 3 however.
Edit: Have made data simpler so it is easier to read
Upvotes: 1
Views: 2126
Reputation: 3802
Or,
In B1 enter formula :
=SUMPRODUCT(LEN(A1:A3)-LEN(SUBSTITUTE(A1:A3," ","")))
Upvotes: 1
Reputation: 75840
This seems like XML/HTML so why not parse it through FILTERXML
with a XPATH
construct to count values that do not contain a space and are not empty. For example with your data in A1
:
=COUNTA(FILTERXML("<t><br>"&SUBSTITUTE(CONCAT(A1:A3),"<br>","</br><br>")&"</br></t>","//br[not(contains(., ' '))][node()]"))
Or when your valid data could hold spaces we would need to filter out the ones that end with a space (and the empty ones again):
=COUNTA(FILTERXML("<t><br>"&SUBSTITUTE(CONCAT(A1:A3),"<br>","</br><br>")&"</br></t>","//br[not(substring(., string-length(.), 1)=' ')][node()]"))
If however what you are after are the numeric nodes only to count then use:
=COUNT(FILTERXML("<t><br>"&SUBSTITUTE(CONCAT(A1:A3),"<br>","</br><br>")&"</br></t>","//br[.*0=0]"))
The cool thing is FILTERXML
will actually return an array for further analyses, in your case you can count the elements in the array.
Upvotes: 1
Reputation: 564
This Microsoft documentation link can help you out with multiple variations of what you're trying to achieve - https://learn.microsoft.com/en-us/office/troubleshoot/excel/formulas-to-count-occurrences-in-excel
To count the occurrences of a substring in a range of cells, you can use this:
SUM(LEN(range)-LEN(SUBSTITUTE(range,"text","")))/LEN("text")
If the above does not work for you you can try this:
Numbers:
To count the occurrences of a substring in one cell, a combination of formulas in excel will do the trick.
Short answer (just plug in your specific text or cell references into this):
=(LEN("original_text")-LEN(SUBSTITUTE("original_text","text_to_count","")))/LEN("text_to_count")
Long answer if you're interested:
=SUBSTITUTE("123331<br>asdsdas <br>","<br>","")
This will substitute all occurrences of
with an empty string in your original string - 123331asdsdas
=LEN(SUBSTITUTE("123331<br>asdsdas <br>","<br>",""))
This will return 14 which is the length of 123331asdsdas.
=LEN("123331<br>asdsdas <br>")
which gives you 22.
=LEN("123331<br>asdsdas <br>") - LEN(SUBSTITUTE("123331<br>asdsdas <br>","<br>",""))
which subtracts length of the substituted string from length of the original string giving you 8.
(LEN("123331<br>asdsdas <br>") - LEN(SUBSTITUTE("123331<br>asdsdas <br>","<br>","")))/LEN("<br>")
divides the above answer from the subtraction by the length of your original string, giving you 2.
This is the number of times
occurred in one cell.
(length_of_original_string - length_of_substituted_string)/length_of_string_to_find
which translates to (22-14)/4 = 2
in your case.Upvotes: 1