Private Sub TextBox1_Change() Dim masque As String, plein As String Static anc As String masque = "### ## ## ##" plein = "000 00 00 00" If (TextBox1.Text & Mid(plein, Len(TextBox1.Text) + 1)) Like masque Then If Mid(masque, Len(TextBox1) + 1, 1) = " " Then If TextBox1.Text >anc Then TextBox1.Text TextBox1.Text & " " End If anc = TextBox1.Text Exit Sub End If TextBox1.Text = anc End Sub
je vois que tu reviens chez nous après 4 ans d'absence et uniquement après avoir tenté en vain sur un site que tu fréquentes bien plus souvent (on se demande pour quelle vraie raison )
Option Explicit Dim Posinex As Byte, SuppInd As Byte Dim tablo(1 To 12) As String * 1 Private Sub TextBox1_Change() Label2.Caption = TextBox1.SelStart 'Posinex End Sub Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) '___ __ __ __ '123456789012 If KeyCode = 8 Then '-- BackSpace Key Posinex = TextBox1.SelStart Label4.Caption = Posinex Select Case TextBox1.SelStart Case 1, 2, 3, 6, 9, 12 tablo(TextBox1.SelStart) = "_" consttext TextBox1.SelStart = Posinex - 1 Case 5, 8, 11 tablo(TextBox1.SelStart) = "_" consttext TextBox1.SelStart = Posinex - 2 Case 4, 7, 10 tablo(TextBox1.SelStart) = " " consttext TextBox1.SelStart = Posinex - 1 End Select KeyCode = 0 End If '___ __ __ __ '123456789012 If KeyCode = 46 Then '-- DEL Key Posinex = TextBox1.SelStart SuppInd = TextBox1.SelStart Label6.Caption = Posinex Select Case TextBox1.SelStart Case 0, 1, 4, 7, 10, 11 tablo(TextBox1.SelStart + 1) = "_" consttext TextBox1.SelStart = Posinex + 1 Case 2, 5, 8 tablo(TextBox1.SelStart + 1) = "_" consttext TextBox1.SelStart = Posinex + 2 Case 3, 6, 9 tablo(TextBox1.SelStart + 1) = " " consttext TextBox1.SelStart = Posinex + 1 End Select KeyCode = 0 End If End Sub Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii < 48 Or KeyAscii > 57 Or _ Len(Replace(TextBox1, "_", "")) = 12 Then KeyAscii = 0 Exit Sub End If Posinex = TextBox1.SelStart Label10.Caption = SuppInd '___ __ __ __ '123456789012 Label8.Caption = Chr(KeyAscii) Select Case TextBox1.SelStart Case 0, 1, 4, 7, 10, 11 tablo(TextBox1.SelStart + 1) = Chr(KeyAscii) consttext TextBox1.SelStart = Posinex + 1 Case 2, 5, 8 tablo(TextBox1.SelStart + 1) = Chr(KeyAscii) consttext TextBox1.SelStart = Posinex + 2 End Select KeyAscii = 0 End Sub Private Sub UserForm_Initialize() Dim i As Byte For i = 1 To 12 tablo(i) = "_" Next i tablo(4) = " " tablo(7) = " " tablo(10) = " " consttext TextBox1.SelStart = 0 End Sub Private Sub consttext() Dim i As Byte TextBox1 = "" For i = 1 To 12 TextBox1 = TextBox1 & tablo(i) Next i End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionje suis assez enclin à m'opposer à de tels procédés, alors qu'il est bien plus simple de forcer la saisie de chiffres uniquement (de très nombreux exemples sur ce forum) et leur nombre (MaxLength) puis de formater à la sortie.
Il est perfectible. J'ai par exemple limité l'effacement aux seuls backspaces entamés depuis la fin.
Voilà. A toi de jouer à le perfectionner, si tu le veux...
Si tu veux développer la tienne, on travaillera dessus
Si tu veux développer la mienne, tu travailleras dessus
Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii = 8 Then Exit Sub If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0 End Sub Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean) If Not Replace(TextBox4.Text, " ", "") Like "#########" Then MsgBox "saisie erronée - saisissez 8 chiffres" Cancel = True End If TextBox4.Text = Format(TextBox4.Text, "### ## ## ##") End Sub
Private Sub TextBox1_Change() Dim Val As String Val = TextBox1.Text If Len(Val) 3 Or Len(Val) 6 Or Len(Val) = 9 Then Val = Val & " " End If TextBox1.Text = Val End Sub