Not equals to sign in vba if statement

0

Hi everyone!

I'm trying to write a simple macro but cant figure out how to get the "not equals" to sign to work.

Here is a simple example of what I'm trying to do:

Sub not_equals()

If Range("A1").Value Is Not "Yes" Then

MsgBox "You must input Yes."

End If

End Sub

I also tried like this:

Sub not_equals()

If Range("A1").Value != "Yes" Then

MsgBox "You must input Yes."

End If

End Sub

Each one gives me errors... I'm sure it's something simple but please can anyone help? 

Answer
Discuss

Answers

0
Selected Answer

Instead of Is Not or != you need to use <>

<>  is the way to say "not equals to" in Excel vba.

So try this:

Sub not_equals()

If Range("A1").Value <> "Yes" Then

MsgBox "You must input Yes."

End If
 
End Sub
Discuss

Discussion

Thank you so much! Works perfectly!
ricky1 (rep: 2) Mar 21, '17 at 2:13 am
Add to Discussion

Answer the Question

You must create an account to use the forum. Create an Account or Login