VBA NOT

What is VBA Not?

In Visual Basic for Applications (VBA), the Not keyword is used as a logical operator to perform a negation operation. It is a keyword that can be used to reverse the logical value of an expression.

For further clarity, see the example below.

VBA Not - Definition Example - 1

Here, a subroutine is created to check if the two values are greater or not. The “Not” function is used to check if the values are NOT greater or NOT lesser. This will return True and False, respectively in the Immediate tab.

VBA Not - Definition Example - 2
Key Takeaways
  • The VBA Not operator is used to negate the logical value of a Boolean expression.
  • It can be applied to Boolean variables or used in conjunction with other logical operators to create complex conditions.
  • Not is a unary operator, meaning it operates on a single operand, usually a Boolean value.
  • It is commonly used in logical expressions to create conditions that check for the opposite state of a given condition.

How to Use Excel VBA Not Function?

To learn how to use VBA Not function effectively, follow along with the steps explained below.

  1. In Excel, select “Developer” from the toolbar. In the “Developer” section, select “Visual Basic.” It opens the VBA Editor.


    How to use Excel VBA Not Function - Step 1a





    To code in VBA, go to the “Insert” option in the VBA Editor and select “Module.” You can follow the code explained in steps so that you can implement it as you read.
     

  2. Start by declaring the name of your subroutine and define a Boolean variable.


    How to use Excel VBA Not Function - Step 2

  3. Initialize it as “True”.


    How to use Excel VBA Not Function - Step 3

  4. Print the boolean value in a message box.


    How to use Excel VBA Not Function - Step 4

    “vbInformation” is a constant variable that is used to depict the “Information” icon on the message box.

  5. Negate the given boolean value given earlier by using the Not function.


    How to use Excel VBA Not Function - Step 5

  6. Print the current value in a message box.


    How to use Excel VBA Not Function - Step 6

    Code:

    Sub ToggleBooleanExample()
    Dim myBoolean As Boolean
    myBoolean = True
    MsgBox “Initial value: ” & myBoolean, vbInformation
    myBoolean = Not myBoolean
    MsgBox “Updated value: ” & myBoolean, vbInformation
    End Sub

  7. Run the subroutine by pressing “F5” or by clicking the green arrow button on the VBA Editor toolbar and get the result in a message box.


    How to use Excel VBA Not Function - Step 7a



Now you know how to use VBA Not function for your purposes. Follow down to try out some more interesting examples.

Examples

Let us see exciting ways to use the VBA Not function in different ways.

Example #1

See how you can use the VBA Not function to compare and contrast different cases. It can not only be used for VBA Boolean expressions but also for checking conditions.

  • Step 1: Begin declaring a subroutine.
VBA Not in Excel - Example 1 - Step 1
  • Step 2: Declare and initialize multiple variables as shown.
Example 1 - Step 2

You can use colons to declare multiple variables in a single line.

  • Step 3: Check if a is less than b.
Example 1 - Step 3

Print the result in the Immediate window using the “Debug.Print” function.

  • Step 4: Similarly, compare b and c. Keep in mind that ‘c’ is declared as Null. It will return a Null value irrespective of the function used. Print the result similarly.
Example 1 - Step 4
  • Step 5: Check if d is less than a. Use the Not function to check if d is not greater than a.
Example 1 - Step 5

Print the result in the Immediate tab.

  • Step 6: Use the Not function with any given variable (not the Null variable).
Example 1 - Step 6

It will perform bitwise comparison instead of negating the value. Print the bitwise comparison value in the immediate tab.

Code:

Sub NOT_Function_For_Many_Cases()
a = 10: b = 12: c = Null: d = 14
chek = Not (a > b)
Debug.Print chek
chek = Not (b > c)
Debug.Print chek
chek = Not (d > a)
Debug.Print chek
chek = Not b
Debug.Print chek
End Sub

  • Step 7: Press the Run button. It prints the responses in the Immediate tab, as shown below.
VBA Not in Excel - Example 1 - Step 7

Example #2

In this example, you write a subroutine that accepts a Boolean input from the user (True or False). You can get the inverse of the Boolean function using the VBA Not Function and check if it’s true/false based on the user’s input. It can be a fun game/activity.

  • Step 1: You can start by first declaring a subroutine to get the inverse of the boolean value given the input.
VBA Not in Excel - Example 2 - Step 1
  • Step 2: Define a boolean variable to accept from the user.
Example 2 - Step 2
  • Step 3: Get the input as True/False from the user using the VBA InputBox function.
Example 2 - Step 3

You can define the title of the InputBox by declaring it after the initial statement, as shown above.

  • Step 4: Convert the input from the user as a Boolean variable using the CBool function.
Example 2 - Step 4

If the condition is “True,” the If statement is executed, and the output is printed in a message box.

  • Step 5: If the above statement is “False,” then the Else block is defined to be executed in that case.
Example 2 - Step 5

