Public Function EndOfWeek(MyDate As String, Optional Vary As Integer = 0, Optional LastDay As Integer = 6) As String Dim WorkDate As Double Vary = 7 * Vary If Not IsDate(MyDate) Then Failout: MsgBox "The passed date " & MyDate & " is not a valid date", vbCritical, "Message from EndOfWeek Function" SetInvalid: EndOfWeek = "Invalid" Exit Function End If If CInt(LastDay) > 6 Then MsgBox "Invalid LastDay value " & LastDay & " value must be between 0 and 6", vbCritical, "Message from EndOfWeek Function" GoTo SetInvalid End If TestDate = Format(MyDate, , LastDay) WorkDate = CDbl(TestDate) If LastDay = 0 Then LastDay = 7 WorkDate = WorkDate + (LastDay - Weekday(MyDate)) WorkDate = WorkDate + Vary EndOfWeek = Format(WorkDate, "MM/DD/YYYY") End Function