Date mismatch ...

adambc

Active Member
Joined
Jan 13, 2020
Messages
370
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
In an earlier post, you helped me format the output of a (US) date picker into UK format using ...

VBA Code:
Private Sub cmdNextActionDate_Click()

Dim dateVariable As Date
dateVariable = CalendarForm.GetDate

If dateVariable <> 0 Then
    Me.txtNextActionDate.Text = Format(CLng(dateVariable), "dd/mm/yyyy")
End If

End Sub

At that time I was using the date picker to select 2 dates and testing that one was after the other when the user tried to save the userform ...

This time I am setting one date field using ...

Code:
txtReportedDate.value = Format(Date, "dd/mm/yyyy")

But when I test as follows ...

Code:
If txtNextActionDate < txtReportedDate Then
    MsgBox "Next Action Date must be after the Reported Date (today), please correct and Save again"
    Exit Sub
End If

Unless the dd value (irrespective of mm) of NextActionDate is >= to the dd value of ReportedDate my MsgBox appears ...

Any ideas please?
 

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)
Try using CDate:
VBA Code:
If CDate(txtNextActionDate) < CDate(txtReportedDate) Then
    MsgBox "Next Action Date must be after the Reported Date (today), please correct and Save again"
    Exit Sub
End If
 
Upvote 0
Not sure what CDATE is (will look it up) but it worked a treat, thanks ...
 
Upvote 0
A supplementary question if I may (but tell me if I need to ask it in a new post) ...

CDATE works great, but doesn't like it if txtNextActionDate is blank (which is possible!) so that stops me using ...

VBA Code:
If txtNextActionDate.value <> "" And CDate(txtNextActionDate) < CDate(txtReportedDate) Then
    MsgBox "Next Action Date must be after the Reported Date (today), please correct and Save again"
    Exit Sub
End If

Is there an easy way round this eg can I test if txtNextActionDate.value = "" and if it is, exit (and carry on with the rest of the Sub cmdSave_Click() routine without even trying to test CDate(txtNextActionDate) < CDate(txtReportedDate)???

Thanks ...
 
Upvote 0
Something like...

If txtNextActionDate.value <> "" And CDate(txtNextActionDate) < CDate(txtReportedDate) Then
MsgBox "Next Action Date must be after the Reported Date (today), please correct and Save again"
Exit Sub
Else
Exit Sub
End If
 
Upvote 0
can I test if txtNextActionDate.value = "" and if it is, exit (and carry on with the rest of the Sub cmdSave_Click() routine
I don't understand, where is the code located? in Sub cmdSave_Click()?
So you don't mean exit sub? just continue to the next line?
 
Upvote 0
Akuini ... yes, continue to next line without testing CDate(txtNextActionDate) < CDate(txtReportedDate) (which I know will fail with an error if txtNextActionDate.value = "

jim may ... as above, I don't want to Exit Sub, but even if I did if txtNextActionDate.value <> "", CDate(txtNextActionDate) < CDate(txtReportedDate) fails with an error (it clearly doesn't like null values!!!
 
Upvote 0
Maybe this:
VBA Code:
If txtNextActionDate.Value <> "" Then
    If CDate(txtNextActionDate) < CDate(txtReportedDate) Then
       MsgBox "Next Action Date must be after the Reported Date (today), please correct and Save again"
       Exit Sub
    End If
End If
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,090
Members
449,065
Latest member
Danger_SF

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