VBA 验证文本框中是否存在文本,然后检查日期格式是否正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11863431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA to verify if text exists in a textbox, then check if date is in the correct format
提问by Paolo Bernasconi
I have an excel form that I created in combination with VBA. In the form I have a textbox in which the user is to type in a date.
我有一个与 VBA 结合创建的 excel 表单。在表单中,我有一个文本框,用户可以在其中输入日期。
I have created this VBA clause to ensure the user types in a date that supports the format xx/xx/xxxx.
我创建了这个 VBA 子句来确保用户输入的日期支持格式xx/xx/xxxx。
If Not IsDate(textboxDate1.Text) Then
Call MsgBox("Please select a correct Date format", vbOKOnly)
Exit Sub
End If
However, with this VBA code, the user is required to enter a date, whether the user needs to or not. So when I have a 4 other textboxes to input a date in my form, and the user only needs to enter in 1 date, and not 5, I have the problem where the user is required to put in a date for the other four textboxs in order submit the form.
但是,使用此 VBA 代码,无论用户是否需要,都需要输入日期。因此,当我有 4 个其他文本框在我的表单中输入日期,而用户只需要输入 1 个日期而不是 5 个日期时,我遇到了要求用户为其他四个文本框输入日期的问题以提交表格。
So my question: What VBA code is available to firstdetermine whether text exists in the textbox, and then secondto determine whether the date is in the correct format or not.
所以我的问题:什么VBA代码提供给第一个确定的文本中是否存在文本,然后第二次来确定日期是否是正确的格式或没有。
I was trying something similar to this:
我正在尝试类似的事情:
If textboxDate1.ListIndex = -1 Then
but I couldn't get it to work with the IsDate clause.
但我无法让它与 IsDate 子句一起使用。
Many thanks in advance.
提前谢谢了。
采纳答案by rene
If (Len(Trim(textboxDate1.Text)) <> 0) And Not IsDate(textboxDate1.Text) Then
Call MsgBox("Please select a correct Date format", vbOKOnly)
Exit Sub
End If