AW: Tausender-Trennzeichen
15.09.2003 16:27:06
ChrisL
Hi Armin
Nachstehend mal ein Ansatz. Die erste Variante läuft so einigermassen OK, wenn du die englische schreibweise (#,##0.00) verwendest. Variante 2 habe ich dir auf die deutsche Variante umgeschrieben, da ich es aber nicht testen konnte, könnte es dabei noch Probleme geben...
' ***************** VARIANTE 1 ******************
Option Explicit
Private Sub TextBox1_Change()
If IsNumeric(TextBox1) Then
Dim Pos As Integer
If Mid(TextBox1, TextBox1.SelStart + 1, 1) = "." Or InStr(TextBox1, ".") = 0 Then
TextBox1 = Format(TextBox1, "#,##0.00")
TextBox1.SelStart = InStr(TextBox1, ".") - 1
Else
Pos = TextBox1.SelStart
TextBox1 = Format(TextBox1, "#,##0.00")
TextBox1.SelStart = Pos
End If
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(".")
KeyAscii = 0
TextBox1.SelStart = TextBox1.SelStart + 1
Case Asc(",")
If InStr(TextBox1, ",") <> 0 Then
KeyAscii = 0
Else
KeyAscii = Asc(",")
End If
Case Asc(vbBack)
Case Asc("-")
If Len(TextBox1) = 0 Then
If InStr(TextBox1, "-") <> 0 Then
Else
KeyAscii = Asc("-")
End If
Else
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
' ***************** VARIANTE 2 ******************
Option Explicit
Private Sub TextBox1_Change()
If IsNumeric(TextBox1) Then
Dim Pos As Integer
If Mid(TextBox1, TextBox1.SelStart + 1, 1) = "," Or InStr(TextBox1, ",") = 0 Then
TextBox1 = Format(TextBox1, "#,##0.00")
TextBox1.SelStart = InStr(TextBox1, ".") - 1
Else
Pos = TextBox1.SelStart
TextBox1 = Format(TextBox1, "#,##0.00")
TextBox1.SelStart = Pos
End If
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Asc(",")
KeyAscii = 0
TextBox1.SelStart = TextBox1.SelStart + 1
Case Asc(",")
Case Asc(vbBack)
Case Asc("-")
If Len(TextBox1) = 0 Then
If InStr(TextBox1, "-") <> 0 Then
Else
KeyAscii = Asc("-")
End If
Else
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
Gruss
Chris