+ Reply to Thread
Results 1 to 7 of 7

InStr returns 0 when it shouldn't

Hybrid View

  1. #1
    Registered User
    Join Date
    02-11-2016
    Location
    Wisconsin
    MS-Off Ver
    07-360
    Posts
    53

    InStr returns 0 when it shouldn't

    I have spent too long trying to figure this out and was hoping that someone may have the answer (likely a noob mistake on my part). I am running the following code:
                For i = 1 To 2
                LoopText = Me("CB_CG" & i).value
                MsgBox ("LoopText = " & LoopText & newLine() & _
                  "sh3.Range('ClientFullName').value = " & sh3.Range("ClientFullName").value & newLine() & _
                  "Instr Returns: " & InStr(LCase(LoopText), LCase(sh3.Range("ClientFullName").value)))
                MsgBox (InStr(LCase(LoopText), LCase(sh3.Range("ClientFullName").value)))
                    If InStr(LCase(LoopText), LCase(sh3.Range("ClientFullName").value)) <> 0 Then
                    MsgBox ("The Client can not be included with the billing code listed.  Please remove the client or use a different billing code.")
                    Exit Sub
                    End If
                Next i
    And every time, InStr returns 0 no matter the string or substring values (even if Identical).

    InStr Dead.JPG

    I know the code is redundant and messy but this is after several hours of trying to get it to work. My guess is that it is due to trying to use a form combobox value as the string to search in as using the same info from the workbook seems to work.

    Any help will be greatly appreciated!

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: InStr returns 0 when it shouldn't

    Looks like you've got extra space or some other non-print character for ("ClientFullName").value

    INSTR searches for match of entire string, so if there is extra space in middle, it won't find match.

    Edit: Try TRIM and/or WorksheetFunction.Clean on the value before INSTR.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: InStr returns 0 when it shouldn't

    Attaching a sample workbook enables others to work on your problem:

    To attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and click "manage attachments" to open the upload window.




    To add a file to a post

  4. #4
    Registered User
    Join Date
    02-11-2016
    Location
    Wisconsin
    MS-Off Ver
    07-360
    Posts
    53

    Re: InStr returns 0 when it shouldn't

    Thanks CK76
    That in fact was the case, ("ClientFullName") was created with "First" & " " & "Last" as a cell Formula. Apparently adding a space like this is different than typing a space in a string. I will just run InStr twice, once for first and once for last (In other words removing the space). If both are met, then it will run the code.

    Thanks! I knew it was something small like that. I would have never have guessed spaces are not equal.
    Last edited by mnadamn19; 09-08-2017 at 02:38 PM.

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: InStr returns 0 when it shouldn't

    Hello
    I tested your code on my own and it works and the result was 1 not 0
    Sub Test()
        Dim LoopText As String
        
        Dim sh3
        Set sh3 = ActiveSheet
    
        LoopText = "John Doe"
    
        MsgBox ("LoopText = " & LoopText & vbNewLine & _
                "sh3.Range('ClientFullName').value = " & _
                sh3.Range("ClientFullName").Value & vbNewLine & _
                "Instr Returns: " & _
                InStr(LCase(LoopText), LCase(sh3.Range("ClientFullName").Value)))
                
        MsgBox (InStr(LCase(LoopText), LCase(sh3.Range("ClientFullName").Value)))
    End Sub
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,533

    Re: InStr returns 0 when it shouldn't

    It is difficult to debug from a picture. You insist that InStr returns 0 even when the string and substring values are identical. What have you done to ascertain for certain that they are identical? If I were debugging this, I would focus my efforts on verifying that they are indeed identical as you claim. Some tests I would probably run:

    1) A character by character comparison of the search string and the sub string. Looking at the picture of your message box, I see a larger space between John Doe in LoopText and the cell value. Is it possible there are two space characters there? Is it possible that they are different space characters (regular space code 32 vs non-breaking space code 160 vs. maybe an m-space character code 8195)?
    2) What does InStr() return for "john" or "doe" or "hn" or other combination of substring?
    3) What do I get from Len(substring) versus Len(looptext)?

    This sort of question usually comes down to validating/falsifying the OP's claim that the two strings are identical, so I would focus my efforts there.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  7. #7
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: InStr returns 0 when it shouldn't

    You are welcome and thanks for the rep.

    FYI - Typing and concatenating space character should be the same thing. I suspect, you have trailing or preceding space attached to "First" or "Last".

+ 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. instr returns 0 always
    By rhayes1010 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-07-2013, 06:13 PM
  2. Shouldn't IF then work?
    By ccampbell14 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-12-2013, 04:10 PM
  3. InStr function returns pseudo True/False?
    By GCW esq in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-04-2012, 08:00 AM
  4. error when there shouldn't be
    By omgeokid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-17-2011, 11:52 AM
  5. Shouldn't DDB and VDB(...,True) be the same
    By lucabol in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-27-2008, 06:53 AM
  6. # signs where there shouldn't be....
    By dramajuana in forum Excel General
    Replies: 6
    Last Post: 08-14-2006, 05:25 PM
  7. Shouldn't this work?
    By Jonathan Cooper in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-03-2006, 12:10 PM
  8. Hyperlinking when it shouldn't...
    By B.C.Lioness in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-17-2005, 03:06 PM

Tags for this Thread

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