Option Explicit
Dim lig As Integer
Dim flag As Boolean

Private Sub ComboBox1_Change()

With Feuil1
    On Error Resume Next
    lig = WorksheetFunction.Match(ComboBox1.Value, .Range("E5:E" & Range("E" & 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()
Dim dlg As Integer

'modification ligne num semaine
If CommandButton1.Caption = "Modifier la Ligne" Then
    Feuil1.Cells(lig, 2) = "S" & Format(BoxSemaine.Text, "00") 'num semaine
    Exit Sub
End If

'ajout nouvelle ligne de donnees

Call controle
If flag = True Then Exit Sub

With Feuil1 'code name de onglet previsio

    .Unprotect 'déprotéger la feuille
    dlg = .Range("B" & Rows.Count).End(xlUp).Row 'Recherche de la 1ère ligne vide
    If dlg = 2 Then dlg = 3 'si derniere ligne =2 on met la variable a valeur 3
    
    'Saisir les informations
    .Cells(dlg + 1, 2) = "S" & Format(BoxSemaine.Text, "00") 'num semaine
    .Cells(dlg + 1, 3) = BoxDem.Text
    .Cells(dlg + 1, 4) = BoxN°Moule.Text
    .Cells(dlg + 1, 5) = BoxN°Essai.Text
    .Cells(dlg + 1, 6) = BoxPièces.Text
    .Cells(dlg + 1, 7) = BoxTypeEssai.Text
    .Cells(dlg + 1, 8) = BoxProjet.Text
    .Cells(dlg + 1, 9) = BoxDemandeur.Text
    .Cells(dlg + 1, 10) = BoxNbreEmpreinte.Text
    .Cells(dlg + 1, 11) = BoxNbrePDC.Text
    .Cells(dlg + 1, 12) = BoxNbrePDDC.Text
    .Cells(dlg + 1, 13) = BoxNbreAspect.Text
    .Cells(dlg + 1, 14) = BoxDateArrivéePrévisionnel.Text
    .Cells(dlg + 1, 15) = BoxDélaiSouhaité.Text

    'Ajout formules en Colonne A et T
    If dlg + 1 > 4 Then
        .Cells(4, 1).AutoFill Destination:=Feuil1.Range("A4:A" & dlg + 1), Type:=xlFillDefault 'recopier formule en colonne A sur ligne ajoutee
        .Cells(4, 20).AutoFill Destination:=Feuil1.Range("T4:T" & dlg + 1), Type:=xlFillDefault 'recopier formule en colonne T sur ligne ajoutee
    End If

End With
Call trier

Call Mise_en_forme
'Remise à zéro de la Userform
'Reprotéger la feuille
Feuil1.Protect
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 i As Integer
Dim c As Control

With ComboBox1
    .List = Feuil1.Range("E5:E" & Range("E" & Rows.Count).End(xlUp).Row).Value
    .Enabled = True
    '.DropDown
    .Style = fmStyleDropDownList
    '.Style = fmStyleDropDownCombo
End With

For Each c In Me.Controls 'on boucle sur chacun des objets sur l'USF
    If c.Name Like "Box*" Then c.Value = vbNullString: c.Locked = True
    If c.Name = "BoxSemaine" Then c.Locked = False
Next c
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" 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