+ Reply to Thread
Results 1 to 4 of 4

Loop through all occurrences of a string within a string

  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,441

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

    the INSTR function has a start position argument.

    Please Login or Register  to view this content.
    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,441

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

    Please Login or Register  to view this content.

+ 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