且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VBA验证文本中是否存在文本,然后检查日期是否正确

更新时间:2022-11-01 10:42:15

  If(Len(Trim(textboxDate1.Text)))0)而不是IsDate(textboxDate1.Text )然后
调用MsgBox(请选择正确的日期格式,vbOKOnly)
退出子
结束如果


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.

I have created this VBA clause to ensure the user types in a date that supports the format 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.

So my question: What VBA code is available to first determine whether text exists in the textbox, and then second to determine whether the date is in the correct format or not.

I was trying something similar to this:

 If textboxDate1.ListIndex = -1 Then

but I couldn't get it to work with the IsDate clause.

Many thanks in advance.

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