1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| Option Explicit
Dim docDep, col, f, Ln, Lgn, commercial
Dim chemin
Dim Outlook As Object
Dim Mail As Object
Sub Split()
Set docDep = ActiveSheet
'Suppression des feuilles portant le nom des commerciaux
For Ln = 2 To Range("A" & Rows.Count).End(xlUp).Row
For Each f In Sheets
If f.Name = Cells(Ln, "A").Value Then
Application.DisplayAlerts = False
f.Delete
End If
Next f
Next Ln
'Split des données selon les commerciaux
col = Cells(1, Columns.Count).End(xlToLeft).Column
For Ln = 2 To Range("A" & Rows.Count).End(xlUp).Row 'pour la ligne 2 à la dernière ligne
commercial = ""
For Each f In Sheets
If f.Name = Cells(Ln, "A").Value Then
Lgn = f.Cells(Rows.Count, "A").End(xlUp)(2).Row
Range(Cells(Ln, "A"), Cells(Ln, col)).Copy f.Cells(Lgn, "A")
commercial = Cells(Ln, "A").Value
End If
Next f
If commercial = "" Then
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = docDep.Cells(Ln, "A").Value
With docDep
.Range(.Cells(1, 1), .Cells(1, col)).Copy Cells(1, "A")
.Range(.Cells(Ln, 1), .Cells(Ln, col)).Copy Cells(2, "A")
.Activate
End With
End If
Next Ln
End Sub
Sub EnregistrerLesOnglets()
chemin = ThisWorkbook.Path & "\"
For Each f In Sheets
f.Copy
ActiveWorkbook.SaveAs Filename:=chemin & f.Name
Set Outlook = CreateObject("Outlook.Application")
Set Mail = Outlook.CreateItem(0)
With Mail
.To = Cells(2, 5)
.Subject = "Sujet"
.Body = "Corps"
.Attachments.Add chemin & f.Name
.Display
End With
ActiveWorkbook.Close False
Next f
End Sub |
Partager