Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

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
like image 819
Newskooler Avatar asked Jan 07 '23 22:01

Newskooler


1 Answers

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.

like image 72
Automate This Avatar answered Apr 27 '23 19:04

Automate This