Excel – How to check if Variant is empty and assign other variable to it in Excel VBA

By | October 14, 2023

I am trying to make a function that has 3 inputs, were two of them are optional: colOffsetA & colOffsetB. If only one of the two optional ones is used in the function, then I wish that this value is automatically assigned to the empty one as well. Right now the code assigns the value 0 for colOffsetB for some reason.

Function TestCode(cellA As Range, Optional colOffsetA As Variant, Optional colOffsetB As Variant) As String

    If IsEmpty(colOffsetB) Then
        colOffsetB = colOffsetA
    End If

I believe what your looking for is IsMissing instead of IsEmpty. Try this:

If IsMissing(colOffsetB) = True Then
    colOffsetB = colOffsetA
End If

Here is a great article on Optional arguments.

Category: Uncategorized