Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"And" and "Or" troubles within an IF statement

Tags:

excel

vba

I'm trying to use "And" & "Or" within an If statement. I probably have my syntax wrong.

the result comes back false when the data should make it true. Here is the code:

ElseIf (origNum = "006260006" Or origNum = "30062600006") And creditOrDebit = "D" Then

'do things here

End If

-When I debug and come to this line it hops over it and doesn't enter in.

-origNum actually equals "006260006" and creditOrDebit = "D".

-so I'm assuming my "Or" statement isn't working.

-Hopefully this is a quick easy question. Thanks!

like image 423
Mike Kellogg Avatar asked Jun 20 '12 15:06

Mike Kellogg


1 Answers

The problem is probably somewhere else. Try this code for example:

Sub test()

  origNum = "006260006"
  creditOrDebit = "D"

  If (origNum = "006260006" Or origNum = "30062600006") And creditOrDebit = "D" Then
    MsgBox "OK"
  End If

End Sub

And you will see that your Or works as expected. Are you sure that your ElseIf statement is executed (it will not be executed if any of the if/elseif before is true)?

like image 165
assylias Avatar answered Sep 22 '22 09:09

assylias