Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 553
Default Counting SubStrings and thier starting positions within Main Strin

What is the easiest way to count the number of occurences of a substring
within a string AND the starting position of each substring? For Example:

String = "dog 123 cat 452 if 6754 dog"
SubString = "dog"

Occurences of SubString = 2
Starting Position of SubString = 1 and 26

Thanks

EM
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default Counting SubStrings and thier starting positions within Main Strin

One way:

Public npos() as integer

Sub GetSubStrings(ByVal SearchString As String, FindStr As String)
n = 1
i = 0
Do
n = InStr(n, SearchString, FindStr)
If n < 0 Then
ReDim Preserve Npos(i)
Npos(i) = n
n = n + 1
i = i + 1
End If
Loop Until n = 0
End Sub

Sub myTest()

Call GetSubStrings("dog 123 cat 452 if 6754 dog", "dog")

MsgBox "Number of strings = " & UBound(Npos) + 1
For i = 0 To UBound(Npos)
MsgBox Npos(i)
Next
End Sub

"ExcelMonkey" wrote:

What is the easiest way to count the number of occurences of a substring
within a string AND the starting position of each substring? For Example:

String = "dog 123 cat 452 if 6754 dog"
SubString = "dog"

Occurences of SubString = 2
Starting Position of SubString = 1 and 26

Thanks

EM

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Counting SubStrings and thier starting positions within Main Strin

cTimes = (Len(sString) - Len(Replace(sString, sSubstring, ""))) _
/ Len(sSubstring)
sMsg = sSubstring & " occurs" & cTimes & " times" & vbNewLine
ipos = 0
For i = 1 To cTimes
ipos = InStr(ipos + 1, sString, sSubstring)
sMsg = sMsg & "#" & i & " occurs at: " & ipos & vbNewLine
Next i

MsgBox sMsg


--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"ExcelMonkey" wrote in message
...
What is the easiest way to count the number of occurences of a substring
within a string AND the starting position of each substring? For Example:

String = "dog 123 cat 452 if 6754 dog"
SubString = "dog"

Occurences of SubString = 2
Starting Position of SubString = 1 and 26

Thanks

EM



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
array formulas counting substrings across rows Christi Excel Worksheet Functions 6 April 24th 09 05:36 AM
counting starting with a number other than 1 norman mellow Excel Discussion (Misc queries) 1 December 30th 08 08:42 PM
Counting Starting Digits Qban Excel Worksheet Functions 1 December 11th 05 04:04 PM
Counting Positions smandula Excel Programming 2 October 3rd 05 04:50 PM
counting cells in a column based on thier color Niek Otten Excel Programming 0 January 20th 04 08:11 PM


All times are GMT +1. The time now is 12:37 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"