Option Explicit
Const separator As String = "-"
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim t$
With TextBox1
If Not Chr(KeyAscii) Like "*[0-9]*" Then KeyAscii = 0
t = Mid(.Value, 1, .SelStart)
If Len(t) = 2 Or Len(t) = 5 Then t = t & separator
t = t & Chr(KeyAscii):
If Len(t) >= 6 Then If Not IsDate(Left(t, 6) & "2000") Then t = Left(t, 3)
If Len(t) = 10 And Not IsDate(t) Then t = Left(t, 6)
.Value = Mid(t, 1, 10)
If Val(.Value) > 31 Then .Value = ""
End With
KeyAscii = 0
controldatediff
End Sub
Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim t$
With TextBox2
If Not Chr(KeyAscii) Like "*[0-9]*" Then KeyAscii = 0
t = Mid(.Value, 1, .SelStart)
If Len(t) = 2 Or Len(t) = 5 Then t = t & separator
t = t & Chr(KeyAscii):
If Len(t) >= 6 Then If Not IsDate(Left(t, 6) & "2000") Then t = Left(t, 3)
If Len(t) = 10 And Not IsDate(t) Then t = Left(t, 6)
.Value = Mid(t, 1, 10)
If Val(.Value) > 31 Then .Value = ""
End With
KeyAscii = 0
controldatediff
End Sub
Private Sub controldatediff()
If Len(TextBox1 & TextBox2) = 20 Then TextBox3 = CDate(TextBox2) - CDate(TextBox1)
End Sub