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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| Option Explicit
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim i&
For i = 1 To Len(TextBox1.Value)
If InStr("1234567890.", Mid(TextBox1.Value, i, 1)) = 0 Then
TextBox1.Value = ""
MsgBox "Saisie invalide"
Cancel = True
Exit For
End If
Next i
End Sub
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim bFlag As Boolean
With TextBox1
'si pas encore de point
If Len(.Text) - Len(Replace(.Text, ".", "")) = 0 Then
'selon si la touche Maj est appuyée
Select Case Shift
Case 0 'Maj non appuyée
Select Case KeyCode
'bFlag est True si SelStart > 0
Case 110: bFlag = (.SelStart > 0) 'point clavier numérique
Case 188: KeyCode = 110: bFlag = (.SelStart > 0) 'virgule
End Select
Case 1 'Maj appuyée
If KeyCode = 190 Then bFlag = (.SelStart > 0) 'point clavier alpha
End Select
'si bFlag = True alors : on a un seul point ET on vient de le saisir ET il n'est pas en première position
If bFlag Then
'donc on affiche .5
.Text = .Text & ".5"
KeyCode = 0
End If
'sinon, si déjà un point
Else
'Donne l'accès uniquement aux touches : Retour arrière, Suppr, flèche gauche, flèche droite, TAB, Entrée
If KeyCode <> 8 And KeyCode <> 46 And KeyCode <> 37 And KeyCode <> 39 And KeyCode <> 9 And KeyCode <> 13 Then KeyCode = 0
End If
End With
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr("1234567890.", Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If Right(TextBox1.Text, 1) = "." Then TextBox1.Text = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
End Sub |
Partager