Saving Pivot table to PDF

saloelrn1988

New Member
Joined
Apr 14, 2020
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hi I need Help!

i am able to save my Pivot table in PDF however upon saving it saves other pages with blanks. i wanted to save it up until the cell containing data(my last cell may vary because I'm using different data per week).

See below coding that i used:

Sub Save_PDF()
'
' Save_PDF Macro
' Save pivot table as PDF
Dim saveLocation As String
saveLocation = "C:\Users\elon\Desktop\" & Sheets("email data").Range("B3")

Sheets("pivot table").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveLocation _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
Worksheets("raw data").Select
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You can use the ExportAsFixedFormat method of the pivot table range, instead of the active sheet...

VBA Code:
Option Explicit

Sub Save_PDF()

    Dim saveLocation As String
    saveLocation = "C:\Users\elon\Desktop\" & Sheets("email data").Range("B3").Value
    
    Dim targetPivotTable As pivotTable
    Set targetPivotTable = Sheets("pivot table").PivotTables("PivotTable1") 'change the pivot table name accordingly
    
    Dim targetExportRange As Range
    Set targetExportRange = targetPivotTable.TableRange2 'range includes the page field
    
    targetExportRange.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=saveLocation, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
        
End Sub

Hope this helps!
 
Upvote 0
You can use the ExportAsFixedFormat method of the pivot table range, instead of the active sheet...

VBA Code:
Option Explicit

Sub Save_PDF()

    Dim saveLocation As String
    saveLocation = "C:\Users\elon\Desktop\" & Sheets("email data").Range("B3").Value
   
    Dim targetPivotTable As pivotTable
    Set targetPivotTable = Sheets("pivot table").PivotTables("PivotTable1") 'change the pivot table name accordingly
   
    Dim targetExportRange As Range
    Set targetExportRange = targetPivotTable.TableRange2 'range includes the page field
   
    targetExportRange.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=saveLocation, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=True
       
End Sub

Hope this helps!

hi, I'm not that knowledgeable when it come to coding.

how would i select a pivot name?

and what do you mean in "range includes the page field" should i make any modification on that as well?
 
Upvote 0
how would i select a pivot name?

Select a cell within your pivot table, and check the ribbon for the name of the pivot table...

VBA Code:
Ribbon >> PivotTable Analyze tab >> PivotTable group >> PivotTable Name

Alternatively, you can refer to your pivot table by index. For example, since the worksheet contains only one pivot table (I've assumed so from your original post) , you can refer to it using index number 1...

VBA Code:
Set targetPivotTable = Sheets("pivot table").PivotTables(1)

and what do you mean in "range includes the page field" should i make any modification on that as well?

**Edited**

It means that the cells containing the page field or page fields will be included in the exported PDF. If for some reason you don't want to include them, you would use the TableRange1 object instead of TableRange2...

VBA Code:
Set targetExportRange = targetPivotTable.TableRange1
 
Last edited:
Upvote 0
Select a cell within your pivot table, and check the ribbon for the name of the pivot table...

VBA Code:
Ribbon >> PivotTable Analyze tab >> PivotTable group >> PivotTable Name

Alternatively, you can refer to your pivot table by index. For example, since the worksheet contains only one pivot table (I've assumed so from your original post) , you can refer to it using index number 1...

VBA Code:
Set targetPivotTable = Sheets("pivot table").PivotTables(1)



It means that the cells containing the page field or page fields will be included in the exported PDF. If for some reason you don't want to include them, you would use the TableRange object instead of TableRange2...

VBA Code:
Set targetExportRange = targetPivotTable.TableRange

i already found the table name however im having a run time error once i execute my macro a
1588962680098.png
 
Upvote 0
Sorry, that should have been TableRange1...

VBA Code:
Set targetExportRange = targetPivotTable.TableRange1
 
Upvote 0
You didn't say which error you're getting, but it's probably a "Run-time error 1004: Document not saved ... ".

Try running the macro again. This time, though, when the error occurs and you click on Debug, move your cursor over the variable saveLocation.

Has it been assigned a valid path and filename?

And, if so, do you have permissions to access the folder?
 
Upvote 0
below is my runtime error, also the path that i used in the beginning is the same as the path that I'm using now.

Actually the path is on my desktop

1588965405442.png
 
Upvote 0
For testing purposes only, try replacing...

VBA Code:
Filename:=saveLocation, _

with

VBA Code:
Filename:="C:\Users\" & Environ("username") & "\Desktop\sample.pdf", _

Do you still get an error?
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,664
Members
449,114
Latest member
aides

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top