+ Reply to Thread
Results 1 to 7 of 7

Why isn't this if statement functioning correctly?!?

  1. #1
    Registered User
    Join Date
    07-08-2011
    Location
    Cleveland, OH
    MS-Off Ver
    Excel 2003
    Posts
    17

    Why isn't this if statement functioning correctly?!?

    Alright so with these hypothetical values:
    ActiveCell.Value = 0.2
    LowerPt = -.1
    UpperPt = .1
    OldLow1 = -.005
    OldHigh1 = .011

    What should happen when I run the code pasted below is that the program should print
    "Scrap" in the specified cell, and then ignore the other If statements because the values don't satisfy it.
    But what really happens when I run it is that It prints scrap, then skips the second if statement, then runs through the third one, which makes no sense whatsoever.
    I've looked for explanations on the forum but couldn't find anything similar, so I'm here again asking for a little guidance, (I know active doesn't need to be declared, but I was trying everything to get this little piece of the code to work). Anyone have any ideas?
    Thanks!
    Please Login or Register  to view this content.
    Last edited by rmar2011; 07-29-2011 at 07:32 AM.

  2. #2
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Why isn't this if statement functioning correctly?!?

    Maybe something like this is what your after?

    Please Login or Register  to view this content.
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

  3. #3
    Registered User
    Join Date
    07-08-2011
    Location
    Cleveland, OH
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Why isn't this if statement functioning correctly?!?

    I was wondering how you did an elseif statement in excel... but the problem is when I replace the code with this it ends up just running through the first "scrap" statement every time which is really weird

  4. #4
    Registered User
    Join Date
    07-08-2011
    Location
    Cleveland, OH
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Why isn't this if statement functioning correctly?!?

    Could it have to do with how my variables are declared maybe? When I run it through the debugger step by step one value shows up as .2 and the other shows up in quotations like "-.1" for example.

  5. #5
    Registered User
    Join Date
    07-08-2011
    Location
    Cleveland, OH
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Why isn't this if statement functioning correctly?!?

    still looking for an explanation to this... if anyone can relate

  6. #6
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Why isn't this if statement functioning correctly?!?

    Yes, if the value the variable is holding is a string rather than a double, then that will make a big difference. The < and > operators can perform string comparisons as well as numerical comparisons. To understand the rules on this, have a read through the "Comparison Operators" topic in your VBA helpfile. What I suspect is happening is that this rule is being enforced:
    Quote Originally Posted by VBA Helpfile
    IF One expression is a String and the other is any Variant except a Null THEN Perform a string comparison.
    Change your variable declarations accordingly.
    Hope that helps,

    Colin

    RAD Excel Blog

  7. #7
    Registered User
    Join Date
    07-08-2011
    Location
    Cleveland, OH
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Why isn't this if statement functioning correctly?!?

    YES. I figured it was something like that... :/ Thanks for the help, Colin

+ 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