How to Generate a Report in PDF Format Using Excel VBA – 3 Methods

 

The dataset depicts a sales report. It contains the name, city, and sales amount of different people.

3 Effective Ways to Generate a Report in PDF Format Using Excel VBA


Method 1 – Using Excel VBA to Add a Command Button to Generate a Report in PDF Format

STEPS:

Using Excel VBA to Add Command Button to Generate Report in PDF Format

  • Enter Macro_1 in Macro name.
  • Click Create.

Using Excel VBA to Add Command Button to Generate Report in PDF Format

  • In the blank code window, enter the following code:
Sub Macro_1()
ChDir "C:\Users\User\Desktop\ExcelDemy"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\User\Desktop\ExcelDemy\Generate Report in PDF.pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
  • Click Save or press Ctrl + S to save the code. Close the window.

The highlighted line in the code indicates the location to save the PDF.

Using Excel VBA to Add Command Button to Generate Report in PDF Format

  • Go to the Developer tab.
  • Select Button in the Insert tab.

Using Excel VBA to Add Command Button to Generate Report in PDF Format

  • Draw a button. Right-click it.
  • Select Edit Text.

Using Excel VBA to Add Command Button to Generate Report in PDF Format

  • Rename the button: ‘Generate Report to PDF’.
  • Right-click the button.
  • Select Assign Macro.

  • Select Macro-1 and click OK.

  • Click the button ‘Generate Report in PDF’.

  • An excel report in PDF format is created.
  • The PDF report is stored in the selected folder.

Read More: How to Generate PDF Reports from Excel Data


Method 2 – Create a Report in PDF Format with a Specific File Name Using Excel VBA 

STEPS:

  • Go to the Developer tab and select Visual Basic.

Create Report Using Excel VBA in PDF Format with Specific File Name

  • A new VBA project window will open.
  • Right-click Sheet2 (Save with Name).
  • Select Insert > Module.

Create Report Using Excel VBA in PDF Format with Specific File Name

  • A blank VBA code window is displayed.
  • Enter the following code:
Sub Report_Name()
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="Report PDF", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
From:=1, _
To:=5, _
OpenAfterPublish:=True
End Sub
  • Press the F5 key or click Run to run the code.

The highlighted line in the code indicates the new name of the PDF format.

  • The excel sheet opens in PDF format.

Read More: How to Generate Reports in Excel Using Macros


Method 3 – Generating a Report in PDF Format from Multiple Sheets with Excel VBA

Create a report in another sheet using these two sheets:

Report Generating in PDF Format from Multiple Sheets with Excel VBA

 

STEPS:

  • Go to another sheet: Report.
  • Right-click the sheet.
  • Select View Code.

Report Generating in PDF Format from Multiple Sheets with Excel VBA

  • In the new blank VBA code window, enter the following code:
Private Sub Workbook_Open()
Dim nextblankrow As Long
Dim lastrow As Long
Sheets("Report").Select
Sheets("Report").Cells.ClearContents
Range("A1").Value = "Name"
Range("B1").Value = "City"
Range("C1").Value = "Sales Amount"
For i = 1 To Sheets.Count - 1
lastrow = Sheets(i).Range("A" & Rows.Count).End(xlUp).Row
Sheets(i).Range("A2:C6").Copy
Sheets(i).Range(Cells(2, 1).Address, Cells(lastrow, 4).Address).Copy
nextblankrow = Sheets("Report").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
Sheets("Report").Cells(nextblankrow, 1).PasteSpecial Paste:=xlPasteValues
Next
Cells(1, 5).Select
Application.CutCopyMode = False
Sheets("Report").Range(Cells(1, 1), Cells(nextblankrow, 4)).Columns.AutoFit
ChDir "E:\Exceldemy"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"E:\Exceldemy\Generate Reeport.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End Sub
  • Press F5 key or click Run.

Report Generating in PDF Format from Multiple Sheets with Excel VBA

  •  Data from the two sheets is merged into the Report sheet.

  • Browse the folder specified in the code and find the PDF file.

Read More: How to Automate Excel Reports Using Macros


Download Practice Workbook

Download the practice workbook.


Related Articles


<< Go Back to Report in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Mukesh Dipto
Mukesh Dipto

Mukesh Dipto is an excellent marine engineer who loves working with Excel and diving into VBA programming. For him, programming is like a superhero tool that saves time when dealing with data, files, and the internet. His skills go beyond the basics, including Rhino3D, Maxsurf C++, AutoCAD, HTML, CSS, JavaScript, and WordPress. He got his B.Sc in Naval Architecture & Marine Engineering from BUET, and now he's switched gears, working as a content developer. In this role, he... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo