+ Reply to Thread
Results 1 to 8 of 8

InStr function returns pseudo True/False?

  1. #1
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    InStr function returns pseudo True/False?

    I have been playing with the Instr function (Excel 2003), and it appears to behave as though it's returning a True/False value.
    For example, the following will print "1":

    Please Login or Register  to view this content.
    I said "pseudo" above because if the code tests for "True", it doesn't find it. The following prints "0":

    Please Login or Register  to view this content.
    What's the go? Safe to use it like that (boolean)?

  2. #2
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: InStr function returns pseudo True/False?

    Hi,

    Instr returns the position of the match

    This means if instr returns 0 no match is found

    If it returns > 0 then a match is found

    Please Login or Register  to view this content.
    Please take time to read the forum rules

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: InStr function returns pseudo True/False?

    your first example works because vba performs an implicit coercion to a boolean (0 = false, any other number = true). the second fails because your explicit comparison is false (True = -1, not the 2 your InStr returns). as steffen said it is much better to compare to 0.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Re: InStr function returns pseudo True/False?

    So what's the value of making it explicit?
    More obvious to others (and to me in a year's time) what's going on?
    Less chance of making an error?
    It seems pretty clear in an IF statement what's happening, n'est-ce pas?

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: InStr function returns pseudo True/False?

    Quote Originally Posted by GCW esq View Post
    So what's the value of making it explicit?
    More obvious to others (and to me in a year's time) what's going on?
    Less chance of making an error?
    both. would you really use
    Please Login or Register  to view this content.
    when coding?
    It seems pretty clear in an IF statement what's happening, n'est-ce pas?
    it seems not since you asked this question. ;-)
    what benefit do you see in the former syntax given the confusion it has caused you? InStr does not return a boolean, it returns a long (well, variant) so why not use it that way?

  6. #6
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Re: InStr function returns pseudo True/False?

    Thanks, JosephP. I wasn't really confused. I just thought I was seeing a neater way to do things, and wondering why not?
    (Said sadly) I guess you're right. I'll go back to my old, explicit ways. SIGH!

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: InStr function returns pseudo True/False?

    I'm not saying you can't use it that way-you certainly can. I just don't think it's the best option partly because you'll probably try and use that = True comparison again at some point and it won't work but won't return an error either. of course, I can't think of any reason to use an '= True' comparison at all: if something is true, there ain't much point comparing it to True just to return True again. :-)

  8. #8
    Registered User
    Join Date
    11-25-2011
    Location
    Lilydale, Victoria, Australia
    MS-Off Ver
    2013 (365)
    Posts
    63

    Re: InStr function returns pseudo True/False?

    Yes, thanks.
    The only reason I used the "=True" was to test what the code was doing. I didn't actually try to use it in anything, so no worries there.
    I see your point though. Probably best to keep it explicit.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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