Search code examples
excelvbapdfpdf-generation

Convert Invoice Page Excel to PDF Using VBA


I have invoices on two different sheets in a workbook, one for distributors Invoice and one for online sales, I have to convert them to PDF when need and save them in a folder. I have a code but it works on one sheet.

Sub SaveAsPDF()
Dim filename As String
filename = "C:\Users\Administrator\Desktop\INVOICE OUT RESTORE\INVOICE SERIAL WISE\" & Range("L8").Value
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
filename, Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub

Both sheets have their own buttons for running macros.

I need such a code that whenever I need I can use the macro button of the corresponding sheet and get the PDF.


Solution

  • Your code is absolutely correct if it's run from the button on the worksheet, it should print out this sheet. This is guaranteed by using the ActiveSheet object.

    Just check it once again:

    1. The macro should reside in a module not in sheet's codepage.
    2. Every sheet has a button to directly run this macro.

    Check pdfs against sheets. If the macro isn't run directly, check if ActiveSheet is not changed by the early code. enter image description here enter image description here enter image description here