• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
EDUCBA

EDUCBA

MENUMENU
  • Resources
        • Excel Charts

          • Histogram Chart Excel
          • Basic Excel Formulas
          • Text to Columns in Excel
        • Excel Charts
        • Excel Tips

          • Excel Gantt Chart
          • IFERROR with VLOOKUP
          • Data Table in Excel
        • Excel Tips
        • Excel Tools in Excel

          • Stacked Column Chart
          • Cheat Sheet of Excel Formulas
          • Excel Data Validation
        • Histogram chart in excel
        • Others

          • Resources (A-Z)
          • Excel Functions
          • Financial Functions in Excel
          • Logical Functions in Excel
          • Lookup Reference Functions in Excel
          • Maths Function in Excel
          • TEXT and String Functions in Excel
          • View All
  • Free Courses
  • All Courses
        • Certification Courses

          Excel Course 1
        • All in One Bundle

          All-in-One-Excel-VBA-Bundle
        • Excel course

          Excel-Training
        • Others

          • Excel advanced course
          • VBA Course
          • Excel Data Analysis Course
          • Excel for Marketing Course
          • Excel for Finance Course
          • View All
  • 120+ Courses All in One Bundle
  • Login

VBA Not

Home » Excel » Blog » VBA » VBA Not

VBA Not

Excel VBA Not

VBA Not is a logical function. NOT is one of the logical functions among others such as VBA IF, VBA OR and VBA AND. All these functions work in the same logical concept but all have different applications. Where VBA Not works mainly on Boolean. Which means we will get the output in the form of TRUE and FALSE. And VBA Not is the opposite of the input we feed. Suppose, we want to compare 2 parameters such as temperature. The temperature of places A and B are 30˚C and we use VBA Not to compare the answer then we will definitely get FALSE, as VBA Not means No or No Equal. Opposite to this, if the temperature of place A is 30˚C and temperature of the place B is 35˚C then using VBA Not here will give us the answer as TRUE as both the values are not equal.

How to Use Not Function in Excel VBA?

Below are the different examples to use Not function in Excel VBA.

Start Your Free Excel Course

Excel functions, formula, charts, formatting creating excel dashboard & others

You can download this VBA Not Excel Template here – VBA Not Excel Template

Example #1

In this example, we will compare 2 numbers and see what type of output we would get. To execute this, follow the below steps:

Step 1: Open a Module from the Insert menu tab as shown below.

VBA Not Example 1-1

Step 2: Now write the subprocedure in the name of VBA Not or in any other name as per your choice as shown below.

Code:

Sub VBA_Not()

End Sub

VBA Not Example 1-2

Step 3: For numbers, we used to use Integer but as here we will be using NOT with numbers, so we will use String data type.

Popular Course in this category
Cyber Week Sale
VBA Training (3 Courses, 12+ Projects) 3 Online Courses | 13 Hands-on Projects | 45+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.7 (2,747 ratings)
Course Price

View Course

Related Courses
Excel Advanced Training (14 Courses, 21+ Projects)Excel Data Analysis Training (12 Courses, 6+ Projects)

Code:

Sub VBA_Not()

Dim A As String

End Sub

VBA Not Example 1-3

Step 4: Select the numbers which we want to compare. Here we will be comparing 10 and 20 and see if Number 20 is greater than 10 or not using Not function as shown below.

Code:

Sub VBA_Not()

Dim A As String
A = Not (10 < 20)

End Sub

VBA Not Example 1-4

Step 5: Now to print the output, we will use the message box as shown below.

Code:

Sub VBA_Not()

Dim A As String
A = Not (10 < 20)
MsgBox A

End Sub

VBA Not Example 1-5

Step 6: Run the code by pressing the F5 key or by clicking on the Play button. We will get the message as FALSE.

Output of Example 1-6

We all know that number 20 is greater than 10. But VBA Not is the logical function that always gives the negative answer or opposite answer from the value we feed. So, as per VBA Not, number 20 is NOT greater than 10.

Example #2

