Control de saisie sur UserForm

bonjour,

Mon UserForm me permet de faire un control de saisie s'il manque une info. Tout fonctionne sauf que si j'oublie de remplir une TextBox

le message "Saisie incomplète !" apparait bien, je clique sur Ok, et là mon userform se ferme alors que je devrais retrouner sur ce dernier.

Voici ce que j'ai écris

Private Sub CmdOK_Click()
Dim Ctl As Control
Dim Lign As Long
Dim Flag As Boolean

For Each Ctl In Me.Controls
    If TypeOf Ctl Is MSForms.TextBox Then
        If Ctl.Text = "" Then
            MsgBox "Saisie incomplète !"
            With Ctl
                .BorderStyle = 1
                .BackColor = RGB(241, 221, 220)
                .BorderColor = RGB(255, 0, 0)
                .SetFocus
            End With
            Flag = True
            Exit For
        End If
    End If
Next Ctl

If Not Flag Then
    With Worksheets("Listes")
        Lign = .Cells(.Rows.Count, "K").End(xlUp).Row + 1
        .Cells(Lign, 11) = TxtBoxCategorie
        With .Cells(Lign, 12)
            .Value = TxtBoxValidite
            .HorizontalAlignment = xlCenter
        End With
    End With
End If
Trie_categorie
UserForm2.Ajout_Categorie
Unload Me
End Sub

Pouvez-vous me dire comment faire ?

Merci

Frederique

Bonjour

C'est normal il faut inclure les 3 dernières lignes dans ton If

Modifies la fin de ta macro

If Not Flag Then
    With Worksheets("Listes")
        Lign = .Cells(.Rows.Count, "K").End(xlUp).Row + 1
        .Cells(Lign, 11) = TxtBoxCategorie
        With .Cells(Lign, 12)
            .Value = TxtBoxValidite
            .HorizontalAlignment = xlCenter
        End With
    End With
Trie_categorie
    UserForm2.Ajout_Categorie
    Unload Me
 End If
End Sub

Ou autre solution

A la place de Exit For utilises Exit Sub

En plus cette syntaxe permet de t'affranchir de la variable Flag --> Exit le If Not Flag Then ..... End If

Re-,

Salut Banzaï,

Pour ma part, j'ai compris ceci :

For Each Ctl In Me.Controls
    If TypeOf Ctl Is MSForms.TextBox Then
        If Ctl.Text = "" Then
            MsgBox "Saisie incomplète !"
            Ctl.SetFocus
            Exit Sub
            With Ctl
                .BorderStyle = 1
                .BackColor = RGB(241, 221, 220)
                .BorderColor = RGB(255, 0, 0)
                .SetFocus
            End With
            Flag = True
            Exit For
        End If
    End If
Next Ctl

bonjour Banzai64,

Merci pour l'info

Frederique

Rechercher des sujets similaires à "control saisie userform"