#VALUE! Error from UDF

J

John Pierce

The following UDF works perfectly - except for the first line, i.e.
when CheckDate is a Sunday or Holiday. The result is #VALUE! but I
want just a blank or empty cell. How can I suppress or fix this?

Public Function RecissionDate(CheckDate, DaysAfter As Integer,
Holidays As Range) As Date
Dim BDADate As Date
Dim i As Integer

If isSunday(CheckDate) Or isHoliday(CheckDate, Holidays) Then
BDADate = ""

Else
BDADate = CheckDate
For i = 1 To DaysAfter
BDADate = BDADate + 1
If isSunday(BDADate) Or isHoliday(BDADate, Holidays)
Then
BDADate = BDADate + 1
Else: BDADate = BDADate
End If
If isSunday(BDADate) Or isHoliday(BDADate, Holidays)
Then
BDADate = BDADate + 1
Else: BDADate = BDADate
End If
Next i
End If
SunTrustRecissionDate = BDADate
End Function
 
A

atpgroups

The following UDF works perfectly - except for the first line, i.e.
when CheckDate is a Sunday or Holiday. The result is #VALUE! but I
want just a blank or empty cell. How can I suppress or fix this?

The problem is that you have defined the function to return a Date,
and "" isn't a valid date. (The date datatype is a specialised numeric
type)
If you change the "...) As Date" to "...) As Variant" it should
work.

Also note that an "If" doesn't need an "Else", and your "Else"s seem
to do nothing.
 
J

John Pierce

The problem is that you have defined the function to return a Date,
and "" isn't a valid date. (The date datatype is a specialised numeric
type)
If you change the "...)  As Date" to "...) As Variant" it should
work.

Also note that an "If" doesn't need an "Else", and your "Else"s seem
to do nothing.

Thanks, changing 'as Date' to 'as Variant' in both places fixed it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top