Erreur dans bouton commande quand on ne rempli rien dans le userform

Cc le forum

petit probleme avec le code de mon bouton .

quand je replie le formulaire dans le userform et que clique sur le bouton, tout se passe bien

mais quand je ne remplie rien et que je clique sur le bouton il envoi un message d'erreur et souligne ma ligne With wb_clients.Sheets(Me.ComboBox1.Value)

voici ci-joint le code de mon bouton et aussi celui de mon userform pour comprendre certain objets

Private Sub CommandButton1_Click()
If ComboBox1.Value = "" Then
    MsgBox " Veuillez rentrer le nom svp"

If ComboBox2.Value = "" Then
    MsgBox " Veuillez rentrer le nom svp"

    With wb_clients.Sheets(Me.ComboBox1.Value)
        rw2 = .Cells(Rows.Count, "A").End(xlUp).Row + 1
        .Range("A" & rw2) = ComboBox1.Value
        .Range("B" & rw2) = TextBox5.Value
        .Range("C" & rw2) = TextBox2.Text
        .Range("D" & rw2) = TextBox4.Text
        .Range("E" & rw2) = TextBox6.Text
        .Range("F" & rw2) = TextBox3.Text
        .Range("H" & rw2) = ComboBox2.Text
        .Range("G" & rw2) = TextBox10.Text

    End With

    With wb_avions.Sheets(Me.ComboBox2.Value)
        rw1 = .Cells(Rows.Count, "A").End(xlUp).Row + 1
        .Range("A" & rw2) = ComboBox1.Value
        .Range("B" & rw2) = TextBox5.Value
        .Range("C" & rw2) = TextBox2.Text
        .Range("D" & rw2) = TextBox4.Text
        .Range("E" & rw2) = TextBox6.Text
        .Range("F" & rw2) = TextBox3.Text
        .Range("H" & rw2) = ComboBox2.Text
        .Range("G" & rw2) = TextBox10.Text
    End With

    ComboBox1.Text = ""
    ComboBox2.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
    TextBox10.Text = ""
    TextBox5.Text = ""
    TextBox6.Text = ""
    TextBox4.Text = ""

    MsgBox ("ENREGISTREMENT EFFECTUER")
    End If
End If
End Sub
Private Sub Userform_Initialize()
    Dim sh As Worksheet

    Set wb_avions = ThisWorkbook  '<--- AVION
    Set wb_clients = Workbooks("C-CLIENT.xlsm")

    For Each sh In wb_avions.Sheets
        Me.ComboBox2.AddItem sh.Name
    Next sh

    For Each sh In wb_clients.Sheets
        Me.ComboBox1.AddItem sh.Name
    Next sh
End Sub

Merci de me donner des idées

Bonjour,

  • Tu testes ta combox1 pour savoir si elle est vide
  • Tu envoies un message
  • Puis ton code continue

Là c'est pas bon si ta combobox est vide alors aprés la msgbox tu dois sortir du code.

Exit sub

Et le end if vient juste après.

Ensuite même chose pour combobox2 avec exit sub et end if

If ComboBox1.Value = "" Then
    MsgBox " Veuillez rentrer le nom svp"
  exit sub
  end if

If ComboBox2.Value = "" Then
    MsgBox " Veuillez rentrer le nom svp"
    Exit Sub
    end if
    

MErci a vous

Rechercher des sujets similaires à "erreur bouton commande quand rempli rien userform"