There is another way to implement VBA Not. This time we will use If-End If loop to execute VBA Not. For this, follow the below steps:

Step 1: Write the subprocedure of VBA Not as shown below.

Code:

Sub VBA_Not2()

End Sub

VBA Not Example 2-1

Step 2: Again we will use the variable as String here as well.

Code:

Sub VBA_Not2()

Dim A As String

End Sub

Declare Variable Example 2-2

Step 3: Here we will compare whether 2 numbers are equal or not. Let’s compare number 10 with 10 and see what type of answer we will get. For this, open If loop and in condition write if NOT 10 is equal to 10 as shown below.

Code:

Sub VBA_Not2()

Dim A As String
If Not (10 = 10) Then

End Sub

VBA Not Example 2-3

Step 4: If the above condition is satisfied then we will get the answer as TRUE.

Code:

Sub VBA_Not2()

Dim A As String
If Not (10 = 10) Then
A = "TRUE"

End Sub

VBA Not Example 2-4

Step 5: Else give us the answer as FALSE. After that close the loop by End-If.

Code:

Sub VBA_Not2()

Dim A As String
If Not (10 = 10) Then
A = "TRUE"
Else
A = "FALSE"
End If

End Sub

End-If Example 2-5

Step 6: To see the output, we will use a message box with variable A.

Code:

Sub VBA_Not2()

Dim A As String
If Not (10 = 10) Then
A = "TRUE"
Else
A = "FALSE"
End If
MsgBox A

End Sub

Use message box Example 2-6

Step 7: Run the code by pressing the F5 key or by clicking on the Play button. We will get the output message as FALSE.

Output of Example 2-7

Which means although number 10 is equal to 10 but since we used NOT so we are getting the opposite answer here.

Example #3

VBA Not can be used to compare the marks as well. Below we have 2 subjects rows under that a student got 61 in Subject1 and 2 in Subject2. And we will get the result in cell B3.

VBA Not Example 3-1

Follow the below steps:

Step 1: Write the subprocedure of VBA Not as shown below.

Code:

Sub VBA_Not3()

End Sub

VBA Not Example 3-2

Step 2: Define 2 variable for Subject1 and Subject2 using Integer and one of result using String data type as shown below.

Code:

Sub VBA_Not3()

Dim Subject1 As Integer
Dim Subject2 As Integer
Dim result As String

End Sub

VBA Not Example 3-3

Step 3: Now select the range cell which has the number for respective subjects as shown below.

Code:

Sub VBA_Not3()

Dim Subject1 As Integer
Dim Subject2 As Integer
Dim result As String
Subject1 = Range("B1").Value
Subject2 = Range("B2").Value

End Sub

VBA Not Example 3-4

Step 4: Now use If loop with VBA Not as marks in Subject1 is greater than equal to 70 and for subject2 is greater than 30 in If condition, then result is “Pass”.

Code:

Sub VBA_Not3()

Dim Subject1 As Integer
Dim Subject2 As Integer
Dim result As String
Subject1 = Range("B1").Value
Subject2 = Range("B2").Value

If Not (Subject1 >= 70 And Subject2 > 30) Then
result = "Pass"

End Sub

VBA Not Example 3-5

Step 5: Else the student is Fail.

Code:

Sub VBA_Not3()

Dim Subject1 As Integer
Dim Subject2 As Integer
Dim result As String
Subject1 = Range("B1").Value
Subject2 = Range("B2").Value

If Not (Subject1 >= 70 And Subject2 > 30) Then
result = "Pass"

Else
result = "Fail"
End If

End Sub

VBA Not Example 3-6

Step 6: At last, we will select cell B3 to get the result.

Code:

Sub VBA_Not3()

Dim Subject1 As Integer
Dim Subject2 As Integer
Dim result As String
Subject1 = Range("B1").Value
Subject2 = Range("B2").Value

If Not (Subject1 >= 70 And Subject2 > 30) Then
result = "Pass"

Else
result = "Fail"
End If

Range("B3").Value = result

End Sub

VBA Not Example 3-7

