Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1840to1844
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

SetFocus funkt nicht

SetFocus funkt nicht
03.08.2021 11:13:54
Andreas
Mit diesem Code überprüfe ich ein Textfeld auf ein gültiges Datum:


Private Sub TextBox1_AfterUpdate()
Dim txt As String
Dim F, Formate
txt = TextBox1.Text
Formate = Array("00-00-0000", "00-00-00", "00-00-0")
If Not IsDate(txt) Then
If IsNumeric(txt) Then
For Each F In Formate
If IsDate(Format(Val(txt), F)) Then
txt = Format(Val(txt), F)
Exit For
End If
Next
End If
End If
If IsDate(txt) Then
TextBox1 = Format(CDate(txt), "DD.MM.YYYY")
Else
MsgBox "Bitte korretes Datum eingeben."
TextBox1.SetFocus
End If
End Sub

Warum funktioniert der TextBox1.SetFocus nicht auf das gleiche Feld, das er eben überprüft hat?
Wenn Ich TextBox4.SetFocus einstelle, springt er auch in TextBox4 - nur nicht in die seinige zurück, die ja korrigiert werden soll.

3
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: SetFocus funkt nicht
03.08.2021 11:41:28
Rudi
Hallo,
das funktioniert:

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim txt As String
Dim F, Formate
txt = TextBox1.Text
Formate = Array("00-00-0000", "00-00-00", "00-00-0")
If Not IsDate(txt) Then
If IsNumeric(txt) Then
For Each F In Formate
If IsDate(Format(Val(txt), F)) Then
txt = Format(Val(txt), F)
Exit For
End If
Next
End If
End If
If IsDate(txt) Then
TextBox1 = Format(CDate(txt), "DD.MM.YYYY")
Else
MsgBox "Bitte korretes Datum eingeben."
Cancel = True
TextBox1.SetFocus
End If
End Sub
Gruß
Rudi
AW: SetFocus funkt nicht
03.08.2021 11:55:25
Andreas
Hallo Rudi,
das stimmt, deines funktioniert. Kannst Du mir kurz erklären, was mein Fehler war ? Ich habe gesehen, Cancel=True
Was bewirkt dieser Eintrag, ich will ja auch was dazulernen...
Lieben Dank für deine Lösung.
Anzeige
AW: SetFocus funkt nicht
03.08.2021 11:56:25
Rudi
das bewirkt, dass das Verlassen der Textbox abgebrochen wird.

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige