+ Reply to Thread
Results 1 to 7 of 7

If string contains "word" then...

  1. #1
    Registered User
    Join Date
    08-21-2003
    Posts
    45

    If string contains "word" then...

    I'm using InStr to look for words within a string but its acting buggy.

    Here's the code I'm using:
    Please Login or Register  to view this content.
    Heres a value where it contains "lpd" and also "course" so the code should return "/lpd/ with Courses" but instead returns "/lpd/ Other":
    http://xyz.com/school/lpd/science-wo...ml?type=course

    Any idea why? Why is InStr not finding "lpd" and "course" in the above value? Is there a better to do what I'm trying to do?

    Thanks for any help.
    Last edited by FDibbins; 01-05-2016 at 07:33 PM.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: If string contains "word" then...

    Your link didn't work for me. Hard to tell without seeing what tempString is but I think you may be having an issue with the parameters of using the Instr() function. Instr returns an integer and you have not specified that as part of your function. I could be wrong though. Here is an example.

    Please Login or Register  to view this content.
    If you are happy with my response please click the * in the lower left of my post.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: If string contains "word" then...

    Perhaps there is there a capitalisation case difference of the text you're looking for and the data? If yes, this will ignore case.

    Please Login or Register  to view this content.
    On another note; if you start the code block by testing if the string contains "lpd", you wouldn't have to test for that again in the subsequent If-ElseIf statements.
    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 01-05-2016 at 08:30 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    08-21-2003
    Posts
    45

    Re: If string contains "word" then...

    Hard to tell without seeing what tempString is
    tempString = "http://xyz.com/school/lpd/science-worksheets.html?type=course"

    Perhaps there is there a capitalisation case difference
    The url I'm looking for strings in is lowercase so there shouldnt be any capitalization issues

    Any other ideas? This is killing me. It seems like such a simple thing so its really frustrating that I cant figure out why it's not working...

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: If string contains "word" then...

    This simple test works.

    Please Login or Register  to view this content.
    So I would have to suspect that in your case tempstring isn't what you think it is, or is it possible r.Offset(, -1).Value isn't the cell you think it is, or this block of code isn't run for some reason.

  6. #6
    Registered User
    Join Date
    08-21-2003
    Posts
    45

    Re: If string contains "word" then...

    That approach works for me too - thanks! I guess there's something funky that happens when using "If InStr... And InStr..." instead of nested if statements (as in your example above). If anyone has an answer, I would love to know the technical reason behind this as, to me, both pieces of code should produce the same result, no?

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: If string contains "word" then...

    You're welcome.

    This test uses your original code with "If InStr... And InStr..." and it worked just as well. So i don't know what to tell you.
    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. Macro To "search all" Most Frequently appearing Text Word String in Excel Sheet
    By caabdul in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-06-2015, 09:08 AM
  2. [SOLVED] (Challenge) Sum Order From Count "Center" Word To Start Word & "Center" Word To End Word
    By sanju2323 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-31-2015, 12:58 AM
  3. [SOLVED] delete "SPACES" at the end of a string or word in a column
    By kjxavier in forum Excel General
    Replies: 6
    Last Post: 10-15-2014, 09:24 AM
  4. [SOLVED] Need formula that finds the last ", " in a text string & substitutes it w/ the word "and"
    By danielneedssomehelp in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 08-23-2014, 04:06 PM
  5. Replies: 1
    Last Post: 08-15-2014, 06:00 AM
  6. Replies: 5
    Last Post: 01-23-2014, 11:02 AM
  7. [SOLVED] How to Count number of "Error" and "OK" after the word "Instrument" found in table row
    By eltonlaw in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2012, 06:26 AM

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