End the If-Else condition in VBA block for this condition.

  • Step 6: Declare another If-Else statement block to check if the condition is “True” after inverting the boolean condition using the Not function. For example, Not True will return the result as “False”.
Example 2 - Step 6

The result is then printed in a Message Box function.

  • Step 7: Define an Else block in case the If statement is false. After using the Not function, if the given condition is not True, print a message box in VBA stating that the condition is False.
Example 2 - Step 7

Code:

Sub ToggleConditionExample()
Dim condition As Boolean
condition = InputBox(“Enter True or False”, “Is it True/False”)
If CBool(condition) Then
MsgBox “Condition is True”, vbInformation
Else
MsgBox “Condition is False”, vbInformation
End If
If CBool(Not condition) Then
MsgBox “Condition is True after toggling”, vbInformation
Else
MsgBox “Condition is False after toggling”, vbInformation
End If
End Sub

  • Step 8: Run the above subroutine to view the output. Type “true.”
VBA Not in Excel - Example 2 - Step 8a
VBA Not in Excel - Example 2 - Step 8b

After clicking ok, you get

VBA Not in Excel - Example 2 - Step 8c

Example #3

Suppose you want to create a UserForm to check if a number is greater than three or not. You can customize your VBA UserForm to do so.

  • Step 1: Insert a new UserForm by clicking the “UserForm” in the dropdown of the “Insert” option.
Example 3 - Step 1
  • Step 2: Create a blank UserForm and customize it in the “Properties” section.
Example 3 - Step 2a
Excel VBA Not - Example 3 - Step 2b

It is how it currently looks.

  • Step 3: Add a text box to the UserForm and customize its properties.
Example 3 - Step 3a
Example 3 - Step 3b

Customize the font size by clicking the box with 3 dots “…” next to the “font” property.

Example 3 - Step 3c

Customize the font looks as you please.

Example 3 - Step 3d
  • Step 4: Create a command button and customize its properties.
Example 3 - Step 4a
Example 3 - Step 4b

Now the UserForm looks like this.

Excel VBA Not - Example 3 - Step 4c
  • Step 5: Double-click on the command button in order to write your code.
Example 3 - Step 5

This will create a new private subroutine, which will execute if you click the button defined in the earlier.

  • Step 6: Define a boolean variable.
Example 3 - Step 6
  • Step 7: Initialize the condition by checking if the given text box value is greater than 3.
Example 3 - Step 7

This will either return as True or False. The text box is the text box defined earlier and its value can be used using the VBA Value function.

  • Step 8: Check if the condition is false using the VBA Not function.
Example 3 - Step 8

Using an If-Else condition block, use the VBA Not function in combination with the If statement to check if it is “False”.

Print a MsgBox function showing that the If block has been executed.

  • Step 9: If the condition is not “False” declare an Else statement to be executed otherwise.
Example 3 - Step 9

End the If-Else statement block.

Example 3 - Step 10

This closes the UserForm after the If-Else statement block is executed in the button click subroutine.

Code:

Private Sub Btn_NOT_Click()
Dim condition As Boolean
condition = (TextBox1.Value > 3)
If Not condition Then
MsgBox “Condition is False”
Else
MsgBox “Condition is True”
End If
Unload UserForm1
End Sub

  • Step 11: Run the UserForm and see the results.
Excel VBA Not - Example 3 - Step 11a
Excel VBA Not - Example 3 - Step 11b

Otherwise,

Excel VBA Not - Example 3 - Step 11c
Excel VBA Not - Example 3 - Step 11d

Important Things to Note

  • Use Not with Boolean values to negate their logical state.
  • Apply Not in If or While statements to check the opposite condition.
  • Do not use Not directly with non-Boolean VBA data types. It will perform a bitwise operation.
  • Use parentheses for clarity when combining Not with other logical operators to ensure correct evaluation.

Frequently Asked Questions (FAQs)

1. Why is VBA Not Function not working?

There may be many reasons why the VBA Not function is not working properly. They can be due to:
Incorrect Syntax.
Improper usage of Not in logical expressions.
Incompatibility with other data types.

2. Can I use Not with non-boolean data types in VBA?

Yes, you can use Not with non-boolean data types, but it works only with integers. VBA Not function when used with a singular integer will turn into a bitwise comparison as shown.

Excel VBA Not - FAQ 2 - 1

This will print the bitwise comparison in the Immediate tab.

Excel VBA Not - FAQ 2 - 2

3. How do I use Not in conjunction with other logical operators?

You can combine Not with other logical operators for negation.
• Example: If Not (x > 5 And y < 10).
Use parentheses for clarity and correct evaluation.

4. Can I use Not with comparison operators in VBA?

Yes, Not can be used with comparison operators in VBA.
• Example: If Not (x = 5) or If Not (y <> 10).
Negates the result of the comparison.

This has been a guide to VBA NOT. Here we learn how to use the NOT function in Excel VBA code, along with step-by-step examples & points to remember. You can learn more from the following articles –

Reader Interactions

Leave a Reply

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