Change date format inside textbox userform

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Welcome. I have this code to enter the date into a textbox in the format "dd-mm-yyyy
I want to change it to "yyyy-mm-dd
Can I do that?

VBA Code:
Sub CtrL_Keydown(ByVal TxtB As MSForms.TextBox, ByVal KeyCode As MSForms.ReturnInteger, ByVal mask As String, Optional dat As Boolean)
    If KeyCode >= 48 And KeyCode <= 57 Then KeyCode = KeyCode + 48
    With TxtB
        Xl = .SelLength: If Xl = 0 Then Xl = 1
        .Value = IIf(.Value = "", mask, .Value): If KeyCode = 8 And Xl > 1 Then KeyCode = 46
        T = .Value: .SelStart = IIf(T = mask, InStr(1, mask, "_") - 1, .SelStart): X = .SelStart:
        Select Case KeyCode
        Case 96 To 105
            If dat Then Xl = 2
            If X = Len(mask) Then KeyCode = 0: Exit Sub
            If Mid(T, X + 1, 1) <> "_" And Not Mid(T, X + 1, 1) Like "[0-9]" Then X = InStr(T, "_") - 1
            Mid(T, X + 1, Xl) = Chr(KeyCode - 48) & Mid(mask, X + 2, Xl - 1): Xl = 0
            X = IIf(Mid(T, X + 1, 1) <> "_", InStr(T, "_") - 1, X + 1)
            If dat Then
                If Val(T) > 31 Or Val(Mid(T, 1, 1)) > 3 Then X = 0: Xl = 2: Mid(T, 1, 2) = Mid(mask, 1, 2): Beep
                If Val(Mid(T, 4, 2)) > 12 Or Val(Mid(T, 4, 1)) > 1 Then Mid(T, 4.2) = Mid(mask, 4, 2): X = 3: Xl = 2: Beep
                d = Mid(T, 1, 2): M = Mid(T, 4, 2): a = Mid(T, 7, 4)
                If IsDate(d & "/" & M) And Not IsDate(d & "/" & M & "/2000") Then Mid(T, 4, 2) = Mid(mask, 4, 2): X = 3: Xl = 2: Beep: KeyCode = 0
                If X = 10 And Not IsDate(T) Then Mid(T, 7, 10) = Mid(mask, 7, 10): X = 6: Xl = 4: Beep
            End If
        Case 8:
            If X = 0 Then KeyCode = 0:: .Value = "": Exit Sub Else Mid(T, X, 1) = Mid(mask, X, 1): X = X - 1: Xl = 0
            If T = mask Then T = ""
        Case 46:
            If X = 10 Then Exit Sub Else Mid(T, X + 1, Xl) = Mid(mask, X + 1, Xl): X = X: Xl = 0:
            If T = mask Then T = ""
        Case Else: KeyCode = 0
        End Select
        .Value = T
        If X > Len(mask) Or X = -1 Then X = Len(mask)
        .SelStart = X: .SelLength = 0: KeyCode = 0
    End With
    KeyCode = 0
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
@sofas
It seems like you want to validate the input as each character is typed. Is that really necessary? It seems quite complicated.
How about letting the user to input the date completely, and then validating it by clicking a command button?
Here's an example: link
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top