CDate in a form

brianfosterblack

Active Member
Joined
Nov 1, 2011
Messages
251
I am using this code to take the date I have entered in a form and put it into a spreadsheet
VBA Code:
    ActiveCell.Value = CDate(FrmNewEntry.TextBox3)
    Selection.NumberFormat = "dd/mm/yyyy"
The problem is that if I have not entered a valid date I get a mismatch error and lose everything I have entered in the form. How do I check when I enter the date into TextBox 3 that I have not entered a valid date so I do not get this error only once I try to enter the data from the form into the spreadsheet. I need to identify while still in the form that I have not entered a valid date and get taken back to TextBox 3 before proceeding further.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You could use IsDate.
VBA Code:
Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Not IsDate(TextBox3.Value) Then
        Cancel = True
        MsgBox "Please enter a valid date!", vbCritical, "Invalid date"
    End If
End Sub
 
Upvote 0
Solution
You could use IsDate.
VBA Code:
Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Not IsDate(TextBox3.Value) Then
        Cancel = True
        MsgBox "Please enter a valid date!", vbCritical, "Invalid date"
    End If
End Sub
Thank you,

This works perfectly. Appreciate the assistance
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,665
Members
449,114
Latest member
aides

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