Option ExplicitPrivate Sub CommandButton1_Click()
Dim Ok As Boolean
Call Datum_pruefen(Ok)
If Not Ok Then Exit Sub
Call Zeit_pruefen(Ok)
If Not Ok Then Exit Sub
Select Case Schalter
Case 1
Range("A1").Value = Format(Datum1.Value, "dd.mm.yyyy")
Range("A2").Value = Format(Zeit1.Value, "hh:mm")
End Select
Unload Me
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub Datum1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 44, 45: KeyAscii = 46 'aus Komma und Minus Punkt machen
Case 46 'Punkt
Case 48 To 57 'Zahlen von 0 bis 9
Case Else: KeyAscii = 0
End Select
End Sub
Private Sub Zeit1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 44, 45: KeyAscii = 58 'aus Komma und Minus Doppelpunkt machen
Case 48 To 58 'Zahlen von 0 bis 9 und Doppelpunkt
Case Else: KeyAscii = 0
End Select
End Sub
Private Sub Datum_pruefen(Ok As Boolean)
Dim Tag As String, Monat As String, Jahr As String
Ok = False
If Not IsDate(Datum1.Value) Then
MsgBox "Datumseingabe falsch.", 48, "Hinweis"
Datum1.SelStart = 0
Datum1.SelLength = Len(Datum1.Value)
Datum1.SetFocus
Exit Sub
End If
Tag = Left(Datum1.Value, InStr(1, Datum1.Value, ".") - 1)
Monat = Mid(Datum1.Value, InStr(1, Datum1.Value, ".") + 1)
Monat = Mid(Monat, 1, InStr(1, Datum1.Value, ".") - 1)
Jahr = Mid(Datum1.Value, Len(Tag) + Len(Monat) + 3)
If Len(Jahr) < 4 Then Jahr = "2" & String(4 - Len(Jahr) - 1, "0") & Jahr
Select Case CInt(Monat)
Case 1, 3, 5, 7, 8, 10, 12
If CInt(Tag) > 31 Then
MsgBox "Tag falsch.", 48, "Hinweis"
Datum1.Value = Datum1.Value
Datum1.SelStart = 0
Datum1.SelLength = Len(Datum1.Value)
Datum1.SetFocus
Exit Sub
End If
Case 4, 6, 9, 11
If CInt(Tag) > 30 Then
MsgBox "Tag falsch.", 48, "Hinweis"
Datum1.SelStart = 0
Datum1.SelLength = Len(Datum1.Value)
Datum1.SetFocus
Exit Sub
End If
Case 2
If CInt(Jahr) Mod 4 = 0 And (CInt(Jahr) Mod 100 <> 0 Xor CInt(Jahr) Mod 400 = 0) Then
If CInt(Tag) > 29 Then
MsgBox "Tag falsch.", 48, "Hinweis"
Datum1.SelStart = 0
Datum1.SelLength = Len(Datum1.Value)
Datum1.SetFocus
Exit Sub
End If
Else
If CInt(Tag) > 28 Then
MsgBox "Tag falsch.", 48, "Hinweis"
Datum1.SelStart = 0
Datum1.SelLength = Len(Datum1.Value)
Datum1.SetFocus
Exit Sub
End If
End If
End Select
If CInt(Monat) > 12 Then
MsgBox "Monat falsch.", 48, "Hinweis"
Datum1.SelStart = 0
Datum1.SelLength = Len(Datum1.Value)
Datum1.SetFocus
Exit Sub
End If
If CInt(Jahr) < CInt(Year(Date)) Then
MsgBox "Jahr falsch.", 48, "Hinweis"
Datum1.SelStart = 0
Datum1.SelLength = Len(Datum1.Value)
Datum1.SetFocus
Exit Sub
End If
Datum1.Value = Format(Datum1.Value, "dd.mm.yyyy")
Ok = True
End Sub
Private Sub Zeit_pruefen(Ok As Boolean)
Dim Stunden As String, Minuten As String
Ok = False
If Not IsDate(Zeit1.Value) Then
MsgBox "Zeiteingabe falsch.", 48, "Hinweis"
Zeit1.SelStart = 0
Zeit1.SelLength = Len(Zeit1.Value)
Zeit1.SetFocus
Exit Sub
End If
If InStr(1, Zeit1.Value, ":") > 0 Then
Stunden = Left(Zeit1.Value, InStr(1, Zeit1.Value, ":") - 1)
Minuten = Mid(Zeit1.Value, Len(Stunden) + 2)
Else
Stunden = Zeit1.Value
Minuten = "00"
End If
If CInt(Stunden) > 23 Then
MsgBox "Stundeneingabe falsch.", 48, "Hinweis"
Zeit1.SelStart = 0
Zeit1.SelLength = Len(Zeit1.Value)
Zeit1.SetFocus
Exit Sub
End If
If CInt(Minuten) > 59 Then
MsgBox "Minuteneingabe falsch.", 48, "Hinweis"
Zeit1.SelStart = 0
Zeit1.SelLength = Len(Zeit1.Value)
Zeit1.SetFocus
Exit Sub
End If
Zeit1.Value = Format(Stunden & ":" & Minuten, "hh:mm")
Ok = True
End Sub
'In einem Normalen Modul:
Option Explicit
Public Schalter As Integer
Public Sub Com1()
Schalter = 1
UserForm1.Show
End Sub