简体   繁体   English

Excel VBA用户表单现有日期验证

[英]Excel VBA userform existing date validation

I am currently working on a textbox (textbox1) in which the date must be entered. 我目前正在处理必须在其中输入日期的文本框(textbox1)。 However, until now it is also possible to enter a non-existent date, for example 40-40-2019. 但是,到目前为止,还可以输入一个不存在的日期,例如40-40-2019。 Is there a way to validate textbox1 with only existing dates? 有没有一种方法可以仅使用现有日期来验证textbox1? I currently use the European date entry, so dd-mm-yyyy. 我目前使用欧洲日期输入法,因此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: 使用Isdate()测试日期:

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 https://www.techonthenet.com/excel/formulas/isdate.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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