Wie bekomme ich automatisch einen Bindestrich in eine Textbox?

1 Antwort

Von Experte Suboptimierer bestätigt

Mir fällt da nur die Lösung per VBA ein:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Dim text As String
    text = Replace(TextBox1.text, "-", "")
    
    If ((KeyAscii < Asc("0") Or KeyAscii > Asc("9")) Or Len(text) >= 6) And Not KeyAscii = Asc(vbBack) Then ' Nur erlaubte Tasten ausführen
        KeyAscii = 0
        Exit Sub
    End If
    
    If Len(TextBox1.text) >= 3 Then ' nach drei Ziffern einen Bindestrich einfügen
        TextBox1.text = Left(text, 3) & "-" & Mid(text, 4)
    End If
End Sub