Countif(substring)

MartinL

Well-known Member
Joined
Oct 16, 2008
Messages
1,141
Office Version
  1. 365
Platform
  1. Windows
Can I count the number of time a substring appears in a list

ie:
Excel Workbook
AB
1String to find
2is a
3This is a string3
4This is also a string
5This is a mistake
6This is a sentence
Sheet1
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi Peter

I tried it on my licve work and it didnt work
then I tried it on another string and it did work
then i noticed, that the string i was searching for was the only thing in the cell and it appeared only once anyway.

I think this solution does not work if the substring is at the beginning (and possibly end) of the string

Ugghhhhh!

Martin
 
Last edited:
Upvote 0
Hi All,

As Peter knows, I'm not much at formulas, but I think there would be an issue with stuff like "is also" with the wilcard.

Not well tested, but I was thinking maybe testing against the word boundary on both ends might work.
Excel Workbook
AB
1String to find
2is a
3This is a string4
4This is also a string
5This is a mistake
6This is a sentence
7No, that is a mistake.
Sheet1
Excel 2000
Cell Formulas
RangeFormula
B3=SubStrings_Count(A3:A7,B2,TRUE,TRUE)


With the UDF in a Standard Module:<font face=Courier New><SPAN style="color:#00007F">Option</SPAN><SPAN style="color:#00007F">Explicit</SPAN><br>    <br><SPAN style="color:#00007F">Function</SPAN> SubStrings_Count(CellRange<SPAN style="color:#00007F">As</SPAN> Range, _<br>                          SubString<SPAN style="color:#00007F">As</SPAN> String, _<br>                          <SPAN style="color:#00007F">Optional</SPAN> AllTimesPerCell<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Boolean</SPAN> =<SPAN style="color:#00007F">True</SPAN>, _<br>                          <SPAN style="color:#00007F">Optional</SPAN> bolIgnoreCase<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Boolean</SPAN> = True _<br>                          )<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN><br>                          <br><SPAN style="color:#00007F">Dim</SPAN> REX<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Object</SPAN><SPAN style="color:#007F00">'< RegExp</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> rexMatches<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Object</SPAN><SPAN style="color:#007F00">'< MatchCollection</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> rCell<SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> lCount<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN><br>    lCount = 0<br>    <SPAN style="color:#00007F">Set</SPAN> REX = CreateObject("VBScript.RegExp")<br>    <SPAN style="color:#00007F">With</SPAN> REX<br>        .Global = AllTimesPerCell<br>        .IgnoreCase = bolIgnoreCase<br>        .Pattern = "\b" & SubString & "\b"<br>        <SPAN style="color:#00007F">For</SPAN><SPAN style="color:#00007F">Each</SPAN> rCell<SPAN style="color:#00007F">In</SPAN> CellRange<br>            <SPAN style="color:#00007F">If</SPAN> .Test(rCell)<SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> rexMatches = .Execute(rCell)<br>                lCount = lCount + rexMatches.Count<br>            <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN><br>    <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN><br>    SubStrings_Count = lCount<br><SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Function</SPAN></FONT>

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,215,438
Messages
6,124,873
Members
449,192
Latest member
MoonDancer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top