Tuesday, October 11, 2022

What is Null in Excel VBA

To understand keyword or value Null we must understand what is variables and how to declare variables. Below examples how we declare variables with Dim statement.

Dim StrName  - Declare as open data type default as Variant

Dim StrName as String -  Declare as String data type (0 to approximately 2 billion)

Dim IntID as integer -  Declare as Integer data type (-32,768 to 32,767)

When dealing with no valid data,unknown or empty data we use Null as value and this Null value only can be used when variables declare as variant (Same range as for variable-length String).

For example:

Dim myVar  OR Dim myVar as Variant, then only we can assigned myVar = Null.
If we declare other than these 2 it will trigger error Run-time error '94': Invalid use of Null.

VBA Vode:

Option Explicit
Sub StatementNull()

    Dim myVar1
    Dim myVar2 As Variant
    
    myVar1 = Null
    myVar2 = Null
    
    Debug.Print myVar1
    Debug.Print myVar2

End Sub

Microsoft Reference-Null
Other Reference - Null

Practice makes perfect. Thank You.

macro enabled excel
excel macro
vba coding
vba code

No comments:

Post a Comment