PC Review


Reply
Thread Tools Rate Thread

Count occurences of string in cell

 
 
=?Utf-8?B?R2V0dGluZ1RoZXJl?=
Guest
Posts: n/a
 
      12th Jan 2007
I'm trying to count the number of times a sequence of characters occurs in a
cell. The sequence could be surrounded by non-printing characters, and/or
could appear more that once in one string (ABCblahABC SDF ABC blah).

I tried the following, but it seems to "miss" sometimes. Any reason why?

Thanks in advance!

Sub getNum()
Dim i As Long
Dim str As String

For i = 2 To Cells(Rows.Count, "a").End(xlUp).Row
str = InStr(Cells(i, 1), "ABC")
Cells(i, "B") = str
Next i

End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sk1C?=
Guest
Posts: n/a
 
      12th Jan 2007
Instr just gives you the starting position of the first occurrence of "ABC".

Try this worksheet function
=(LEN($A2)-LEN(SUBSTITUTE($A2,B$1,"")))/LEN(B$1)

where your string is in A1 and "ABC" is in B1 (or you can hardcode "ABC" in
the formula. then copy down (and across if needed).

if it has to be done w/vba check vba help for the Replace function, which
works similar to the substitute function and construct a statement similar to
the one above.


"GettingThere" wrote:

> I'm trying to count the number of times a sequence of characters occurs in a
> cell. The sequence could be surrounded by non-printing characters, and/or
> could appear more that once in one string (ABCblahABC SDF ABC blah).
>
> I tried the following, but it seems to "miss" sometimes. Any reason why?
>
> Thanks in advance!
>
> Sub getNum()
> Dim i As Long
> Dim str As String
>
> For i = 2 To Cells(Rows.Count, "a").End(xlUp).Row
> str = InStr(Cells(i, 1), "ABC")
> Cells(i, "B") = str
> Next i
>
> End Sub
>

 
Reply With Quote
 
=?Utf-8?B?TWFydGluIEZpc2hsb2Nr?=
Guest
Posts: n/a
 
      12th Jan 2007
Hi,

The loop you are using is only checking the first occurance of the string
and returning the position in thestring not the count. The following appears
to do the job.

Sub getNum()

Const csFind As String = "ABC"
Dim lRow As Long
Dim sStr As String
Dim iStrCount As Integer, iStartPos As Integer, iPosInStr As Integer

For lRow = 2 To Cells(Rows.Count, "a").End(xlUp).Row
sStr = Cells(lRow, 1)
iStrCount = 0
iPosInStr = 0
Do
iPosInStr = InStr(iPosInStr + 1, sStr, csFind)
If iPosInStr <> 0 Then iStrCount = iStrCount + 1
Loop Until iPosInStr = 0
Cells(lRow, 2) = iStrCount
Next lRow

End Sub

--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


"GettingThere" wrote:

> I'm trying to count the number of times a sequence of characters occurs in a
> cell. The sequence could be surrounded by non-printing characters, and/or
> could appear more that once in one string (ABCblahABC SDF ABC blah).
>
> I tried the following, but it seems to "miss" sometimes. Any reason why?
>
> Thanks in advance!
>
> Sub getNum()
> Dim i As Long
> Dim str As String
>
> For i = 2 To Cells(Rows.Count, "a").End(xlUp).Row
> str = InStr(Cells(i, 1), "ABC")
> Cells(i, "B") = str
> Next i
>
> End Sub
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Count occurences in string Ron Rosenfeld Microsoft Excel Misc 0 10th Jun 2009 08:06 PM
How to Count String occurences =?Utf-8?B?Q2hhcGxhaW4gRG91Zw==?= Microsoft Excel Programming 4 22nd Mar 2005 07:14 PM
Tough One, trying to count occurences of string based on string in separate column Rick Microsoft Excel New Users 3 28th Feb 2004 02:10 AM
Tough One, trying to count occurences of string based on string in separate column Rick Microsoft Excel Worksheet Functions 3 28th Feb 2004 02:10 AM
How to count the occurences of a text string within a cell... Healingbear Microsoft Excel Misc 1 19th Jan 2004 01:33 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 PM.