VBA IsNull Function

VBA IsNull Function

Objective of VBA IsNull Function

VBA IsNull function is categorized as an Information function in VBA. It is a built-in function in MS Office Excel VBA. The VBA IsNull function checks specified Expression is Null or not. This function has one input parameter. It returns a Boolean value (True or False). If specified expression is null then it returns True, Otherwise it returns False. We can use this function in VBA and can’t use this function in Excel. We can use this VBA IsNull function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the IsNull function, where we can use this VBA IsNull function and real-time examples.

Syntax of VBA IsNull Function

The syntax of the VBA IsNull function is

IsNull(Expression)

The VBA IsNull function returns a Boolean value either True or False.

Parameters or Arguments

The IsNull function has one input parameter or argument.
Where
Expression: It is a required parameter. The expression is an argument that you want to evaluate.

Where we can apply or use VBA IsNull Function?

We can use this VBA IsNull function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Check an expression(“vbaf1”) is null or not

Here is a simple example of the VBA IsNull function. This below example checks the specified expression is null or not. The output of below macro is FALSE.

'Check an expression("vbaf1") is null or not
Sub VBA_IsNull_Function_Ex1()
    
    'Variable declaration
    Dim iExpression As String
    Dim sOutput As Boolean
    
    iExpression = "vbaf1"
    
    sOutput = IsNull(iExpression)
   
    'Display output message
    MsgBox "The expression is null or not : " & sOutput, vbInformation, "VBA IsNull Function"
    
End Sub

Output: Here is the screen shot of the first example output.
VBA IsNull Function

Example 2: Check an expression(12345) is null or not

Here is a simple example of the VBA IsNull function. This below example checks the specified expression is null or not. The output of below macro is FALSE.

'Check an expression(12345) is null or not
Sub VBA_IsNull_Function_Ex2()
    
    'Variable declaration
    Dim iExpression As Integer
    Dim sOutput As Boolean
    
    iExpression = 12345
    
    sOutput = IsNull(iExpression)
   
    'Display output message
    MsgBox "The expression is null or not : " & sOutput, vbInformation, "VBA IsNull Function"
    
End Sub

Output:Here is the screen shot of the second example output.
VBA IsNull Function

Example 3: Check an expression(unassigned variable) is null or not

Here is a simple example of the VBA IsNull function. This below example checks the specified expression is null or not. The output of below macro is FALSE.

'Check an expression(unassigned variable) is null or not
Sub VBA_IsNull_Function_Ex3()
    
    'Variable declaration
    Dim iExpression As Double
    Dim sOutput As Boolean
    
    sOutput = IsNull(iExpression)
       
    'Display output message
    MsgBox "The expression is null or not : " & sOutput, vbInformation, "VBA IsNull Function"
    
End Sub

Output:Here is the screen shot of the third example output.
VBA IsNull Function

Example 4: Check an expression(empty string) is null or not

Here is another example of the VBA IsNull function. This below example checks the specified expression is null or not. The output of below macro is FALSE.

'Check an expression(empty string) is null or not
Sub VBA_IsNull_Function_Ex4()
    
    'Variable declaration
    Dim iExpression As String
    Dim sOutput As Boolean
    
    iExpression = ""
    
    sOutput = IsNull(iExpression)
       
    'Display output message
    MsgBox "The expression is null or not : " & sOutput, vbInformation, "VBA IsNull Function"
    
End Sub

Output:Here is the screen shot of the fourth example output.
VBA IsNull Function

Example 5: Check an expression(Null) is null or not

Here is one more example of the VBA IsNull function. This below example checks the specified expression is null or not. The output of below macro is TRUE.

'Check an expression(Null) is null or not
Sub VBA_IsNull_Function_Ex5()
    
    'Variable declaration
    Dim iExpression
    Dim sOutput As Boolean
    
    iExpression = Null
    
    sOutput = IsNull(iExpression)
       
    'Display output message
    MsgBox "The expression is null or not : " & sOutput, vbInformation, "VBA IsNull Function"
    
End Sub

Output:Here is the screen shot of the fifth example output.
VBA IsNull Function

Instructions to use Macro Codes

Here are the step by step instructions to use the VBA IsNull function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

VBA Information Functions List of VBA Functions

Leave a Comment

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