WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Free Tutorials
  • Certification Courses
  • Excel VBA All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA IF NOT

VBA IF NOT

IF NOT in VBA

Logical functions are useful for the calculations which require multiple conditions or criteria to test. In our earlier articles, we have seen “VBA IF,” “VBA OR,” and “VBA AND” conditions. In this article, we will discuss the “VBA IF NOT” function. Before introducing VBA IF NOT function, let me show you about VBA NOT function first.

What is NOT Function in VBA?

“NOT” is one of the logical functions we have with excel & VBA. All the logical functions require logical tests to be conducted and return TRUE if the logical test is correct. If the logical test is not correct, then it will return FALSE as a result.

But “VBA NOT” is completely opposite of the other logical function. I would say this is the inverse function of logical functions.

“VBA NOT” function returns “FALSE” if the logical test is correct and if the logical test is not correct, it will return “TRUE.” Now, look at the syntax of the “VBA NOT” function.

NOT(Logical Test)

This is very simple, we need to provide the logical test. NOT function evaluates the test and returns the result.

VBA IF NOT

Examples of NOT & IF Function in VBA?

Below are the examples of using IF and NOT function in excel VBA.

You can download this VBA IF NOT Excel Template here – VBA IF NOT Excel Template

Example #1

Take a look at the below code for an example.

Code:

Sub NOT_Example()

  Dim k As String

  k = Not (100 = 100)

  MsgBox k

End Sub

In the above code, I have declared the variable as String.

Popular Course in this category
Sale
All in One Excel VBA Bundle (35 Courses with Projects)
4.9 (1,353 ratings)
35+ Courses | 120+ Hours | Full Lifetime Access | Certificate of Completion
View Course
Dim k As String

Then for this variable, I have assigned the NOT function with the logical test as 100 = 100.

k = Not (100 = 100)

Then I have written the code to show the result in the VBA message box. MsgBox k

Now I will execute the code and see the result.

VBA IF NOT Example 1

We got the result as “FALSE.”

Now look back at the logical test. We have provided the logical test as 100 = 100, which is generally TRUE since we had given the NOT function, we got the result as FALSE. As I told, in the beginning, it gives inverse results compared to other logical functions. Since 100 is equal to 100, it has returned the result as FALSE.

Example #2

Now. Weok at one more example with different numbers.

Code:

Sub NOT_Example()

 Dim k As String

 k = Not (85 = 148)

 MsgBox k

End Sub

The code is the same only thing I have changed here is I have changed the logical test from 100 = 100 to 85 = 148.

Now I will run the code and see what the result is.

Example 2

This time we got the result as TRUE. Now examine the logical test.

k = Not (85 = 148)

We all know 85 is not equal to the number 148. Since it is not equal, the NOT function has returned the result as TRUE.

NOT with IF Condition:

In excel or VBA, any logical conditions are not complete without the combination IF condition. Using IF in excel condition, we can do many more things beyond default TRUE or FALSE. For example, in the above examples, we got default results of FALSE & TRUE; instead of that, we can modify the result in our own words.

Look at the below code.

Code:

Sub NOT_Example2()

  Dim Number1 As String
  Dim Number2 As String

  Number1 = 100
  Number2 = 100

  If Not (Number1 = Number2) Then
   MsgBox "Number 1 is not equal to Number 2"
  Else
   MsgBox "Number 1 is equal to Number 2"
  End If

End Sub

I have declared two variables.

Dim Number1 As String & Dim Number2 As String

For these two variables, I have assigned the numbers as 100 & 100, respectively.

Number1 = 100 & Number2 = 100

Then for the NOT function, I have attached the IF condition to alter the default TRUE or FALSE. If the result of the NOT function is TRUE, then my result will be as follows.

MsgBox “Number 1 is not equal to Number 2.”

If the NOT function result is FALSE, then my result is as follows.

MsgBox “Number 1 is equal to Number 2.”

Now I will run the code and see what happens.

VBA IF NOT Example 3

We got the result as “Number 1 is equal to Number 2”, so NOT function has returned the FALSE result to the IF condition, so the IF condition returned this result.

Like this, we can use the IF condition to do the inverse test.

Recommended Articles

This has been a guide to VBA IF NOT. Here we discuss how to use the IF & NOT function in excel VBA along with examples and downloadable excel templates. Below are some useful articles related to VBA –

  • VBA Replace String
  • VBA If Else Statement
  • VBA AND Function
  • IF OR in VBA
0 Shares
Share
Tweet
Share
All in One Excel VBA Bundle (35 Courses with Projects)
  • 35+ Courses
  • 120+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

WallStreetMojo

Download VBA IF NOT Excel Template

New Year Offer - All in One Financial Analyst Bundle (250+ Courses, 40+ Projects) View More