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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| Sub Rappel()
'Declare Variables
Dim TsrFichOri As String, TsrTabEnvoiManuel As String, RefClient As String
Dim strInputBox As String
Dim rowNumber As Long
Dim EmailClient As String, NomClient As String, ComStru As String
Dim Montant As Currency
Dim FirstDate As Date
Dim RapFichDes As String, RapFichDesPDF As String
Dim RapTabDetails As String
Dim RapDisqDes As String
Dim RapRepDes As String
Dim FirstRow As Currency, CrtRow As Currency, LastRow As Currency
RapDisqDes = "S:\"
RapRepDes = "Dossiers C&C\Listes\Rappels\Rappels envoyés\"
'Initialize Variables
Range("A1").Select
TsrFichOri = ActiveWorkbook.Name
TsrTabEnvoiManuel = "Envoi manuel"
Windows(TsrFichOri).Activate
Worksheets(TsrTabEnvoiManuel).Activate
RefClient = InputBox("Veuillez entrer la référence du client", "Module d'envoi de rappel")
'Exit Macro if User Presses Cancel
If RefClient = "" Then Exit Sub
'Find first row with reference
'https://www.automateexcel.com/excel/find-all-instances-with-vba/
ActiveSheet.Outline.ShowLevels RowLevels:=3
rowNumber = Columns(1).Find(What:=RefClient, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
ActiveSheet.Outline.ShowLevels RowLevels:=2
Rows(rowNumber).ShowDetail = True
FirstDate = Cells(rowNumber, 6).Value
FirstRow = rowNumber
'Find row with "Total " & RefClient
rowNumber = Columns(1).Find(What:="Total " & RefClient, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
'Extract necessary values
EmailClient = Cells(rowNumber, 3).Value
NomClient = Cells(rowNumber, 4).Value
Montant = Cells(rowNumber, 11).Value
ComStru = Cells(rowNumber - 1, 12).Value
LastRow = rowNumber
'Copy Headline from Original Workbook to New Workbook
RapFichDes = NomClient & " - Rappel 1 au " & Format(Date, "YYYY MM DD") & ".xlsx"
RapFichDesPDF = NomClient & " - Rappel 1 au " & Format(Date, "YYYY MM DD") & ".pdf"
Workbooks.Add.SaveAs Filename:=RapDisqDes & RapRepDes & RapFichDes
RapTabDetails = "Details Prestations"
ActiveSheet.Name = RapTabDetails
Dim shtTarget As Worksheet
Set shtTarget = Workbooks(RapFichDes).Worksheets(RapTabDetails)
With Workbooks(TsrFichOri).Worksheets(TsrTabEnvoiManuel)
.Range(.Cells(1, 1), .Cells(1, 2)).Copy Destination:=shtTarget.Range(shtTarget.Cells(1, 1), shtTarget.Cells(1, 2))
.Range(.Cells(1, 4), .Cells(1, 6)).Copy Destination:=shtTarget.Range(shtTarget.Cells(1, 3), shtTarget.Cells(1, 5))
.Range(.Cells(1, 8), .Cells(1, 11)).Copy Destination:=shtTarget.Range(shtTarget.Cells(1, 6), shtTarget.Cells(1, 9))
.Range(.Cells(1, 13), .Cells(1, 14)).Copy Destination:=shtTarget.Range(shtTarget.Cells(1, 10), shtTarget.Cells(1, 11))
End With
For CrtRow = FirstRow To LastRow
With Workbooks(TsrFichOri).Worksheets(TsrTabEnvoiManuel)
If CrtRow < LastRow Then
.Range(.Cells(CrtRow, 1), .Cells(CrtRow, 2)).Copy Destination:=shtTarget.Range(shtTarget.Cells(CrtRow - FirstRow + 2, 1), shtTarget.Cells(CrtRow - FirstRow + 2, 2))
.Range(.Cells(CrtRow, 4), .Cells(CrtRow, 6)).Copy Destination:=shtTarget.Range(shtTarget.Cells(CrtRow - FirstRow + 2, 3), shtTarget.Cells(CrtRow - FirstRow + 2, 5))
End If
.Range(.Cells(CrtRow, 8), .Cells(CrtRow, 11)).Copy Destination:=shtTarget.Range(shtTarget.Cells(CrtRow - FirstRow + 2, 6), shtTarget.Cells(CrtRow - FirstRow + 2, 9))
.Range(.Cells(CrtRow, 13), .Cells(CrtRow, 14)).Copy Destination:=shtTarget.Range(shtTarget.Cells(CrtRow - FirstRow + 2, 10), shtTarget.Cells(CrtRow - FirstRow + 2, 11))
End With
Next CrtRow
With shtTarget.Range(shtTarget.Cells(2, 9), shtTarget.Cells(CrtRow - FirstRow, 9))
.FormulaR1C1 = "=IF(TODAY()-RC[-4]>365,RC[-1]*28.98,RC[-1]*10)"
.Copy: .PasteSpecial Paste:=xlPasteValues
End With
Application.CutCopyMode = False
With shtTarget.Range(shtTarget.Cells(LastRow + 2 - FirstRow, 6), shtTarget.Cells(LastRow + 2 - FirstRow, 9))
.Copy: .PasteSpecial Paste:=xlPasteValues
End With
Application.CutCopyMode = False
' Mise en forme du fichier
With shtTarget.Columns("B:K")
.EntireColumn.AutoFit
End With
With shtTarget.Columns("I:I")
.Style = "Currency"
End With
Rows("1:1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False ' force le contenu à s'adapter aux dimensions de la cellule
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A1").ColumnWidth = 5
' Définition du périmètre à imprimer
ActiveSheet.PageSetup.Orientation = xlLandscape ' Orientation paysage
ActiveSheet.PageSetup.PrintArea = Range(Cells(1, 1), Cells(LastRow + 2 - FirstRow, 11)).Address
' Impression de la feuille en PDF.
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=RapDisqDes & RapRepDes & RapFichDesPDF, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub ' Rappel() |
Partager