Create an Invoice and Save it in the PDF Format with Excel VBA (Quick View)
Private Sub Create_invoice_save_pdf()
'for invoice number
Sheet2.Range("C5").Value = Sheet2.Range("C5").Value + 1
'for pdf file save
Sheet1.Range("B2:E20").ExportAsFixedFormat xlTypePDF, Filename:=_"C:\Invoice\" & Sheet.Range("C5").Value, openafterpublish:=True
'for clear invoice
Sheet2.Range("B12:E15").ClearContents
Sheet2.Range("E17:E19").ClearContents
End Sub
How to Create an Invoice and Save it in the PDF Format with Excel VBA: 2 Easy Steps
We have a dataset that contains information about the invoice of the Star Restaurant. We will save it into a PDF format.
Step 1 – Use the SUM Function to Create an Invoice
- Select cell E12.
- Insert the following formula:
=C12*D12
- Hit Enter.
- AutoFill the SUM function to the rest of the cells in column E.
- Select cell E17 and use the following SUM function in that cell:
=SUM(E12:15)
- Hit Enter.
- We will give a 5% discount to the customer. Select cell E19 and insert the following formula:
=E17*95%
- Hit Enter.
Read More: Excel Macro to Save as PDF with Filename from Cell Value
Step 2 – Save the File as a PDF
- Go to Developer and select Visual Basic.
- A window named Microsoft Visual Basic for Applications – Invoice will appear.
- Select Insert and choose Module.
- In the module, insert this VBA code:
Private Sub Create_invoice_save_pdf()
'for invoice number
Sheet2.Range("C5").Value = Sheet2.Range("C5").Value + 1
'for pdf file save
Sheet1.Range("B2:E20").ExportAsFixedFormat xlTypePDF, Filename:=_"C:\Invoice\" & Sheet.Range("C5").Value, openafterpublish:=True
'for clear invoice
Sheet2.Range("B12:E15").ClearContents
Sheet2.Range("E17:E19").ClearContents
End Sub
- Go to Run and press Run Sub/UserForm or hit F5.
- Here’s the resulting file.
Read More: Excel Macro: Save as PDF with Date in Filename
Things to Remember
If the Developer tab is not visible in your ribbon, you need to make it visible. To do that, go to File → Option → Customize Ribbon
Download the Practice Workbook
Related Articles
- Excel VBA: Choose Location and Save as PDF
- Excel VBA Macro to Save PDF in Specific Folder
- Excel Macro to Save as PDF