Interdir textbox et combobox vide dans userform

Bonjour à tous,

Voici le code de mon userform avec 2 textbox et 2 combobox, je voudrais obliger la saisie sur ces 4 champs

merci d'avance !

Private Sub CommandButton1_Click()
Application.EnableEvents = False
With Worksheets("GMD")
LaLigne = .UsedRange.Resize(, 4).Find("*", , , , xlRows, xlPrevious).Row + 1
            .Cells(LaLigne, 1).Value = ComboBox1
            .Cells(LaLigne, 2).Value = Replace(TextBox2, " ", vbNullString)
            .Cells(LaLigne, 3).Value = ComboBox2
            .Cells(LaLigne, 4).Value = Replace(TextBox4, " ", vbNullString)
End With
With Worksheets("Modifications")
        MaLigne = .UsedRange.Resize(, 8).Find("*", , , , xlRows, xlPrevious).Row + 1
            .Cells(MaLigne, 1).Resize(1, 8).Value = Array(Environ("USERNAME"), _
                                                              Replace(TextBox2, " ", vbNullString), _
                                                              "", Replace(TextBox4, " ", vbNullString), _
                                                              "Création du document " & Replace(TextBox2, " ", vbNullString), _
                                                              Date, _
                                                              Hour(Now) & ":" & Minute(Now), "nouveau document")
End With
Me.Hide
  Application.EnableEvents = True
    End Sub

Private Sub TextBox2_Change()

End Sub

Private Sub TextBox4_Change()

End Sub

Private Sub UserForm_Initialize()

    With Worksheets("Listes déroulantes") 'Ici, il faut mettre le bon nom de l'onglet
    Me.ComboBox2.List = Application.Transpose(.Range("A2:A" & .Cells(Rows.Count, 1).End(xlUp).Row))
    End With

    With Worksheets("Listes déroulantes") 'Ici, il faut mettre le bon nom de l'onglet
    Me.ComboBox1.List = Application.Transpose(.Range("B2:B" & .Cells(Rows.Count, 2).End(xlUp).Row))
    End With

End Sub

Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

Dim rg As Range

If TextBox2.Text = "" Then
Exit Sub
End If
Set rg = Sheets("GMD").Range("B6:B2000").Find(Replace(TextBox2, " ", vbNullString), lookat:=xlWhole)

If Not rg Is Nothing Then

    MsgBox "document existant"
    Cancel = True

End If

End Sub

Bonjour Nananinanana, bonjour le forum,

En début de code du CommandButton de validadion de ta UserForm :

Dim CTRL As Control 'déclare la variable CTRL (ConTRôLe)

For Each CTRL In Me.Controls 'boucle sur touts les contrôles de l'UserForm en cours
    'condition 1 : si le contrôle est une Combobox ou une TextBox
    If TypeOf CTRL Is MSForms.ComboBox Or TypeOf CTRL Is MSForms.TextBox Then
        If CTRL.Value = "" Then 'condition 2 : si le contrôle est vide
            CTRL.SetFocus 'place le curseur dans le controle
            MsgBox "Vous devez renseigner ce champ !" 'message
            Exit Sub 'sort de la procédure
        End If 'fin de la condition 2
    End If 'fin de la condition 1
Next CTRL 'prochain contrôle de la boucle

Suivi du reste de ton code...

Merci ThauTheme,

Plus que parfait !

Rechercher des sujets similaires à "interdir textbox combobox vide userform"