How to Count String occurences

C

Chaplain Doug

Excel 2003. Is there a quick way (a function perhaps) to count the number of
occurences of a substring within a string? I have address strings in which I
want to quickly count the number of occurences of a double space (" ") in
the string. Thanks for the help.
 
C

Chip Pearson

Try something like the following:


Dim S As String
Dim SearchString As String
Dim SubStringCount As Integer
S = "the quick brown fox"
SearchString = " " ' two spaces
SubStringCount = (Len(S) - Len(Replace(S, SearchString, ""))) /
Len(SearchString)
Debug.Print SubStringCount


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





message
news:[email protected]...
 
B

bj

If you are sure there are no space multiples greater than 2 you can use
=(len(a1)-len(substitute(a1," ",""))/2
 
C

Chaplain Doug

Thanks for the cool tip. Did you guys study mathematics? I've got degrees
in it, but my doltish brain didn't think of this. I AM getting old! God
bless.

Chip Pearson said:
Try something like the following:


Dim S As String
Dim SearchString As String
Dim SubStringCount As Integer
S = "the quick brown fox"
SearchString = " " ' two spaces
SubStringCount = (Len(S) - Len(Replace(S, SearchString, ""))) /
Len(SearchString)
Debug.Print SubStringCount


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





message
 
C

Chip Pearson

As a matter of fact, I do have a degree in mathematics. Haven't
used it in years, though.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


message
Thanks for the cool tip. Did you guys study mathematics? I've
got degrees
in it, but my doltish brain didn't think of this. I AM getting
old! God
bless.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top