Option ExplicitPrivate Sub Command1_Click() Dim Arr() As String Dim i As Long Dim MyDate As Date Arr = Split(Text1.Text, "-") If UBound(Arr) < 1 Then Exit Sub MyDate = Format(Arr(0) & "-" & Arr(1) & "-" & Arr(2), "yyyy-mm-dd") Debug.Print MyDate Debug.Print Arr(UBound(Arr)) End SubPrivate Sub Form_Load() Text1.Text = Date & "-z" End Sub
Private Sub Text1_LostFocus() If Len(Text1.Text) > 12 Then MsgBox "错误!" Text1.SetFocus Text1.Text = "" Exit Sub ElseIf Len(Text1.Text) <= 12 Then If Right(Text1.Text, 2) <> "-z" And Right(Text1.Text, 2) <> "-b" And Right(Text1.Text, 2) <> "-y" Then MsgBox "错误!" Text1.SetFocus Text1.Text = "" Exit Sub ElseIf IsDate(Left(Text1.Text, 10)) = False Then MsgBox "错误!" Text1.SetFocus Text1.Text = "" Exit Sub End If End If
再判断_符号.
按楼上两位的还得判断年月日必须是数字最后一位是字母b、z、y
Dim Arr() As String
Dim i As Long
Dim MyDate As Date
Arr = Split(Text1.Text, "-")
If UBound(Arr) < 1 Then Exit Sub
MyDate = Format(Arr(0) & "-" & Arr(1) & "-" & Arr(2), "yyyy-mm-dd")
Debug.Print MyDate
Debug.Print Arr(UBound(Arr))
End SubPrivate Sub Form_Load()
Text1.Text = Date & "-z"
End Sub
http://www.soulogic.com/doc/RegularExpressions/
If Len(Text1.Text) > 12 Then
MsgBox "错误!"
Text1.SetFocus
Text1.Text = ""
Exit Sub
ElseIf Len(Text1.Text) <= 12 Then
If Right(Text1.Text, 2) <> "-z" And Right(Text1.Text, 2) <> "-b" And Right(Text1.Text, 2) <> "-y" Then
MsgBox "错误!"
Text1.SetFocus
Text1.Text = ""
Exit Sub
ElseIf IsDate(Left(Text1.Text, 10)) = False Then
MsgBox "错误!"
Text1.SetFocus
Text1.Text = ""
Exit Sub
End If
End If