Step 7: Run the code by pressing the F5 key or by clicking on the Play button. We will get the result as PASS.

Output of Example 3-8

This means technically student is failed but we have used VBA Not in if the condition so the result is shown as Pass.

Pros of Excel VBA Not

  • It is rarely used but when we need the opposite or negative answer then we can use it here.
  • It is useful in comparing the negative outputs which may lead to giving a positive result.

Things to Remember

  • VBA Not gives the opposite answer to the value we feed. It can be positive or negative, but the answer will be the opposite of that value we give as input.
  • Always use VBA Not where we need to change the output opposite to the actual output.
  • Remember to save the file as macro enable excel format so that this will retain the code for future use as well.
  • Using VBA Not with If-Else loop will be more advisable.

Recommended Articles

This is a guide to VBA Not. Here we discuss how to use Not Function in Excel VBA along with practical examples and downloadable excel template. You may also look at the following articles to learn more –

  1. Examples of VBA IF Not
  2. VBA Variable Types (Excel Template)
  3. VBA ScreenUpdating Application
  4. How to Use Goal Seek in VBA?

All in One Excel VBA Bundle (120+ Courses, 30+ Projects)

120+ Online Courses

30+ Projects

500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Reader Interactions
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar
Excel Functions Tutorials
  • VBA
    • VBA Max
    • VBA Environ
    • VBA Not
    • VBA LOOKUP
    • Programming in Excel
    • VBA Name Worksheet
    • VBA ScreenUpdating
    • VBA Do Loop
    • VBA Declare Array
    • VBA Macros
    • VBA Delete Sheet
    • VBA Editor
    • VBA Error Handling
    • VBA Goal Seek
    • VBA DateDiff
    • VBA On Error Resume Next
    • VBA Examples
    • VBA Counter
    • VBA Val
    • VBA Me
    • VBA Double
    • VBA Variable Types
    • VBA FreeFile
    • VBA Switch Case
    • VBA Unprotect Sheet
    • VBA Format Number
    • VBA ReDim
    • VBA IF Statements
    • VBA DoEvents
    • VBA Public Variable
    • VBA Columns
    • VBA Cdate
    • VBA Selection Range
    • VBA Hide Columns
    • VBA COUNTA
    • VBA Insert Row
    • VBA Protect Sheet
    • VBA AutoFill
    • VBA IF Not
    • VBA GetObject
    • VBA Debug Print
    • VBA Collection
    • VBA Text
    • VBA Randomize
    • VBA Variant
    • VBA Selection
    • VBA Right
    • VBA Date Format
    • VBA Rename Sheet
    • VBA Paste Values
    • VBA InputBox
    • VBA Conditional Formatting
    • VBA Pause
    • VBA Pivot Table
    • VBA Message Box
    • VBA OFFSET
    • VBA ByRef
    • FileCopy in VBA
    • VBA StrConv
    • VBA SUB
    • VBA Boolean
    • VBA Global Variables
    • VBA Worksheets
    • VBA IIF
    • VBA Close UserForm
    • VBA Variable Declaration
    • VBA Join
    • VBA Dictionary
    • VBA Operators
    • VBA Charts
    • VBA For Each Loop
    • VBA FileDialog
    • VBA Format
    • VBA MID
    • VBA GetOpenFileName
    • VBA DatePart
    • VBA Code
    • VBA Option Explicit
    • VBA FileSystemObject (FSO)
    • VBA ASC
    • VBA UserForm
    • VBA Cells
    • VBA InStrRev
    • VBA Class Module
    • VBA Constants
    • VBA Length of String
    • VBA DateSerial
    • VBA StrComp
    • VBA Array Length
    • VBA Check File Exists
    • VBA Ubound
    • VBA CDBL
    • VBA Activate Sheet
    • VBA DateValue
    • VBA Borders
    • VBA Comment
    • VBA Intersect
    • VBA Workbook
    • VBA Concatenate
    • VBA Name
    • VBA Find and Replace
    • VBA Tutorial
    • VBA CSTR
    • VBA Save As
    • VBA Hyperlink
    • VBA Print
    • VBA Switch
    • VBA END
    • VBA Like
    • VBA Set
    • VBA excel programming
    • VBA Call Sub
    • VBA Object
    • VBA RoundUp
    • VBA ArrayList
    • VBA Named Range
    • VBA PowerPoint
    • VBA Block Comment
    • VBA OverFlow Error
    • VBA Insert Column
    • VBA Lcase
    • VBA List Box
    • VBA Delete File
    • VBA Clear Contents
    • VBA TextBox
    • VBA Font Color
    • VBA Range Cells
    • VBA INT
    • VBA UCASE
    • VBA Value
    • VBA Remove Duplicates
    • VBA Break for Loop
    • VBA Sleep
    • VBA Do Until Loop
    • VBA Union
    • VBA Long
    • VBA Copy Paste
    • VBA Data Types
    • VBA Delete Column
    • VBA Enum
    • VBA IsEmpty
    • VBA 1004 Error
    • VBA RegEx
    • VBA IsNumeric
    • VBA Paste
    • VBA Transpose
    • VBA Left
    • VBA Delete Row
    • VBA Integer
    • VBA Active Cell
    • VBA InStr
    • VBA Round
    • VBA Subscript out of Range
    • VBA Dim
    • VBA Replace
    • VBA Sort
    • VBA String
    • VBA Split
    • VBA Wait
    • VBA MOD
    • VBA Time
    • VBA TIMER
    • VBA While Loop
    • VBA Date
    • Excel VBA MsgBox
    • VBA IFError
    • VBA Color Index
    • VBA Match
    • VBA Case
    • VBA Arrays
    • VBA GoTo
    • VBA On Error
    • VBA Range
    • VBA Do While Loop
    • VBA Number Format
    • VBA Loops
    • VBA TRIM
    • VBA Find
    • VBA Select Case
    • VBA Else If
  • Excel Functions (10+)
  • Excel Tools (97+)
  • Financial Functions in Excel (17+)
  • Logical Functions in Excel (12+)
  • Lookup Reference Functions in Excel (30+)
  • Maths Function in Excel (39+)
  • TEXT and String Functions in Excel (25+)
  • Date and Time Function in Excel (20+)
  • Statistical Functions in Excel (55+)
  • Information Functions in Excel (4+)
  • Excel Charts (44+)
  • Excel Tips (195+)
  • Workplace Productivity (4+)
  • Microsoft Office Tools (14+)
  • Excel Formula and Functions (20+)
  • MS Excel Shortcuts (4+)
