Option Explicit
Dim PDFName As String, abort As Boolean
Sub Main()
SALES_HISTORY
If Not abort Then Call PRINT_PAGE_PDF
End Sub
Sub SALES_HISTORY()
Dim ws As Worksheet
Dim Inv_Data, Hist_Data
Dim Cashier As String
Dim Invoice_Number As Long, lr As Long, i As Long, j As Long
Dim Cash_Received As Double
Dim Inv_Date As Date
Application.ScreenUpdating = False
Set ws = Sheets("Sales History")
ReDim Hist_Data(1 To 40, 1 To 8)
abort = False
Const Invoice_Reference As String = "Invoice_" '<<< Change to required Invoice reference
With Sheets("Point of Sale")
lr = .Cells(Rows.Count, "I").End(xlUp).Row
Inv_Data = .Range("H18:O" & lr)
Inv_Date = Int(.[J8]): Cashier = .[J9]: Cash_Received = .[E12]
If Cashier = "" Or Cash_Received = 0 Then
MsgBox "Cashier and/or Cash Received data missing" & vbCrLf & " Run terminated", vbCritical
abort = True
Exit Sub
End If
Invoice_Number = .[E18] + 1 ' Add 1 to "Last Invoice Number"
.[L11] = Invoice_Number
For i = 1 To UBound(Inv_Data) ' Update "Sales History" data
If Inv_Data(i, 1) = "" Then Exit For
Hist_Data(i, 1) = Invoice_Number: Hist_Data(i, 2) = Inv_Date: Hist_Data(i, 3) = Cashier:
Hist_Data(i, 4) = Inv_Data(i, 1): Hist_Data(i, 5) = Inv_Data(i, 2): Hist_Data(i, 6) = Inv_Data(i, 3)
Hist_Data(i, 7) = Inv_Data(i, 5): Hist_Data(i, 8) = Inv_Data(i, 8)
Next i
.[E18] = Invoice_Number ' re-set "Last Invoice Number"
End With
With ws ' Update "Sales History"
lr = .Cells(Rows.Count, "A").End(xlUp).Row
.Cells(lr + 1, "A").Resize(UBound(Inv_Data), 8) = Hist_Data
.Columns(6).Resize(, 3).NumberFormat = "#,0.00"
End With
PDFName = Invoice_Reference & Format(Invoice_Number, "0000") ' Set PDF name
Application.ScreenUpdating = True
End Sub
Sub PRINT_PAGE_PDF()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim PDFPath As String
PDFPath = "C:\Users\johnt\OneDrive\Desktop\pdf\" & PDFName & ".pdf" ' Set the desired PDF file path
With ActiveSheet.PageSetup
.PrintArea = "$G$1:$L$56"
End With
' Export the active sheet as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Code updated to cater for error condtion is "Point of Sale" i.e Cashier and/or Cash Received" data missing.
Re the rror: Impossible to say without knowing what data you entered.
Did you change the path:
PDFPath = "C:\Users\johnt\OneDrive\Desktop\pdf\" & PDFName & ".pdf" ' Set the desired PDF file path
Bookmarks