Results 1 to 5 of 5

Thread: Syntax

  1. #1
    Bronze Lounger
    Join Date
    Sep 2007
    Posts
    1,203
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    If Not Fnd = 0 Or Fnd > 20 Then

    I'm not getting the expected results for the above. I want to do one thing if FND is >0 but <21, and something else if =0 or >20.

    Is there something wrong with the syntax?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Try

    If Not (Fnd = 0 Or Fnd > 20) Then

  3. #3

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    "Not" has precedence above "Or", so

    If Not Fnd = 0 Or Fnd > 20 Then

    is equivalent to

    If (Not Fnd = 0) Or Fnd > 20 Then

    and this is equivalent to

    If Fnd <> 0 Then

    To make "Not" apply to both parts of the "Or" clause, you must put them between parentheses"

    If Not (Fnd = 0 Or Fnd > 20) Then

    Of course, you could also have reversed the condition:

    If Fnd = 0 Or Fnd > 20 Then
    ...
    Else
    ...
    End If

  5. #5
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    Perhaps it would be clearer to just use:
    Code:
    If Fnd > 0 And Fnd < 21 Then
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •