Option Explicit
Option Compare Text
Dim lig As Integer
Dim flag As Boolean

Private Sub ComboBox1_Change()

With Feuil1
    On Error Resume Next
    lig = WorksheetFunction.Match(ComboBox1.Value, .Range("C5:C" & Range("C" & Rows.Count).End(xlUp).Row).Value, 0) + 4
    If lig = 0 Then Exit Sub
    On Error GoTo 0
    BoxSemaine = Right(.Cells(lig, 2).Value, 2)
    BoxDem = .Cells(lig, 3).Value
    BoxN°Moule = .Cells(lig, 4).Value
    BoxN°Essai = .Cells(lig, 5).Value
    BoxPièces = .Cells(lig, 6).Value
    BoxTypeEssai = .Cells(lig, 7).Value
    BoxProjet = .Cells(lig, 8).Value
    BoxDemandeur = .Cells(lig, 9).Value
    BoxNbreEmpreinte = .Cells(lig, 10).Value
    BoxNbrePDC = .Cells(lig, 11).Value
    BoxNbrePDDC = .Cells(lig, 12).Value
    BoxNbreAspect = .Cells(lig, 13).Value
    BoxDateArrivéePrévisionnel = .Cells(lig, 14).Value
    BoxDélaiSouhaité = .Cells(lig, 15).Value
End With

End Sub

Private Sub CommandButton1_Click() 'ajout ou modification ligne

Call controle
If flag = True Then Exit Sub

With Feuil1 'code name de onglet prevision
    .Unprotect 'déprotéger la feuille
        
    If CommandButton1.Caption = "Modifier la ligne" Then 'cas modification ligne
                
    Else: lig = .Range("B" & Rows.Count).End(xlUp).Row + 1 'cas ajout - Recherche de la 1ère ligne vide
        If lig <= 4 Then lig = 5 'si derniere ligne <= 4 on met la variable a valeur 5
    End If
  
    'Saisir les informations
    .Cells(lig, 2) = "S" & Format(BoxSemaine.Text, "00") 'num semaine
    .Cells(lig, 3) = BoxDem.Text
    .Cells(lig, 4) = BoxN°Moule.Text
    .Cells(lig, 5) = BoxN°Essai.Text
    .Cells(lig, 6) = BoxPièces.Text
    .Cells(lig, 7) = BoxTypeEssai.Text
    .Cells(lig, 8) = BoxProjet.Text
    .Cells(lig, 9) = BoxDemandeur.Text
    .Cells(lig, 10) = BoxNbreEmpreinte.Text
    .Cells(lig, 11) = BoxNbrePDC.Text
    .Cells(lig, 12) = BoxNbrePDDC.Text
    .Cells(lig, 13) = BoxNbreAspect.Text
    .Cells(lig, 14) = BoxDateArrivéePrévisionnel.Text
    .Cells(lig, 15) = BoxDélaiSouhaité.Text

    'Ajout formules en Colonne A et T
    If lig > 4 Then
        .Cells(4, 1).AutoFill Destination:=Feuil1.Range("A4:A" & lig), Type:=xlFillDefault 'recopier formule en colonne A sur ligne ajoutee
        .Cells(4, 20).AutoFill Destination:=Feuil1.Range("T4:T" & lig), Type:=xlFillDefault 'recopier formule en colonne T sur ligne ajoutee
    End If
    
End With

lig = 0 'remettre à zéro la variable ligne

Call trier
Call Mise_en_forme

'Reprotéger la feuille
Feuil1.Protect

'Remise à zéro de la Userform
Dim c As Control
For Each c In Me.Controls 'on boucle sur chacun des objets sur l'USF
    If c.Name Like "Box*" Then c.Value = vbNullString 'si nom de l'objet commence par "Box..." on vide l'objet (ici Box correspond a une textbox)
Next c
End Sub

Private Sub CommandButton2_Click() 'annuler
Unload Me 'Unload Me 'pour ferme l'USF automatiquement
End Sub

Private Sub CommandButton3_Click() 'modification
Dim cel As Range

With ComboBox1
    For Each cel In Feuil1.Range("C5:C" & Feuil1.Range("C" & Rows.Count).End(xlUp).Row)
        If cel.Offset(0, -2).Value <> "DEM terminée" And cel.Offset(0, -2).Value <> "DEM annulée" Then
            ComboBox1.AddItem cel.Value
        End If
    Next cel
    .Value = vbNullString
    .Enabled = True
    '.DropDown
    .Style = fmStyleDropDownList
End With

CommandButton1.Caption = "Modifier la ligne"
End Sub

Private Sub UserForm_Initialize()
ComboBox1.Enabled = False
End Sub

Private Sub controle()
Dim c As Control

For Each c In Me.Controls
    
    If TypeName(c) <> "Label" And TypeName(c) <> "Combobox" Then
        If c.Value = vbNullString Then MsgBox "Veuillez compléter la rubrique " & c.Tag, , "Rubrique incomplète": flag = True: Exit Sub
    End If
Next c
flag = False
End Sub
