首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel表格-条目

Excel表格-条目
EN

Stack Overflow用户
提问于 2014-09-16 18:44:30
回答 1查看 112关注 0票数 0

是否有办法让日期输入字段在月/日/年之间自动输入"/“?

我的当前表单有一些子类,以确保日期以正确的日期格式输入,并且它还检查条目是否写入mm/dd/yyyy,但是,我希望取消需要手动输入"/“的用户,并且在我的研究和测试中没有找到可行的解决方案。任何有帮助的解决方案都将不胜感激。

谢谢!

代码语言:javascript
复制
    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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-17 06:35:53

然后您可以跟踪到UserForm

代码语言:javascript
复制
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

第二版:

代码语言:javascript
复制
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

现在用户可以输入09172014,文本为09/17/2014。或者输入为09any key17any key2014,文本为09/17/2014。

月> 12,天> 31也是可以预防的。这不是最终的数据验证,因为它不能检查月是否有31天。但是最终的数据验证已经有了。

问候

Axel

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25876226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档