Option Explicit
Option Compare Text
'Dim lig As Integer
Const Couljaune = &H80FFFF
Dim flag As Boolean
Dim modif As Boolean

Private Sub BoxDem_AfterUpdate()
If modif Then BoxDem.BackColor = Couljaune
End Sub

Private Sub BoxDemandeur_AfterUpdate()
If modif Then BoxDemandeur.BackColor = Couljaune
End Sub

Private Sub BoxN°Essai_AfterUpdate()
If modif Then BoxN°Essai.BackColor = Couljaune
End Sub

Private Sub BoxN°Moule_AfterUpdate()
If modif Then BoxN°Moule.BackColor = Couljaune
End Sub

Private Sub BoxNbreEmpreinte_AfterUpdate()
If modif Then BoxNbreEmpreinte.BackColor = Couljaune
End Sub

Private Sub BoxPièces_AfterUpdate()
If modif Then BoxPièces.BackColor = Couljaune
End Sub
'
'Private Sub BoxPièces_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'If modif Then BoxPièces.BackColor = Couljaune
'End Sub
'
Private Sub BoxProjet_AfterUpdate()
If modif Then BoxProjet.BackColor = Couljaune
End Sub

Private Sub BoxSemaine_AfterUpdate()
If modif Then BoxSemaine.BackColor = Couljaune
End Sub

Private Sub BoxTypeEssai_AfterUpdate()
If modif Then BoxTypeEssai.BackColor = Couljaune
End Sub

Private Sub ComboBox1_Change() 'choix des demandes pour modifications

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
modif = True
End Sub

Private Sub CommandButton1_Click() 'ajout ou modification ligne

Call controle 'controler si toutes les textbox sont completees
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
        
        'Saisir les informations modifiees si textbox sur fond jaune
        If BoxSemaine.BackColor = Couljaune Then .Cells(lig, 2) = "S" & Format(BoxSemaine.Text, "00"): .Cells(lig, 2).Interior.Color = Couljaune 'num semaine
        If BoxDem.BackColor = Couljaune Then Cells(lig, 3) = BoxDem.Text: .Cells(lig, 3).Interior.Color = Couljaune
        If BoxN°Moule.BackColor = Couljaune Then .Cells(lig, 4) = BoxN°Moule.Text: .Cells(lig, 4).Interior.Color = Couljaune
        If BoxN°Essai.BackColor = Couljaune Then .Cells(lig, 5) = BoxN°Essai.Text: .Cells(lig, 5).Interior.Color = Couljaune
        If BoxPièces.BackColor = Couljaune Then .Cells(lig, 6) = BoxPièces.Text: .Cells(lig, 6).Interior.Color = Couljaune
        If BoxTypeEssai.BackColor = Couljaune Then .Cells(lig, 7) = BoxTypeEssai.Text: .Cells(lig, 7).Interior.Color = Couljaune
        If BoxProjet.BackColor = Couljaune Then .Cells(lig, 8) = BoxProjet.Text: .Cells(lig, 8).Interior.Color = Couljaune
        If BoxDemandeur.BackColor = Couljaune Then .Cells(lig, 9) = BoxDemandeur.Text: .Cells(lig, 9).Interior.Color = Couljaune
        If BoxNbreEmpreinte.BackColor = Couljaune Then .Cells(lig, 10) = BoxNbreEmpreinte.Text: .Cells(lig, 10).Interior.Color = Couljaune
        If BoxNbrePDC.BackColor = Couljaune Then .Cells(lig, 11) = BoxNbrePDC.Text: .Cells(lig, 11).Interior.Color = Couljaune
        If BoxNbrePDDC.BackColor = Couljaune Then .Cells(lig, 12) = BoxNbrePDDC.Text: .Cells(lig, 12).Interior.Color = Couljaune
        If BoxNbreAspect.BackColor = Couljaune Then .Cells(lig, 13) = BoxNbreAspect.Text: .Cells(lig, 13).Interior.Color = Couljaune
        If BoxDateArrivéePrévisionnel.BackColor = Couljaune Then .Cells(lig, 14) = Format(BoxDateArrivéePrévisionnel.Text, "d-mmm"): .Cells(lig, 14).Interior.Color = Couljaune
        If BoxDélaiSouhaité.BackColor = Couljaune Then .Cells(lig, 15) = Format(BoxDélaiSouhaité.Text, "d-mmm"): .Cells(lig, 15).Interior.Color = Couljaune
        
        .Cells(lig, 23) = "X" 'ajout de X dans colonne modification
        
        Call trier
        Call planning
        
    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
  
        '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) = Format(BoxDateArrivéePrévisionnel.Text, "d-mmm")
        .Cells(lig, 15) = Format(BoxDélaiSouhaité.Text, "d-mmm")
        .Cells(lig, 17).NumberFormat = "d-mmm"
        .Cells(lig, 22).NumberFormat = "d-mmm"
    
        '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
        
        Call Mise_en_forme
        Call trier
    End If
    
End With

'lig = 0 'remettre à zéro la variable ligne

'Call Mise_en_forme
'Call trier

'If CommandButton1.Caption = "Modifier la ligne" Then Call planning 'cas modification ligne

'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 'si nom de l'objet commence par "Box..." on vide l'objet (ici Box correspond a une textbox)
        c.Value = vbNullString
        c.BackColor = &HFFFFFF 'remise couleur blanche
    End If
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
    .List = ListSort(.List) 'appel fonction pour trier la combobox1
    .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