Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub Road()
- Dim Worker_ID As Variant
- Dim Worker_name As Variant
- Dim Car_ID As Variant
- Dim Worker_mail As Variant
- Dim Full_payment As Variant
- Dim Company_payment As Variant
- Dim Worker_payment As Variant
- Dim Name As Variant
- Dim PdfFile As String
- Dim FolderPath As String
- Dim i As Integer, j As Integer
- Dim CurrentDate As String
- Dim FSO As Object
- Dim totalFee As Double
- Dim vat As Double
- Dim RowCounter As Integer
- Dim startRow As Integer
- Dim CategoryRow As Integer
- ' Create folder based on current date and time
- Set FSO = CreateObject("Scripting.FileSystemObject")
- CurrentDate = Format(Now, "yyyy-mm-dd_hh-nn-ss")
- FolderPath = "C:\Users\Ori\Documents\work\road 6\" & CurrentDate & "\"
- ' Check if the folder exists, if not, create it
- If Not FSO.FolderExists(FolderPath) Then
- FSO.CreateFolder (FolderPath)
- End If
- ' Define the template row (assuming Row 61 in "Show" sheet contains the desired formatting)
- Dim TemplateRow As Range
- Set TemplateRow = Sheets("Show").Range("A61:I61")
- For i = 2 To Sheets("DATA").Range("A65000").End(xlUp).Row
- ' Initialize totals
- totalFee = 0
- ' Insert the worker id
- Worker_ID = Sheets("DATA").Range("A" & i).Value
- Sheets("Show").Range("B8").Value = Worker_ID
- ' Insert the worker name
- Worker_name = Sheets("DATA").Range("E" & i).Value
- Sheets("Show").Range("B7").Value = Worker_name
- ' Insert the worker car ID
- Car_ID = Sheets("DATA").Range("B" & i).Value
- Sheets("Show").Range("B9").Value = Car_ID
- ' Insert the worker email
- Worker_mail = Sheets("DATA").Range("F" & i).Value
- Sheets("Show").Range("C10").Value = Worker_mail
- ' Insert how much the payment is
- Full_payment = Sheets("DATA").Range("H" & i).Value
- Sheets("Show").Range("E19").Value = Full_payment
- ' Insert how much of the payment the company will cover
- Company_payment = Sheets("DATA").Range("I" & i).Value
- Sheets("Show").Range("E20").Value = Company_payment
- ' Insert how much of the payment the worker is paying
- Worker_payment = Sheets("DATA").Range("J" & i).Value
- Sheets("Show").Range("E21").Value = Worker_payment
- Name = Sheets("DATA").Range("E" & i).Value
- ' Initialize RowCounter and StartRow
- RowCounter = 63
- startRow = 7
- ' Loop through Driving Data for the current car ID
- Do While Sheets("Driving Data").Range("B" & startRow).Value = Car_ID
- ' Debugging: Check if the correct Car_ID is being matched
- Debug.Print "Car_ID: " & Car_ID & " | Driving Data Car_ID: " & Sheets("Driving Data").Range("B" & startRow).Value
- ' Copy the template row formatting
- TemplateRow.Copy
- Sheets("Show").Range("A" & RowCounter & ":I" & RowCounter).PasteSpecial Paste:=xlPasteFormats
- ' Insert data into Show sheet
- Sheets("Show").Range("A" & RowCounter).Value = Sheets("Driving Data").Range("A" & startRow).Value
- Sheets("Show").Range("B" & RowCounter).Value = Sheets("Driving Data").Range("B" & startRow).Value
- Sheets("Show").Range("C" & RowCounter).Value = Sheets("Driving Data").Range("C" & startRow).Value
- Sheets("Show").Range("D" & RowCounter).Value = Sheets("Driving Data").Range("D" & startRow).Value
- Sheets("Show").Range("E" & RowCounter).Value = Sheets("Driving Data").Range("E" & startRow).Value
- Sheets("Show").Range("F" & RowCounter).Value = Sheets("Driving Data").Range("F" & startRow).Value
- Sheets("Show").Range("G" & RowCounter).Value = Sheets("Driving Data").Range("G" & startRow).Value
- Sheets("Show").Range("H" & RowCounter).Value = Sheets("Driving Data").Range("H" & startRow).Value
- ' Add to total fee
- totalFee = totalFee + Sheets("Driving Data").Range("G" & startRow).Value
- ' Check if we need to start a new page
- If (RowCounter - 63) Mod 40 = 0 Then
- ' Repeat headers on new page
- Sheets("Show").Rows("44:62").Copy
- Sheets("Show").Rows(RowCounter + 1).PasteSpecial Paste:=xlPasteFormats
- RowCounter = RowCounter + 18
- End If
- RowCounter = RowCounter + 1
- startRow = startRow + 1
- Loop
- ' Insert totals and VAT
- vat = totalFee * 0.17
- Sheets("Show").Range("E53").Value = totalFee
- Sheets("Show").Range("E56").Value = vat
- Sheets("Show").Range("E59").Value = totalFee + vat
- ' Save the PDF for the first page (portrait)
- Sheets("Show").PageSetup.Orientation = xlPortrait
- Sheets("Show").PageSetup.PrintArea = "A1:I43"
- PdfFile = FolderPath & Name & "_1.pdf"
- Sheets("Show").ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, _
- Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
- ' Save the PDF for the second page (landscape)
- Sheets("Show").PageSetup.Orientation = xlLandscape
- Sheets("Show").PageSetup.PrintArea = "A44:I88"
- PdfFile = FolderPath & Name & "_2.pdf"
- Sheets("Show").ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, _
- Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
- Next i
- ' Clean up
- Set FSO = Nothing
- Application.CutCopyMode = False
- End Sub
- Sub CreatePDFWithLandscapeMatrix()
- Dim wsData As Worksheet
- Dim wsShow As Worksheet
- Dim lastRow As Long
- Dim currentRow As Long
- Dim carID As String
- Dim carType As String
- Dim eventDate As String
- Dim entryGate As String
- Dim entryCount As String
- Dim fee As Double
- Dim totalFee As Double
- Dim vat As Double
- Dim pageNumber As Integer
- Set wsData = ThisWorkbook.Sheets("Driving Data")
- Set wsShow = ThisWorkbook.Sheets("Show")
- ' Ensure the Show sheet is set to landscape
- wsShow.PageSetup.Orientation = xlLandscape
- ' Clear previous data
- wsShow.Range("A63:I" & wsShow.Rows.Count).ClearContents
- lastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
- currentRow = 63 ' Starting row for matrix data
- pageNumber = 1
- totalFee = 0
- ' Loop through the data in Driving Data sheet
- For i = 7 To lastRow
- carID = wsData.Cells(i, "B").Value
- carType = wsData.Cells(i, "C").Value
- eventDate = wsData.Cells(i, "D").Value
- entryGate = wsData.Cells(i, "E").Value
- entryCount = wsData.Cells(i, "F").Value
- fee = wsData.Cells(i, "G").Value
- ' Check if new page is needed
- If currentRow > 62 And (currentRow - 62) Mod 40 = 0 Then
- pageNumber = pageNumber + 1
- wsShow.HPageBreaks.Add Before:=wsShow.Cells(currentRow, 1)
- AddPageHeaders wsShow, currentRow
- currentRow = currentRow + 1
- End If
- ' Add data to Show sheet
- wsShow.Cells(currentRow, "B").Value = carID
- wsShow.Cells(currentRow, "C").Value = carType
- wsShow.Cells(currentRow, "D").Value = eventDate
- wsShow.Cells(currentRow, "E").Value = entryGate
- wsShow.Cells(currentRow, "F").Value = entryCount
- wsShow.Cells(currentRow, "G").Value = fee
- wsShow.Cells(currentRow, "H").Value = fee * 0.17 ' Assuming VAT calculation is correct
- wsShow.Cells(currentRow, "I").Value = fee + (fee * 0.17)
- totalFee = totalFee + fee
- currentRow = currentRow + 1
- Next i
- ' Add total fee, VAT, and sum to the specified cells
- wsShow.Cells(53, "E").Value = totalFee
- wsShow.Cells(56, "E").Value = totalFee * 0.17
- wsShow.Cells(60, "E").Value = totalFee + (totalFee * 0.17)
- ' Save as PDF
- Dim pdfPath As String
- pdfPath = ThisWorkbook.Path & "\Output.pdf"
- wsShow.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, _
- IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
- End Sub
- Sub AddPageHeaders(ws As Worksheet, startRow As Long)
- ' Assuming headers are located at row 62 and need to be copied to each new page
- ws.Range("A62:I62").Copy
- ws.Range("A" & startRow & ":I" & startRow).PasteSpecial Paste:=xlPasteAll
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment