检查输入日期格式

时间:2016-05-26 02:29:37

标签: excel vba date

有人知道如何检查vba文本框中的输入日期格式吗? 我有这个代码,但我不确定输出。用户的输入格式应为mm/dd/yyyy。如果没有,那么它会弹出一条消息说:使用mm / dd / yyy日期格式。

If Not Format(txtStartDate.Value, "mm/dd/yyyy") Then
    MsgBox "Please use the  mm/dd/yyyy date format."
    txtStartDate.setFocus
    Exit sub
End If

我只是不确定自己的态度。谢谢!

1 个答案:

答案 0 :(得分:3)

      Dim regEx As New RegExp
      regEx.Pattern = "^[0-9]{2}/[0-9]{2}/[0-9]{4}"
      Dim inputstring As String
      inputstring = "10/2/200"

      If Not regEx.test(inputstring) Then
      MsgBox "Please use the  mm/dd/yyyy date format."
        Exit Sub
      End If
相关问题