Option Explicit
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'patricktoulon
'textbox (million millier mille) formaté dynamico aux taper des touches
'la touche (BACK ;retour en arrière) est fonctionnelle
Dim v 'j'ai juste ajouté ce dim car le code planté
With TextBox1
v = Replace(.Value, " ", "")
Select Case KeyCode
Case 96 To 105, 48 To 57
If KeyCode < 96 Then KeyCode = KeyCode + 48
If InStr(1, v, ",") > 0 Then
If Len(Split(v, ",")(1)) = 2 Then KeyCode = 0: Exit Sub
Else
'v = v & Chr(KeyCode - 48): KeyCode = 0
v = Split(Format(v & Chr(KeyCode - 48), "#,##0.00"), ",")(0): KeyCode = 0
End If
.Value = v
Case 110, 188: KeyCode = 188 ' la virgule prime
If InStr(1, .Value, ",") > 0 Or .Value = "" Then KeyCode = 0
v = Split(Format(v, "#,##0.00"), ",")(0)
.Value = v & ",": KeyCode = 0
Case 8 'gestion de la touche back
If v = "" Then KeyCode = 0: Exit Sub
v = Left(v, Len(v) - 1)
If v = "" Then Exit Sub
If InStr(1, v, ",") = 0 Then v = Split(Format(v, "#,##0.00"), ",")(0)
.Value = v: KeyCode = 0
Case 13, 9
.Value = Format(.Value, "#,##0.00") 'au cas ou on sort et que ce n'est pas fini
Case Else: KeyCode = 0 'Toutes les autres touches sont bloquées
End Select
End With
End Sub