Excel Functions Courses
  • VBA Training
  • Excel Advanced Training
  • Excel Data Analysis Training
Footer
About Us
  • Who is EDUCBA?
  • Sign Up
  •  
Free Courses
  • Free Online Excel Course
  • Free Vba Course
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training
Resources
  • Resources (A To Z)
  • Excel Charts
  • Excel Tips
  • Excel Tools in Excel
  • Excel Functions
  • Financial Functions in Excel
  • Logical Functions in Excel
  • Lookup Reference Functions in Excel
  • Maths Function in Excel
  • TEXT and String Functions in Excel
  • Date and Time Function in Excel
  • Statistical Functions in Excel
  • Information Functions in Excel
Apps
  • iPhone & iPad
  • Android
Support
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions

© 2019 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

Download VBA Not Excel Template

By continuing above step, you agree to our Terms of Use and Privacy Policy.
EDUCBA
Free Excel Course

Excel functions, formula, charts, formatting creating excel dashboard & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Excel Course

Excel functions, formula, charts, formatting creating excel dashboard & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Excel Course

Excel functions, formula, charts, formatting creating excel dashboard & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Excel Course

Excel functions, formula, charts, formatting creating excel dashboard & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Let’s Get Started
Please provide your Email ID
Email ID is incorrect

Limited Period Offer - All in One Excel VBA Bundle (120+ Courses, 500+ hours of Videos) View More

Limited Period Offer - Limited Period Offer - All in One Excel VBA Bundle (120+ Courses, 500+ hours of Videos) View More