Wouter Groeneweg
Wouter Groeneweg

Reputation: 53

Excel VBA userform existing date validation

I am currently working on a textbox (textbox1) in which the date must be entered. However, until now it is also possible to enter a non-existent date, for example 40-40-2019. Is there a way to validate textbox1 with only existing dates? I currently use the European date entry, so dd-mm-yyyy. And the code below to change the date to a standard format:

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
 Me.TextBox1 = Format(Me.TextBox1, "DD-MM-YYYY")
End Sub 

I expect the output to be only existing date functions

Upvotes: 1

Views: 3097

Answers (1)

D_Bester
D_Bester

Reputation: 5921

Use Isdate() to test for date:

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Isdate(Me.TextBox1.Text) then
        Me.TextBox1.Text = Format(Me.TextBox1.Text, "DD-MM-YYYY")
    Else
        '...
    End if
End Sub 

https://www.techonthenet.com/excel/formulas/isdate.php

Upvotes: 0

Related Questions