Excel表单 – 条目

有没有办法让dateinput字段在月/日/年之间自动input“/”?
我目前的forms有保证dateinput一个正确的date格式,它也进行检查,以确保input写入mm / dd / yyyy,但是我想取消用户需要手动input“/”在我的研究和testing中没有find一个可行的解决scheme。 任何有用的解决scheme非常感谢。

谢谢!

If Me.TourDateText.Value = "" Then MsgBox "Please enter Tour Date.", vbExclamation, "frmEntry" Me.FirstNameText.SetFocus Exit Sub End If With Me.TourDateText If Not IsDate(.Value) Then .SetFocus MsgBox "Enter a valid date" Exit Sub End If End With If Not IsDate(Me.TourDateText.Value) Then MsgBox "The Tour Date field must contain only dates in the format mm/dd/yyyy.", vbExclamation, "frmEntry" Me.TourDateText.SetFocus Exit Sub End If 

您可以有以下进入UserForm

 Private Sub TourDateText_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Select Case KeyAscii Case Asc("0") To Asc("9") Case Else KeyAscii = 0 End Select If Len(Me.TourDateText.Text) = 2 Then If Val(Me.TourDateText.Text) > 12 Then KeyAscii = 0 Else _ Me.TourDateText.Text = Me.TourDateText.Text & "/" End If If Len(Me.TourDateText.Text) = 5 Then If Val(Mid(Me.TourDateText.Text, 4, 2)) > 31 Then KeyAscii = 0 Else _ Me.TourDateText.Text = Me.TourDateText.Text & "/" End If If Len(Me.TourDateText.Text) >= 10 Then KeyAscii = 0 End Sub 

第二版:

 Private Sub TourDateText_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Debug.Print KeyAscii If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 If Len(Me.TourDateText.Text) = 2 Then If Val(Me.TourDateText.Text) > 12 Then KeyAscii = 0 Else _ Me.TourDateText.Text = Me.TourDateText.Text & "/" End If If Len(Me.TourDateText.Text) = 5 Then If Val(Mid(Me.TourDateText.Text, 4, 2)) > 31 Then KeyAscii = 0 Else _ Me.TourDateText.Text = Me.TourDateText.Text & "/" End If If Len(Me.TourDateText.Text) >= 10 Then KeyAscii = 0 End Sub 

现在用户可以input09172014,文本将会是09/17/2014。 或者input可以是09 [任意键] 17 [任意键] 2014和文本将是09/17/2014。

也是几个月> 12和天> 31将被阻止。 这不是最终的数据validation,因为它无法检查月份是否有31天。 但最后的数据validation已经有了。

问候

阿克塞尔