wie kann ich ein Textfeld in einem Formular Formatieren mit und nur 2 Dezimalstellen?
Gruß André
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox1 = Format(TextBox1, "#,##0.00 ")
End Sub
Du kannst den Code auch einem Button zuweisen der auf dem Formular liegt. z.B. soPrivate Sub CommandButton1_Click()
TextBox1 = Format(TextBox1, "#,##0.00 ")
End Sub
Es kommt also drauf an wann Du formatieren willstPrivate Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(TextBox1.Text) Then TextBox1 = Format(TextBox1, "#,##0.00 ")
End Sub
Bei Text wird die Formatierung ignoriert, aber ein Datum ist auch eine Zahl !
' Erzeugt die Anzeige mit
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
' Wenn nicht numerisch, dann gehts nicht weiter
If Not IsNumeric(TextBox1.Value) And TextBox1.Value "" Then
Cancel = True
ElseIf TextBox1.Value "" Then
Application.EnableEvents = False
TextBox1.Value = Format(TextBox1.Value, "#0.00") & " "
Application.EnableEvents = False
End If
End Sub
' Verhindert die Doppeleingabe von Kommas
Private Sub TextBox1_Change()
Static lastValue As String
Dim cursPos As Long
If InStr(TextBox1.Value, ",") InStrRev(TextBox1.Value, ",") Then
' bei zweitem Komma wird die alte Eingabe wiederhergestellt
' die Cursor-Position wird bei Eingabe von Einzelzeichen ebenfalls
' korrekt wiederhergestellt, bei Copy&Paste von mehreren Zeichen nicht
cursPos = TextBox1.SelStart
TextBox1.Value = lastValue
TextBox1.SelStart = Application.Max(cursPos - 1, 0)
Else
lastValue = TextBox1.Value
End If
End Sub
' Löscht das -Zeichen bei der Eingabe
Private Sub TextBox1_Enter()
' Wenn was drinsteht, dann ist auch das Eurozeichen drin
If TextBox1.Value "" Then
Application.EnableEvents = False
TextBox1.Value = Left(TextBox1.Value, Len(TextBox1.Value) - 2)
Application.EnableEvents = True
End If
End Sub
' Läßt nur Zahlen und das Komma als Eingabe zu
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If (KeyAscii 57) And KeyAscii 44 Then
KeyAscii = 0
End If
End Sub