UDF for counting the number of occurrences of text in range

rnegru

New Member
Joined
Jan 7, 2009
Messages
8
Hello all,

I don't know if anyone has posted the same question yet, I am sorry but I haven't found it. I use an UDF to count the number of occurrences in any one cell for an inputed string. I have pasted below the code:

Function Count_Word(ByRef sInput As String, ByRef sWord As String) As Long
Dim lCount As Long
Dim lWhere As Long
Dim lWordLen As Long

lWordLen = Len(sWord)

lWhere = InStr(sInput, sWord)

Do While lWhere
lCount = lCount + 1
lWhere = InStr(lWhere + lWordLen, sInput, sWord)
Loop

Count_Word = lCount
End Function


Can anyone help me with a new UDF (or with modifying this one), so that I could count the occurrences of text in a range? I use Excel 2003, if that helps.

Thanks in advance,
Radu
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

texasalynn

Well-known Member
Joined
May 19, 2002
Messages
8,458
you don't really need a UDF, try this

Excel Workbook
AB
1Can anyone help me with a new UDF (or with modifying this one), so that I could count the occurrences of text in a range? I use Excel 2003, if that helps.
232
Sheet1
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Hi, Try:-
This will also find the word when its within another word.
Code:
Function word(rng [COLOR=navy]As[/COLOR] Range, wd [COLOR=navy]As[/COLOR] String) [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR]
[COLOR=navy]Dim[/COLOR] w [COLOR=navy]As[/COLOR] [COLOR=navy]Integer,[/COLOR] c
  [COLOR=navy]For[/COLOR] w = 1 To Len(rng)
    [COLOR=navy]If[/COLOR] Mid(rng, w, Len(wd)) = wd [COLOR=navy]Then[/COLOR]
        c = c + 1
     [COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] w
word = c
[COLOR=navy]End[/COLOR] Function
Regards Mick
 
Last edited:

rnegru

New Member
Joined
Jan 7, 2009
Messages
8
Hi,

Thanks for your reply.

@texasalynn: thanks for the formula, it's a very useful one and I will use it :). but i need to be able to search for a certain text in a string, and then count the number of times it occurs. Thanks nonetheless.

@MickG: Your UDF works like a charm when in rng field I select a single cell. However, if I select multiple cells, it evaluates to a #VALUE! error. Any ideas how I could extend the rng to multiple cell selections?

Regards,
Radu
 

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154

ADVERTISEMENT

Maybe:
Excel Workbook
AB
1Peter Piper picked a peck of pickled peppers; A peck of pickled peppers Peter Piper picked;4
2If Peter Piper picked a peck of pickled peppers,4
3Where's the peck of pickled peppers Peter Piper picked?2
Sheet4
Excel 2003
Cell Formulas
RangeFormula
B1=SUBSTRING_COUNT(A1:A3,"peck")
B2=SUBSTRING_COUNT(A1:A3,"pickled peppers")
B3=SUBSTRING_COUNT(A1,"pickled peppers")


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> SubString_Count(CellAddress<SPAN style="color:#00007F">As</SPAN> Range, _<br>                         TestSubString<SPAN style="color:#00007F">As</SPAN> String, _<br>                        <SPAN style="color:#00007F">Optional</SPAN> bolIgnoreCase<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Boolean</SPAN> =<SPAN style="color:#00007F">False</SPAN>)<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN><br>Dim _<br>oMatches    <SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Object</SPAN>, _<br>ary        <SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Variant</SPAN>, _<br>val        <SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Variant</SPAN>, _<br>lCount      <SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN><br>    <br>    lCount = 0<br>    <br>    <SPAN style="color:#00007F">With</SPAN> CreateObject("VBScript.RegExp")<br>        .Global =<SPAN style="color:#00007F">True</SPAN><br>        .IgnoreCase = bolIgnoreCase<br>        .Pattern = "\b" & TestSubString & "\b"<br>        <br>        <SPAN style="color:#00007F">If</SPAN> CellAddress.Cells.Count > 1<SPAN style="color:#00007F">Then</SPAN><br>        <br>            ary = CellAddress.Value<br>            <br>            <SPAN style="color:#00007F">For</SPAN><SPAN style="color:#00007F">Each</SPAN> val<SPAN style="color:#00007F">In</SPAN> ary<br>                <SPAN style="color:#00007F">If</SPAN> .Test(val)<SPAN style="color:#00007F">Then</SPAN><br>                    <SPAN style="color:#00007F">Set</SPAN> oMatches = .Execute(val)<br>                    lCount = lCount + oMatches.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">Else</SPAN><br>            <SPAN style="color:#00007F">If</SPAN> .Test(CellAddress.Value)<SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> oMatches = .Execute(CellAddress.Value)<br>                lCount = oMatches.Count<br>            <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN><br>    <br>    SubString_Count = lCount<br><SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Function</SPAN></FONT>

Hope that helps,

Mark
 
Last edited:

rnegru

New Member
Joined
Jan 7, 2009
Messages
8
@GTO: Hi, Mark. Worked like a charm. Thanks a million :)

Regards,
Radu
 

Watch MrExcel Video

Forum statistics

Threads
1,122,518
Messages
5,596,622
Members
414,082
Latest member
sasmita

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
Top