Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

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