Option Explicit Sub ExporterVentesParCommercial() Dim wb As Workbook Dim wsSource As Worksheet Dim wsRapport As Worksheet Dim commerciaux As Object Dim commercial As Variant Dim derniereLigne As Long Dim r As Long, ligneSortie As Long Dim ligneTotal As Long Dim nomRapport As String Dim cheminPDF As String Dim fichierPDF As String Dim tbl As ListObject Dim plageDonnees As Range Dim ancienScreenUpdating As Boolean Dim ancienDisplayAlerts As Boolean Dim nbReussites As Long Dim messageErreur As String On Error GoTo GestionErreur Set wb = ThisWorkbook Set wsSource = wb.Worksheets("Ventes") If Len(wb.Path) = 0 Then MsgBox "Enregistrez d'abord le classeur.", vbExclamation Exit Sub End If ancienScreenUpdating = Application.ScreenUpdating ancienDisplayAlerts = Application.DisplayAlerts Application.ScreenUpdating = False Application.DisplayAlerts = False cheminPDF = wb.Path & Application.PathSeparator derniereLigne = wsSource.Cells(wsSource.Rows.Count, "B").End(xlUp).Row If derniereLigne < 2 Then MsgBox "Aucune donnée de vente trouvée.", vbInformation GoTo Nettoyage End If ' Lister les commerciaux sans doublon Set commerciaux = CreateObject("Scripting.Dictionary") commerciaux.CompareMode = vbTextCompare For r = 2 To derniereLigne If Trim(CStr(wsSource.Cells(r, "B").Value)) <> "" Then commerciaux(Trim(CStr(wsSource.Cells(r, "B").Value))) = True End If Next r ' Créer un rapport par commercial For Each commercial In commerciaux.Keys nomRapport = NomFeuilleValide(CStr(commercial)) ' Ne pas écraser une feuille existante nomRapport = NomFeuilleUnique(wb, nomRapport) Set wsRapport = wb.Worksheets.Add( _ After:=wb.Worksheets(wb.Worksheets.Count)) wsRapport.Name = nomRapport ' Copier les en-têtes wsRapport.Range("A1:D1").Value = _ wsSource.Range("A1:D1").Value ligneSortie = 2 ' Copier les ventes de ce commercial For r = 2 To derniereLigne If StrComp(Trim(CStr(wsSource.Cells(r, "B").Value)), _ CStr(commercial), vbTextCompare) = 0 Then wsRapport.Cells(ligneSortie, "A").Resize(1, 4).Value = _ wsSource.Cells(r, "A").Resize(1, 4).Value ligneSortie = ligneSortie + 1 End If Next r ' Créer un tableau Excel mis en forme Set plageDonnees = wsRapport.Range("A1:D" & ligneSortie - 1) Set tbl = wsRapport.ListObjects.Add( _ xlSrcRange, plageDonnees, , xlYes) tbl.TableStyle = "TableStyleMedium2" ' Ajouter le total sous le tableau ligneTotal = ligneSortie + 1 wsRapport.Cells(ligneTotal, "C").Value = "TOTAL DES VENTES" wsRapport.Cells(ligneTotal, "D").Formula = _ "=SUM(D2:D" & ligneSortie - 1 & ")" With wsRapport.Range("C" & ligneTotal & ":D" & ligneTotal) .Font.Bold = True .Interior.Color = RGB(220, 230, 241) .Borders(xlEdgeTop).LineStyle = xlContinuous .Borders(xlEdgeTop).Weight = xlMedium End With ' Mettre en forme les colonnes wsRapport.Columns("A").NumberFormat = "dd mmm yyyy" wsRapport.Columns("D").NumberFormat = "#,##0.00" wsRapport.Columns("A:D").AutoFit ' Améliorer la lisibilité wsRapport.Columns("A").ColumnWidth = _ Application.Max(wsRapport.Columns("A").ColumnWidth, 15) wsRapport.Columns("B").ColumnWidth = _ Application.Max(wsRapport.Columns("B").ColumnWidth, 18) wsRapport.Columns("C").ColumnWidth = _ Application.Max(wsRapport.Columns("C").ColumnWidth, 20) ' Paramétrer la mise en page du PDF With wsRapport.PageSetup .Orientation = xlLandscape .PaperSize = xlPaperA4 .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = False .PrintArea = wsRapport.Range( _ "A1:D" & ligneTotal).Address .PrintTitleRows = "$1:$1" .CenterHeader = "Rapport des ventes - " & CStr(commercial) .CenterFooter = "Page &P sur &N" End With ' Exporter en PDF fichierPDF = cheminPDF & _ NomFichierValide(CStr(commercial)) & "_Rapport_Ventes.pdf" wsRapport.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=fichierPDF, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False nbReussites = nbReussites + 1 Next commercial Nettoyage: Application.ScreenUpdating = ancienScreenUpdating Application.DisplayAlerts = ancienDisplayAlerts If nbReussites > 0 Then MsgBox nbReussites & _ " rapports de ventes créés et exportés dans :" & _ vbCrLf & cheminPDF, vbInformation End If Exit Sub GestionErreur: messageErreur = Err.Description Application.ScreenUpdating = ancienScreenUpdating Application.DisplayAlerts = ancienDisplayAlerts MsgBox "Erreur : " & messageErreur, vbCritical End Sub Function NomFeuilleValide(ByVal txt As String) As String Dim caracteresInterdits As Variant Dim car As Variant caracteresInterdits = Array("/", "\", "?", "*", "[", "]", ":") For Each car In caracteresInterdits txt = Replace(txt, car, "_") Next car txt = Replace(txt, "'", "_") txt = Trim(txt) If Len(txt) = 0 Then txt = "Commercial" NomFeuilleValide = Left(txt, 31) End Function Function NomFeuilleUnique(wb As Workbook, _ ByVal nomBase As String) As String Dim candidat As String Dim compteur As Long candidat = nomBase compteur = 1 Do While FeuilleExiste(wb, candidat) compteur = compteur + 1 candidat = Left(nomBase, 31 - _ Len("_" & compteur)) & "_" & compteur Loop NomFeuilleUnique = candidat End Function Function FeuilleExiste(wb As Workbook, _ ByVal nomFeuille As String) As Boolean Dim ws As Worksheet For Each ws In wb.Worksheets If StrComp(ws.Name, nomFeuille, vbTextCompare) = 0 Then FeuilleExiste = True Exit Function End If Next ws End Function Function NomFichierValide(ByVal txt As String) As String Dim caracteresInterdits As Variant Dim car As Variant caracteresInterdits = Array("\", "/", ":", "*", "?", _ """", "<", ">", "|") For Each car In caracteresInterdits txt = Replace(txt, car, "_") Next car txt = Trim(txt) If Len(txt) = 0 Then txt = "Commercial" NomFichierValide = txt End Function