Option Explicit
Dim Ws As Worksheet
Dim SelectedIndex As Integer
Private Sub Montant_HT_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Montant_HT = CDbl(Qté.Text) * CDbl(PrixUnité.Text)
    Montant_HT = Format(Montant_HT.Value, "0€")
End Sub
Private Sub TexBox6_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox6 = Format(TextBox6.Value, "000")
End Sub
Private Sub PrixUnité_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    PrixUnité = Format(Me.PrixUnité.Value, "0€")
End Sub
Private Sub UserForm_Initialize()
    Dim Lst, Lst2
    Jour = Date
    Set Lst = ThisWorkbook.Worksheets("Client").ListObjects(1)    ' ca c'est ton tableau structuré client
    ComboBox1.List = Lst.DataBodyRange.Columns(3).Value    ' la combo.list  prend la colonnes 3 du tableau
    Désignation.List = Sheets("Prestation").Range("ListForfaits").Value
    Set Lst2 = ThisWorkbook.Worksheets("Archive Devis").ListObjects(1)    ' ca c'est ton tableau structuré Archive Devis
    TextBox6 = Year(Date) & "-" & Format(Lst2.ListRows.Count + 1, "000")    'si le tableau n'est pas vide
End Sub
Private Sub ComboBox1_Change()
    Dim Ligne As Long
    Ligne = Me.ComboBox1.ListIndex + 2    'pour faire correspondre l'index du nom sélectionné avec son numéro de ligne dans la feuille Excel
    With Sheets("Client")
        ComboBox2 = .Range("B" & Ligne)
        NOM = .Range("C" & Ligne)
        ADRESSE = .Range("D" & Ligne)
        ComboBox3 = .Range("F" & Ligne)
        TextBox1 = .Range("I" & Ligne)
    End With
End Sub
Private Sub Désignation_Change()
    Dim Ligne As Long
    'Ligne = Me.Désignation.ListIndex + 2 'pour faire correspondre l'index du nom sélectionné avec son numéro de ligne dans la feuille Excel
    '
    'With Sheets("Prestation")
    ''on peut remplir les champs tels que enregistrés dans la feuille
    '
    '
    '
    '    End With
    Me.PrixUnité = Me.Désignation.Column(1)
End Sub
Private Sub Estimatif_Click():    Estimatif_Coût.Show: End Sub
Private Sub ajouter_Click()
    Dim lr As ListRow, Ligne&, lig As Integer, tablo:
    Ligne = 2
    With Sheets("Devis")
        .Cells(Ligne + 2, "B") = TextBox6
        .Cells(Ligne, "C") = Jour
        .Cells(Ligne + 4, "C") = NOM
        .Cells(Ligne + 5, "C") = ADRESSE
        .Cells(Ligne + 6, "C") = CP & " " & ComboBox3
        .Cells(Ligne + 9, "B") = TextBox1
        .Cells(Ligne + 12, "A") = PriseEnCharge
        .Cells(Ligne + 14, "A") = Trajet
        .Cells(Ligne + 16, "A") = FinCourse
        With .ListObjects("Devis")
            With .ListRows.Add()
                If Désignation.ListIndex > -1 Then .Range(1, 1) = Désignation.Value
                If IsNumeric(Qté.Text) Then .Range(1, 2) = CDbl(Qté.Text)
                If IsNumeric(PrixUnité.Text) Then .Range(1, 3) = CDbl(PrixUnité.Text)
                If IsNumeric(Montant_HT) Then .Range(1, 4) = CDbl(Montant_HT.Text)
            End With
        End With
    
    End With
End Sub
Private Sub ArchiveDevis_Click()
    Dim A
    With Sheets("Archive Devis").ListObjects(1).ListRows.Add()
        A = Array(TextBox6.Value, Jour.Value, , NOM.Value, ADRESSE.Value, CP & " " & ComboBox3.Value, TextBox1.Value, _
                  Désignation.Value, CDbl(Qté.Text), CDbl(PrixUnité.Text), Int(CDbl(PrixUnité.Text) * CDbl(Qté.Text)), _
                  PriseEnCharge.Text, Trajet.Text, FinCourse.Text, "")
        
        .Range.Resize(, UBound(A) + 1).Value = A
    End With
End Sub
Private Sub Cp_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Me.CP.Value = Format(Me.CP.Value, "00 000 ")
End Sub
Private Sub QUITTER_Click()
    Dim sh As Worksheet: Set sh = Worksheets("Devis")
    With sh.ListObjects("Devis")
        If Not .DataBodyRange Is Nothing Then
            If MsgBox("Êtes-vous certain de vouloir vider le tableau ?", _
                      vbYesNo, "Demande de confirmation") = vbYes Then
                .DataBodyRange.Delete
                sh.Range("C6, C7, C8, B11, A14, A16, A18") = ""
            End If
        End If
    End With
    Unload Me: Accueil.Show 0
End Sub
Private Sub ComboBox3_Change()
    Me.CP.Value = Format(Me.ComboBox3.Column(1), "00 000")
End Sub
Private Sub cancel_Click()
    ActiveCell = TextBox1.Value
    Unload Me
End Sub
Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Calendar.Affiche Me, Me.TextBox1.Name, ActiveControl.Name
    Cancel = True
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Me.TextBox1.Value = Format(Me.TextBox1.Value, "dddd d mmmm yyyy ")
End Sub