我有一个UserForm,如果用户输入的日期格式不是dd/mm/yyyy
,则在TextBox中,单击提交按钮后将弹出错误消息。这是我到目前为止的内容,它只将无效的日期输入格式化为dd / mm / yyyy,但不显示验证:
If IsDate(Me.DOBTextBox.Value) Then
Me.DOBTextBox = Format(Me.DOBTextBox.Value, "dd/mm/yyyy")
ElseIf Not IsDate(Me.DOBTextBox.Value) Then
MsgBox "Please enter a valid date format dd/mm/yyyy", vbCritical
DOBTextBox.SetFocus
Exit Sub
End If
答案 0 :(得分:0)
验证其中是否有两个斜杠userid,appnumber| trailid |
visit.visitnumber | visit.time | visit.hour | visit.minute |
visit.visitnumber | visit.time | visit.hour | visit.minute
以及/
和day <= 31
:
month <= 12
或者,仅尝试将字符串日期转换为实际日期,然后检查Dim ArrInput As Variant
ArrInput = Split(Me.DOBTextBox.Value, "/")
Dim ValidDate As Boolean
If UBound(ArrInput) = 2 Then 'make sure there are exactly two slashes in the date
If ArrInput(1) > 0 And ArrInput(1) <= 12 And _
ArrInput(0) > 0 And ArrInput(0) <= 31 Then 'month <=12 & day <= 31
ValidDate = True
End If
Else
ValidDate = False
End If
If Not ValidDate Then
MsgBox "Please enter a valid date format dd/mm/yyyy", vbCritical
DOBTextBox.SetFocus
Exit Sub
End If
'code here that executes when date is valid
Dim MyValidDate As Date
MyValidDate = DateSerial(ArrInput(2), ArrInput(1), ArrInput(0))
,day
和month
是否与字符串中的值匹配。
year