Automatische Kommasetzung
14.11.2016 08:36:48
Floeh
ich hoffe Ihr könnt mir weiterhelfen. Ich habe folgend einen "BMI-Rechner", hier muss in der TextBox2 die Größe (m,cm) eingetragen werden. Ich habe bereits eine MsgBox bei ungültiger Eingabe, besser wäre die Automatische Kommasetzung einzufügen. Wie mache ich dies?
Option Explicit
Private Sub CommandButton1_Click()
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox3.BackColor = &HFFFFFF
OptionButton1 = True
TextBox1.SetFocus
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If bChange = True Then
bChange = False
Exit Sub
End If
If Trim(TextBox1.Value) = "" Then
MsgBox "Bitte geben Sie Ihr Gewicht in Kilogramm ein.", _
48, " Hinweis für " & Application.UserName
Cancel = True
TextBox1.SetFocus
End If
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim BMI As Single
If Trim(TextBox2.Value) = "" Then
MsgBox "Bitte geben Sie Ihre Körpergröße in Meter und Zentimeter ein.", _
48, " Hinweis für " & Application.UserName
Cancel = True
TextBox2.SetFocus
ElseIf InStr(TextBox2.Value, ",") = 0 Then
MsgBox "Bitte geben Sie Ihre Körpergröße in Meter einem Komma und Zentimeter ein.", _
48, " Hinweis für " & Application.UserName
Cancel = True
TextBox2.SetFocus
Else
BMI = CSng(CDbl(TextBox1.Value) / CDbl(TextBox2.Value) ^ 2)
If OptionButton1 Then
Select Case BMI
Case Is 40
TextBox3.BackColor = &HC0&
Label4.Caption = "massive Adipositas"
End Select
Else
Select Case BMI
Case Is 40
TextBox3.BackColor = &HC0&
Label4.Caption = "massive Adipositas"
End Select
End If
TextBox3.Value = Format(BMI, "0.0")
TextBox1.SetFocus
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr("1234567890.," & Chr$(8), Chr$(KeyAscii)) = 0 Then
KeyAscii = 0
End If
If KeyAscii = Asc(".") Then
KeyAscii = Asc(",")
End If
' nur ein Dezimalzeichen zulassen
If InStr(1, TextBox1.Text, ",", 0) And KeyAscii = Asc(",") Then
KeyAscii = 0
End If
End Sub
Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr("1234567890.," & Chr$(8), Chr$(KeyAscii)) = 0 Then
KeyAscii = 0
End If
If KeyAscii = Asc(".") Then
KeyAscii = Asc(",")
End If
' nur ein Dezimalzeichen zulassen
If InStr(1, TextBox2.Text, ",", 0) And KeyAscii = Asc(",") Then
KeyAscii = 0
End If
End Sub
Private Sub UserForm_Initialize()
OptionButton1 = True
End Sub