VBA to loop through and save Individual PDFs from a pivot table

Saintshay

New Member
Joined
Jul 28, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi

I really hope someone can guide me please

I have a pivot table. There are over 700 individual records in my table.

When I filter on the records in my first row one by one the A9 will always state the individual record

My code below will save the records as a PDF however I have to filter and run my macro for each one.

Can someone please show me how to make it run for all records

Thanks in Advance

Sub Save_PDF()

Dim saveLocation As String
saveLocation = "C:\Users\SAINYSHAY\Desktop\PRINTS\PDF\" & Sheets("PRINTS").Range("A9").Value

Dim targetPivotTable As PivotTable
Set targetPivotTable = Sheets("PRINTS").PivotTables("PivotTable1")

Dim targetExportRange As Range
Set targetExportRange = targetPivotTable.TableRange2
targetExportRange.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=saveLocation, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True

End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi and welcome to MrExcel board!

Change "NAME" in this line to the name of the field to filter. Set targetField = targetPivotTable.PivotFields("NAME")

Try this:
VBA Code:
Sub Save_PDF()
  Dim saveLocation As String, itm As Variant
  Dim targetPivotTable As PivotTable
  Dim targetField As PivotField
 
  Set targetPivotTable = Sheets("PRINTS").PivotTables("PivotTable1")
  Set targetField = targetPivotTable.PivotFields("NAME")              '<---- Field name to filter
 
  For Each itm In targetField.PivotItems
    targetField.CurrentPage = Trim(itm)
    saveLocation = "C:\Users\SAINYSHAY\Desktop\PRINTS\PDF\" & Sheets("PRINTS").Range("A9").Value
    targetPivotTable.TableRange2.ExportAsFixedFormat xlTypePDF, saveLocation, 0, True, False, OpenAfterPublish:=False
  Next
  targetField.ClearAllFilters
End Sub

Note Code Tag:
In future please use code tags when posting code.
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.
 
Last edited:
Upvote 0
Hi and welcome to MrExcel board!

Change "NAME" in this line to the name of the field to filter. Set targetField = targetPivotTable.PivotFields("NAME")

Try this:
VBA Code:
Sub Save_PDF()
  Dim saveLocation As String, itm As Variant
  Dim targetPivotTable As PivotTable
  Dim targetField As PivotField
 
  Set targetPivotTable = Sheets("PRINTS").PivotTables("PivotTable1")
  Set targetField = targetPivotTable.PivotFields("NAME")              '<---- Field name to filter
 
  For Each itm In targetField.PivotItems
    targetField.CurrentPage = Trim(itm)
    saveLocation = "C:\Users\SAINYSHAY\Desktop\PRINTS\PDF\" & Sheets("PRINTS").Range("A9").Value
    targetPivotTable.TableRange2.ExportAsFixedFormat xlTypePDF, saveLocation, 0, True, False, OpenAfterPublish:=False
  Next
  targetField.ClearAllFilters
End Sub

Note Code Tag:
In future please use code tags when posting code.
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.
Hi DanteAmor


Thank you for this

Its debugging at

targetField.CurrentPage = Trim(itm)

When I highlight over it its states

Targetfield.current ...=<unable to get the current page property of the pivot
 
Upvote 0
You could activate the macro recorder, perform a filtering process, go back to stop the recorder. The generated code, you copy and paste it here.
Or you can share your workbook to check how your pivot table is doing.

You could upload a copy of your file to a free site such www.dropbox.com or google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0

Forum statistics

Threads
1,215,695
Messages
6,126,261
Members
449,307
Latest member
Andile

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