+ Reply to Thread
Results 1 to 6 of 6

InStr not working properly.

  1. #1
    Registered User
    Join Date
    10-10-2017
    Location
    UK
    MS-Off Ver
    365
    Posts
    28

    InStr not working properly.

    I have a variable (string) as below

    DevelopmentStageSF1 - "Implementable, In Planning, Owned, Conditional/In Negotiation, Target"

    I am using an 'if' query to determine which words are in this variable. However it is not really working.


    Why does this pass the 'if' query.

    If InStr(DevelopmentStageSF1, "Implementable") And InStr(DevelopmentStageSF1, "Owned") Then ...


    And this does not...?

    If InStr(DevelopmentStageSF1, "Implementable") And InStr(DevelopmentStageSF1, "Conditional") Then ...
    Last edited by DouglasWicker; 09-22-2021 at 05:01 AM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    91,068

    Re: Why doesn't this work?

    Without my crystal ball, I fear I won't be able to help ...

    Administrative Note:

    We would very much like to help you with your query, however the thread title does not really convey what your request is about. Tell us what you are trying to do, not how you think it should be done.

    Please take a moment to amend your thread title. Make sure that the title properly explains your request. Your title should be explicit and not be generic (this includes function names used without an indication of what you are trying to achieve).

    Please see Forum Rule #1 about proper thread titles and adjust accordingly. To edit the thread title, open the original post to edit and then click on Go Advanced (bottom right) to access the area where you can edit your title.

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)


    Although we value your privacy as much you do, it could be important that members have a rough idea of your location as the solutions they offer may be affected by your locale. For instance, you might in the future post questions which are related to your regional settings.

    With this in mind, please update your profile to something more precise (something such as UK, Europe, USA, UAE, etc. will suffice).

    Thank you for helping us to help you.


    Which version of Excel are you using? If Excel 2016, then please make this clear in your profile. Thanks.
    Last edited by AliGW; 09-21-2021 at 11:35 AM.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

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

    Re: InStr not working properly.

    It appears to me that the Instr() functions are working just fine -- returning the appropriate position number for the in string search. The problem appears to be in the conversion from integer/long to boolean. From VBA's help file for the boolean data type
    Quote Originally Posted by VBA help
    Boolean values are not stored as numbers, and the stored values are not intended to be equivalent to numbers. You should never write code that relies on equivalent numeric values for True and False. Whenever possible, you should restrict usage of Boolean variables to the logical values for which they are designed. -- https://docs.microsoft.com/en-us/dot...lean-data-type
    The problem seems to be that the conversion from integer/long to boolean coupled with the And operator causes two non-zero integers to return a False boolean result (I couldn't find anything to explain why, but that seems to be the problem).

    As suggested by the help file, the solution would be to not trust VBA's built in conversion from numeric integer/long to boolean and write the code so that the boolean test/conversion is explicit. I would expect something like
    Please Login or Register  to view this content.
    so that VBA cannot misinterpret the boolean logic you want.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,009

    Re: InStr not working properly.

    And is a bitwise operator, so unless there are bits that are 1 in the same position in both numbers, you’ll end up with 0 as a result, and thus False when converted to Boolean
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,543

    Re: InStr not working properly.

    In the Immediate window, you have a visualization of what rorya says.
    Please Login or Register  to view this content.
    Artik

  6. #6
    Registered User
    Join Date
    10-10-2017
    Location
    UK
    MS-Off Ver
    365
    Posts
    28

    Re: InStr not working properly.

    Quote Originally Posted by MrShorty View Post
    Please Login or Register  to view this content.
    so that VBA cannot misinterpret the boolean logic you want.
    This worked great. Thank you.

+ 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. If statement - unable to work out why it doesn't work
    By vba_newbie83 in forum Excel General
    Replies: 3
    Last Post: 11-25-2019, 09:26 AM
  2. Testing single code, work fine. Put multiple codes in one sheet, one code doesn't work.
    By MayDay1988 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-30-2017, 06:14 PM
  3. [SOLVED] F4 doesn't work only in excel, how to make it work again?
    By Dave H in forum Excel General
    Replies: 1
    Last Post: 02-26-2014, 11:22 AM
  4. [SOLVED] Macro work in personal workbook, doesn't work in other workbooks
    By Centexcel in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-30-2013, 11:47 AM
  5. [SOLVED] Macro Doesn't Work Through Button, Does Work Through Developer ->Macros Option
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-27-2013, 11:55 AM
  6. VBA sum - doesn't work with only one value.
    By Bevy in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-21-2006, 09:15 AM
  7. [SOLVED] Anyone know why this doesn't work
    By BT in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-28-2005, 05:05 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