简体   繁体   English

文本框日期格式Excel VBA

[英]textbox date format excel vba

I have a code that allows me to manually enter date in textbox1 which then gets selected in the calendar on the useform. 我有一个代码,允许我在textbox1中手动输入日期,然后在useform的日历中选择日期。 There is a second textbox that allows me to add or subtract dates. 第二个文本框允许我添加或减去日期。 The code works perfectly. 该代码运行完美。

Userform Code - 用户表单代码-

Option Explicit 
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) 
    If IsDate(Me.TextBox1.Value) Then Me.Calendar1.Value = Me.TextBox1.Value 
End Sub 
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean) 
    Dim dt As Date 
    With Me 
        If IsDate(.TextBox1.Value) Then 
            dt = CDate(.TextBox1.Value) + Val(.TextBox2.Value)
            .TextBox1.Value = dt 
            .Calendar1.Value = dt
        End If 
    End With 
End Sub 

I would like to manually enter date in textbox1 in a specific format. 我想以特定格式在textbox1中手动输入日期。

The formats will be - 格式为-

dd DD

ddmmm ddmmm

ddmmmyyy ddmmmyyy

I'm not sure how to write a code that does this. 我不确定如何编写执行此操作的代码。

The idea is to enter date in either of the 3 formats specified above in textbox1, which then gets selected on the calendar on the userform. 想法是使用textbox1上面指定的3种格式之一输入日期,然后在用户窗体的日历上选择日期。

edited after op's clarification about allowed formats 在op明确允许格式后进行编辑

you could build upon the following code 您可以基于以下代码

Option Explicit

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim txt As String, dayStr As String, monthStr As String, yearStr As String
    Dim okTxt As Boolean

    txt = Me.TextBox1.Value
    Select Case Len(txt)
        Case 2
            dayStr = txt
            okTxt = okDay(dayStr)
            monthStr = month(Now)
            yearStr = year(Now)
        Case 5
            dayStr = Mid(txt, 3, 3)
            monthStr = Mid(txt, 3, 3)
            okTxt = okDay(Left(txt, 2)) And okMonth(monthStr)
            yearStr = year(Now)
        Case 7
            dayStr = Mid(txt, 3, 3)
            monthStr = Mid(txt, 3, 3)
            yearStr = Mid(txt, 6, 2)
            okTxt = okDay(Left(txt, 2)) And okMonth(monthStr) And okYear(yearStr)
    End Select
    If Not okTxt Then
        MsgBox "Invalid date" _
               & vbCrLf & vbCrLf & "Date must be input in one of the following formats:" _
               & vbCrLf & vbTab & "dd" _
               & vbCrLf & vbTab & "ddmmm" _
               & vbCrLf & vbTab & "ddmmmyy" _
               & vbCrLf & vbCrLf & "Please try again", vbCritical

        Cancel = True
    Else
        Me.Calendar1.Value = CDate(Left(txt, 2) & " " & monthStr & " " & yearStr)
    End If
End Sub

Function okDay(txt As String) As Boolean
    okDay = CInt(txt) > 0 And CInt(txt) < 31
End Function

Function okMonth(txt As String) As Boolean
    Const months As String = "JANFEBMARAPRMAJJUNJULAUGSEPOCTNOVDEC"
    okMonth = InStr(months, UCase(txt)) > 0
End Function

Function okYear(txt As String) As Boolean
    okYear = CInt(txt) > 0 And CInt(txt) < 200 '<--| set your "limit" years
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM