Boolean functions in VBA
This page gives an overview of the VBA Boolean functions with example code showing how they are used. A link is provided for each function to its documentation on microsoft learn VBA site.
- IsArray - The variable contains an array
- IsDate - The variable is a valid date
- IsEmpty - The variable has been initialized
- IsError - The expression is an error value
- IsMissing - The procedure's optional Variant argument has been left empty
- IsNull - The expression contains no valid data
- IsNumeric - The expression can be evaluated as a number
- IsObject - The identifier represents an object variable
- StringStartsWith - The string variable starts with a certain value
- StringStartsWith - The string variable ends with a certain value
- StringStartsWith - The string variable contains a certain value
- FolderExists - A folder with a specified path exists
- FileExists - A file with a specified path exists
- EOF - The end of a file has been reached
- Cbool - Converts a value to a boolean
Code VBA IntelliSense makes these function accessible under submenu VBA, see screenshot. .
Hovering a menu you see an explanation what a procedure does, and what the inserted code will look like, depending on changes you make in the Function Call Builder dialog.
IsArray - Indicates whether a variable is an array
The IsArray function returns a Boolean value indicating whether a variable is an array.
From the Code VBA toolbar, select VBA > String> IsArray »
Dim MyArray(1 To 5) As Integer, YourArray, MyCheck ' Declare array variables.'
YourArray = Array(1, 2, 3) ' Use Array function.'
MyCheck = IsArray(MyArray) ' Returns True.'
MyCheck = IsArray(YourArray) ' Returns True.'
IsDate - Indicates if the variable is a date
The IsDate function returns True if the expression is a date or is recognizable as a valid date or time; otherwise, it returns False.
From the Code VBA toolbar, select VBA > String> IsDate »
Dim MyVar, MyCheck
MyVar = "04/28/2023" ' Assign valid date value.'
MyCheck = IsDate(MyVar) ' Returns True.'
MyCheck = IsDate(MyVar) ' Returns False.'
MyVar = "04.28.14" ' Assign valid time value.'
MyCheck = IsDate(MyVar) ' Returns True.'
MyVar = "04.28.2023" ' Assign invalid time value.'
MyCheck = IsDate(MyVar) ' Returns False.'
MyVar = "13/32/2023" ' Assign invalid d'
See https://docs.microsoft.com/office/vba/language/reference/user-interface-help/isdate-function
IsEmpty - Indicates whether a variable has been initialized
The IsEmpty function returns a Boolean value indicating whether a variable has been initialized.
From the Code VBA toolbar, select VBA > String> IsEmpty »
Dim MyVar, MyCheck
MyCheck = IsEmpty(MyVar) ' Returns True.'
MyVar = Null ' Assign Null.'
MyCheck = IsEmpty(MyVar) ' Returns False.'
MyVar = Empty ' Assign Empty.'
MyCheck = IsEmpty(MyVar) ' Returns True.'
See https://docs.microsoft.com/office/vba/language/reference/user-interface-help/isempty-function
IsError - Indicates whether an expression is an error value
The IsError function returns a Boolean value indicating whether an expression is an error value.
From the Code VBA toolbar, select VBA > String> IsError »
Dim ReturnVal, MyCheck
ReturnVal = UserFunction()
MyCheck = IsError(ReturnVal) ' Returns True.'
See https://docs.microsoft.com/office/vba/language/reference/user-interface-help/iserror-function
IsMissing - True if a procedure's optional Variant argument has been left empty
The IsMissing function returns a Boolean value indicating whether an optional Variant argument has been passed to a procedure.
From the Code VBA toolbar, select VBA > String> IsMissing »
Sub MySub(Optional MyVar As String = "specialvalue")
If MyVar = "specialvalue" Then
' MyVar was omitted.'
Else
...
End Sub
See https://docs.microsoft.com/office/vba/language/reference/user-interface-help/ismissing-function
IsNull - True if an expression contains no valid data
The IsNull function returns a Boolean value that indicates whether an expression contains no valid data (Null).
From the Code VBA toolbar, select VBA > String> IsNull »
Dim MyVar, MyCheck
MyCheck = IsNull(MyVar) ' Returns False.'
MyVar = ""
MyCheck = IsNull(MyVar) ' Returns False.'
MyVar = Null
MyCheck = IsNull(MyVar) ' Returns True.'
See https://docs.microsoft.com/office/vba/language/reference/user-interface-help/isnull-function
IsNumeric - Indicates whether an expression can be evaluated as a number
The IsNumeric function returns a Boolean value indicating whether an expression can be evaluated as a number.
From the Code VBA toolbar, select VBA > String> IsNumeric »
Dim MyVar, MyCheck
MyVar = "53" ' Assign value.'
MyCheck = IsNumeric(MyVar) ' Returns True.'
MyVar = "459.95" ' Assign value.'
MyCheck = IsNumeric(MyVar) ' Returns True.'
MyVar = "45 Help" ' Assign value.'
MyCheck = IsNumeric(MyVar) ' Returns False.'
See https://docs.microsoft.com/office/vba/language/reference/user-interface-help/isnumeric-function
IsObject - True if an identifier represents an object variable
The IsObject function returns a Boolean value indicating whether an identifier represents an object variable.
From the Code VBA toolbar, select VBA > String> IsObject »
Dim MyInt As Integer ' Declare variables.'
Dim YourObject, MyCheck ' Note: Default variable type is Variant.'
Dim MyObject As Object
Set YourObject = MyObject ' Assign an object reference.'
MyCheck = IsObject(YourObject) ' Returns True.'
MyCheck = IsObject(MyInt) ' Returns False.'
MyCheck = IsObject(Nothing) ' Returns True.'
MyCheck = IsObject(Empty) ' Returns False.'
MyCheck = IsObject(Null) ' Returns False.'
See https://docs.microsoft.com/office/vba/language/reference/user-interface-help/isobject-function
StringStartsWith - Tests if a string variable starts with a certain value
The StringStartsWith function 0
From the Code VBA toolbar, select VBA > String> StringStartsWith »
StringStartsWith - Tests if a string variable ends with a certain value
The StringEndsWith function 0
From the Code VBA toolbar, select VBA > String> StringEndsWith »
StringStartsWith - Tests if a string variable contains a certain value
The StringContains function 0
From the Code VBA toolbar, select VBA > String> StringContains »
FolderExists - Determine if a folder with a specified path exists
The FolderExists function returns True if a specified folder exists; False if it does not.
From the Code VBA toolbar, select VBA > String> FolderExists »
FileExists - Determine if a file with a specified path exists
The FileExists function returns True if a specified file exists; False if it does not.
From the Code VBA toolbar, select VBA > String> FileExists »
EOF - Indicates if the end of a file has been reached
The EOF function returns an Integer containing the Boolean value True when the end of a file opened for Random or sequential Input has been reached.
From the Code VBA toolbar, select VBA > String> EOF »
Dim InputData
Open "MYFILE" For Input As #1 ' Open file for input.'
Do While Not EOF(1) ' Check for end of file.'
Line Input #1, InputData ' Read line of data.'
Debug.Print InputData ' Print to the Immediate window.'
Loop
Close #1 ' Close file.'
See https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/eof-function
Cbool - Converts a value to a boolean
The Cbool function converts a value to a boolean. Can only be used in Excel.
From the Code VBA toolbar, select VBA > String> Cbool »
Dim LCompare as Boolean
LCompare = CBool(1=2)