locked
VBA IF OR statement not executing correctly RRS feed

  • Question

  • Hi All,

    In my code i am evaluating a cell value with below IF OR statement

    Sub testes()
    If ActiveCell.Value <> "NY" Or ActiveCell.Value <> "NJ" Then
    
    MsgBox 1
    Else
    MsgBox 2
    End If

    Issue is even if cell= "NJ", code is giving 1 and not 2 as it should be.

    Thanks,

    Zav

    Wednesday, September 16, 2015 8:20 PM

Answers

  • With an OR if either side is true then the test is met.  Since the cell contains "NJ" the first test, <> NY, is met so the result is true. I think you probably want an AND instead.

    Jim

    • Marked as answer by zaveri cc Thursday, September 17, 2015 1:50 AM
    Wednesday, September 16, 2015 8:41 PM

All replies

  • With an OR if either side is true then the test is met.  Since the cell contains "NJ" the first test, <> NY, is met so the result is true. I think you probably want an AND instead.

    Jim

    • Marked as answer by zaveri cc Thursday, September 17, 2015 1:50 AM
    Wednesday, September 16, 2015 8:41 PM
  • Thanks, It worked.
    Thursday, September 17, 2015 1:50 AM