Excel VBA Not Function

Logical functions are useful for calculations that require multiple conditions or criteria to test. In our earlier articles, we have seen “VBA IF,” “VBA ORVBA OROr is a logical function in programming languages, and we have an OR function in VBA. The result given by this function is either true or false. It is used for two or many conditions together and provides true result when either of the conditions is returned true.read more,” and “VBA ANDVBA ANDVBA AND is an inbuilt logical function and a logical operator. Only when all of the conditions specified in this function are met , it returns the output as true. In contrast, if any of the conditions fails, the output is false.read more” conditions. This article will discuss the VBA NOT with the IF function in excelIF Function In ExcelIF function in Excel evaluates whether a given condition is met and returns a value depending on whether the result is “true” or “false”. It is a conditional function of Excel, which returns the result based on the fulfillment or non-fulfillment of the given criteria. read more. But, first, we need to look at the VBA NOT function to understand it.

You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA Not Function (wallstreetmojo.com)

Examples

Example #1

The NOT function is available with VBA too. It works the same as the Excel function. For example, look at the below set of VBA codesVBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more.

Code:

Sub NOT_Example1()

Dim k As String

k = Not (45 = 45)

MsgBox k

End Sub

We have declared the variable “k” as a string in the above code.

Dim k As String

Next, we have assigned the value through the NOT functionNOT FunctionNOT Excel function is a logical function in Excel that is also known as a negation function and it negates the value returned by a function or the value returned by another logical function.read more. Does it NOT function to say whether the number 45 equals 45 or not?

k = Not (45 = 45)

Next, we have assigned the value returned by the NOT function to the variable “k” in the message box.

MsgBox k

Run the code and see what the result is.

Example #2 – NOT with IF Function

As we said in one of the earlier articles, “IF with other logical functions are the best pairs in Excel.”

Similarly, NOT with IF is useful in many ways. For example, with IF, we can have our results instead of the default results of TRUE or FALSE.

Take the same example code above. Again, we will apply NOT with the IF function.

Sub NOT_Example2()

Dim k As String

If Not (45 = 45) Then k = “Test result is TRUE” Else k = “Test result is FALSE” End If

MsgBox k

End Sub

In the above code, we have altered the default results from “Test result is FALSE” and “Test result is TRUE.” If the supplied logical test is true, it will return “Test result is FALSE,” and if the supplied logical test is false, it will return. “Test result is TRUE.”

In the above code, we have a value of 45 = 45, so we will get the answer as follows.

Example #3 – Advanced NOT

The NOT function is utilized best with the IF function. For example, we can use this function to hide all the sheets except one particular sheet.

We have various sheets, as follows in our Excel.

Below is the sample code to hide all sheets except one particular sheet.

Sub NOT_Example3()

Dim Ws As Worksheet

For Each Ws In ActiveWorkbook.Worksheets If Not (Ws.Name = “Data Sheet”) Then Ws.Visible = xlSheetVeryHideen End If Next Ws

End Sub

The above code hides all the worksheets except the worksheet “Data Sheet.”

You can use this VBA code to hideVBA Code To HideVBA columns need to be hidden for the sheet to convey only the relevant information. To hide a column, simply set the property value to ‘TRUE’; to unhide the column, set the property value to ‘FALSE’.read more all the sheets except one particular sheet by changing the sheet name to your sheet name.

For how we can also unhide sheets in excelUnhide Sheets In ExcelThere are different methods to Unhide Sheets in Excel as per the need to unhide all, all except one, multiple, or a particular worksheet. You can use Right Click, Excel Shortcut Key, or write a VBA code in Excel. read more. The below code will unhide all the sheets except the sheet name “Data Sheet.”

Sub NOT_Example4()

Dim Ws As Worksheet

For Each Ws In ActiveWorkbook.Worksheets

If Not (Ws.Name = “Data Sheet”) Then Ws.Visible = xlSheetVisible

End If Next Ws

End Sub

The below code will unhide only the sheet name “Data Sheet.”

Sub NOT_Example3()

Dim Ws As Worksheet

For Each Ws In ActiveWorkbook.Worksheets If Not (Ws.Name <> “Data Sheet”) Then Ws.Visible = xlSheetVisible End If Next Ws

End Sub

This article has been a guide to VBA Not Function. Here, we discuss the working of the Not function, how to use it with the IF function, and practical examples and downloadable templates. Below are some useful articles related to VBA: –

  • VBA If ElseIF NOT in VBAFor Each Loop in Excel VBAGoTo in VBA