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?
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