Thursday, October 27, 2022

VBA Function IsEmpty

IsEmpty function is to check whether a variable has been initialized or assigned any value or not. The result is Boolean either True or False. Basically the variable is variant for example Dim StrVar as Variant, then we assigned StrVar = Null or anything then this variable no longer empty (False). 

Syntax : IsEmpty(expression), Return : True/False

1) Example IsEmpty for True.

VBA Vode:

Option Explicit
Sub Examples_IsEmptyTrue()
    
    Dim StrVar
    Dim MyTest As Boolean
    
    MyTest = IsEmpty(StrVar)
    Debug.Print MyTest 'Result is True

End Sub

Note: The variables (StrVar) is not yet assigned or initialized.

2) Example IsEmpty for False.

VBA Vode:

Option Explicit
Sub Examples_IsEmptyFalse()
    
    Dim StrVar As Variant
    Dim MyTest As Boolean
    
    StrVar = Null
    MyTest = IsEmpty(StrVar)
    Debug.Print MyTest 'Result is False
    
End Sub

Note: The variables (StrVar) already assigned or initialized as Null.

Microsoft Reference-Isempty-function
Other Reference-Variant-empty.htm

Practice makes perfect. Thank You.

excelmacros
macro excel
excel programming
excel vba

No comments:

Post a Comment