简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM