Option Explicit
Const entrees_permises = "+-.,0123456789" & vbCr & vbBack
Const Point = "."
Const Virgule = ","
Const Moins = "-"
Const Plus = "+"
Private Sub TextBox1_Change()
Dim Pos As Integer, iL As Integer
iL = Len( TextBox1.Text)
Pos = InStr( TextBox1.Text, Moins)
If Pos > 1 Then
TextBox1.Text = Moins & Left( TextBox1.Text, Pos - 1) & Mid( TextBox1.Text, Pos + 1, iL - Pos)
End If
Pos = InStr( TextBox1.Text, Plus)
If Pos > 1 Then
TextBox1.Text = Plus & Left( TextBox1.Text, Pos - 1) & Mid( TextBox1.Text, Pos + 1, iL - Pos)
End If
End Sub
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_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 And InStr( TextBox1, Plus) = 0 Then
KeyAscii = Asc(Moins)
Else
KeyAscii = 0
End If
End If
If KeyAscii = Asc(Plus) Then
If InStr( TextBox1, Plus) = 0 And InStr( TextBox1, Moins) = 0 Then
KeyAscii = Asc(Plus)
Else
KeyAscii = 0
End If
End If
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim Pos1 As Long, Pos2 As Long
Pos1 = InStr(TextBox1.Text, Moins)
Pos2 = InStr(TextBox1.Text, Plus)
If Pos1 = 0 And Pos2 = 0 Then
MsgBox "Saisir le signe (+-) de ce nombre", vbOKOnly + vbInformation
End If
End Sub