DateAdd Function error won't accept Null as 0 or ""

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Hi
This function was working correctly and returns the dates when a payment is next payable. This works well except if I enter 2 dates dates which does not include a date in which a payment is due.
I need to value to set the DtAdd too if the dates are past, a variable I can use in an if statement to throw an error.
Originally I tried Null but got a Null not valid. DtAdd=Null
If I put the message box in the function upon exit function the procedure continues to run.
I tried the date as per below but neither if DtAdd="01/01/1900" then or if DtAdd is >" 01/01/2010" then will catch it.

This is the section I need help with.. (full function at bottom)
VBA Code:
If StartDtPlus > StartDt Then
        DtAdd = 1 / 1 / 1900
    Else
        DtAdd = StartDtPlus
    End If

This is the entire function
VBA Code:
Function DtAdd(StartDt As Date, Freq As String, EndDt As Date) As Date

Dim StartDtPlus As Date
Dim LastPayment As Date

NowDt = Format(Now(), "dd/mm/yyyy")
   
    Do Until CLng(StartDtPlus) >= CLng(NowDt)
        If CLng(LastPayment) >= CLng(NowDt) Then Exit Do
            StartDtPlus = StartDt
       
            Select Case Freq
                    Case "Once Only"
                        MsgBox "This date is in the past. Please edit and resubmit.", vbOKOnly, "Date Past"
                        Exit Function
                    Case "Daily"
                        StartDtPlus = DateAdd("d", 1, StartDt)
                        LastPayment = DateAdd("d", -1, EndDt)
                    Case "Weekly"
                        StartDtPlus = DateAdd("ww", 1, StartDt)
                        LastPayment = DateAdd("d", -1, EndDt)
                    Case "Fortnightly"
                        StartDtPlus = DateAdd("ww", (2), StartDt)
                        LastPayment = DateAdd("d", -1, EndDt)
                    Case "Monthly"
                        StartDtPlus = DateAdd("m", 1, StartDt)
                        LastPayment = DateAdd("d", -1, EndDt)
                    Case "Quarterly"
                        StartDtPlus = DateAdd("q", 1, StartDt)
                        LastPayment = DateAdd("d", -1, EndDt)
                    Case "Tri Annually"
                        StartDtPlus = DateAdd("m", (4), StartDt)
                        LastPayment = DateAdd("d", -1, EndDt)
                    Case "Bi Annually"
                        StartDtPlus = DateAdd("m", (6), StartDt)
                        LastPayment = DateAdd("d", -1, EndDt)
                    Case "Annually"
                        StartDtPlus = DateAdd("yyyy", 1, StartDt)
                        LastPayment = DateAdd("d", -1, EndDt)
                    End Select
            Loop

    If StartDtPlus > StartDt Then
        DtAdd = 1 / 1 / 1900
    Else
        DtAdd = StartDtPlus
    End If
   
End Function
Thanks Dannielle
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
We probably need some sample data with expected result in an XL2BB to answer this but does this do what you are trying to do ?
VBA Code:
DtAdd = #1/1/1900#
 
Upvote 0
Solution
The marked solution has been switched accordingly.

@dellehurley - In your future questions, please mark the post as the solution that answered your question to help future readers. No further action is needed for this thread.
Honestly I thought I did, sorry about that.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top