1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| Option Explicit
Const entrees_decimales_neg_permises = "-.,0123456789" & vbCr & vbBack
Const Point = "."
Const Virgule = ","
Const Moins = "-"
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = Asc(Point) Then
If InStr(TextBox1, Virgule) = 0 Then
KeyAscii = Asc(Virgule)
Else
KeyAscii = 0
End If
ElseIf InStr(entrees_decimales_neg_permises, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
ElseIf InStr(TextBox1, Virgule) > 0 And KeyAscii = Asc(Virgule) Then
KeyAscii = 0
End If
If KeyAscii = Asc(Moins) Then
If InStr(TextBox1, Moins) = 0 Then
KeyAscii = Asc(Moins)
Else
KeyAscii = 0
End If
End If
End Sub
Private Sub TextBox1_Change()
Dim Pos As Integer
Pos = InStr(TextBox1.Text, Moins)
If Pos > 1 Then
TextBox1.Text = Moins & Left(TextBox1.Text, Pos - 1) & Mid(TextBox1.Text, Pos + 1)
End If
End Sub |
Partager