+ Reply to Thread
Results 1 to 4 of 4

Loop through all occurrences of a string within a string

Hybrid View

  1. #1
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Loop through all occurrences of a string within a string

    Hi guys,

    with a web request (MSXML2.XMLHTTP) I pull a responsetext of a website.
    This responsetext has several occurrences of the string " shops", which comes with a number in front of it.

    So for example

    "1 shops"
    "12 shops"
    "3 shops"

    These quantities of shops are not in order, so basically what I want to do is loop through the occurences, find the one with the highest quantity and go from there (I'll grab a link after that from the same responsetext, based on the location of that "x shops" I found to be the highest)

    What's the best way to go about it? Just "inStr" or "inStrRev" wouldn't only show me the first and last occurrence - so what's the best way to go through all?
    I was considering to do "x = Instr(responsetext, x, " shops")" and then x = x + 1, loop until there's no x anymore.
    But that doesn't sound very easy, so was wondering if someone can help me do this more efficiently.

    Thanks!
    Please click the * below if this helps

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: Loop through all occurrences of a string within a string

    the INSTR function has a start position argument.

    Private Function m_GetNumber(Text As String) As Long
        Dim lngIndex As Long
        
        For lngIndex = Len(Text) To 1 Step -1
            If Not IsNumeric(Mid(Text, lngIndex)) Then
                m_GetNumber = CLng(Mid(Text, lngIndex + 1))
                Exit Function
            End If
        Next
        m_GetNumber = CLng(Text)
        Exit Function
    End Function
    
    Sub X()
    
        Dim strTest As String
        Dim lngCount As Long
        Dim lngIndex As Long
        Dim lngShops() As Variant
        Dim lngPos As Long
        Dim lngStart As Long
        Const SHOPS = "SHOPS"
        
        strTest = UCase("1 shops 12 shops 3 shops")
        lngCount = (Len(strTest) - Len(Replace(strTest, SHOPS, ""))) / Len(SHOPS)
        ReDim lngShops(1 To lngCount)
        
        lngStart = 1
        lngPos = InStr(lngStart, strTest, SHOPS, vbTextCompare)
        lngIndex = 0
        Do While lngPos > 0
            lngIndex = lngIndex + 1
            lngShops(lngIndex) = m_GetNumber(Trim(Mid(strTest, lngStart, lngPos - lngStart)))
            lngStart = lngPos + Len(SHOPS)
            lngPos = InStr(lngStart, strTest, SHOPS, vbTextCompare)
        Loop
        
        For lngIndex = 1 To lngCount
            Debug.Print lngIndex, lngShops(lngIndex)
        Next
    End Sub
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Loop through all occurrences of a string within a string

    Hi Andy,

    thanks for your reply - this will probably work (I didn't test it yet), but I was looking for something simple actually
    I am now using an approach by splitting the string with delimiter " shops" and loop from 0 to ubound variable checking the last few characters of each split (cause if I split "12 shops" with delimiter " shops"), the final characters of the part before the splut will be "12", so I thought this is the easiest way).

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: Loop through all occurrences of a string within a string

    Sub X()
    
        Dim strTest As String
        Dim lngCount As Long
        Dim lngIndex As Long
        Dim lngShops() As Variant
        Dim vntItem As Variant
        Const SHOPS = "SHOPS"
        
        strTest = UCase("1 shops 12 shops 3 shops")
        lngCount = (Len(strTest) - Len(Replace(strTest, SHOPS, ""))) / Len(SHOPS)
        ReDim lngShops(0 To lngCount - 1)
        
        lngIndex = 0
        For Each vntItem In Split(strTest, SHOPS)
            If Len(Trim(vntItem)) > 0 Then
                lngShops(lngIndex) = CLng(Trim$(vntItem))
                lngIndex = lngIndex + 1
            End If
        Next
        
        For lngIndex = 0 To lngCount - 1
            Debug.Print lngIndex, lngShops(lngIndex)
        Next
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Finding multiple occurrences in a string
    By Julie_Heyes in forum Excel Formulas & Functions
    Replies: 16
    Last Post: 11-01-2012, 03:27 PM
  2. conditional occurrences of specific string in a range
    By yogeshmaney in forum Excel General
    Replies: 3
    Last Post: 07-15-2012, 09:13 AM
  3. String search - multiple occurrences
    By jcountzler in forum Excel General
    Replies: 3
    Last Post: 11-24-2010, 04:12 AM
  4. Count the occurrences of a text string?
    By New2MSExcel in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-24-2010, 08:47 PM
  5. Replies: 10
    Last Post: 10-10-2009, 07:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1