+ Reply to Thread
Results 1 to 6 of 6

If Then condition is False but code executes anyways

  1. #1
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    If Then condition is False but code executes anyways

    I have an If Then statement that's been working as it should, but all of sudden I'm having an issue with the condition.
    I'm not really sure why this is running the code when FindRng is Nothing. I've debugged and an error is thrown. When I take the debug out, the If Then is True. How is this even possible? I've heard that Find can be buggy sometimes.
    Please Login or Register  to view this content.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,833

    Re: If Then condition is False but code executes anyways

    Try:
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: If Then condition is False but code executes anyways

    I should have clarified that the first If Then works correctly. The second If Then condition is False since 'blah' is not within the range being searched. VBA see's FindRng as Nothing, but then continues executing the code within the second If Then statement instead of seeing False and moving out of the condition. I've set the FindRng as an object. Do I need to review this? If I try and use <> "" on the second If Then condiiton, it doesn't like that which tells me something is different about FindRng vs. blah in regards to what type of object they are. It's been working fine up to a point, but all of a sudden decided to "ignore" the condition.

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,833

    Re: If Then condition is False but code executes anyways

    They should all be defined as "range" which they are. Can you post a copy of your file (de-sensitized if necessary) and explain in detail what you are trying to do referring to specific cells, rows, columns and sheets? See the yellow banner at the top of this page for instructions to attach a file.

  5. #5
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: If Then condition is False but code executes anyways

    I can't how unbelievably stupid I am. Too much overthinking of this. I just didn't have the Not in the second If Then statement. Somehow I must have removed it when fiddling with the code. Thanks for letting me bounce something off you.
    Please Login or Register  to view this content.
    Last edited by terriertrip; 10-30-2020 at 07:23 PM.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: If Then condition is False but code executes anyways

    This is not correct:

    Please Login or Register  to view this content.
    You're trying to set an object using a data value. It should be:

    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

+ 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. Code Executes Before Add-In Data Updates
    By OzTrekker in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-19-2014, 02:16 AM
  2. Code executes on one IF condition but ignores the other (Nested IF Issue)
    By ebb1320 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-01-2013, 07:04 PM
  3. Excel 2007 Macro that Executes Action Periodically Until Condition is Met
    By jmr.noun in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-25-2012, 03:41 AM
  4. MsgBox does not appear after the code executes.
    By contaminated in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-05-2010, 07:49 AM
  5. Code only executes correctly when run from certain worksheet
    By jbguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-18-2009, 05:12 AM
  6. Vba Code Executes After Copying Sheet
    By RichG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2007, 09:09 PM
  7. Condition code True False on Checkbox
    By RobertCo in forum Excel General
    Replies: 2
    Last Post: 12-16-2006, 07:47 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