PDA

View Full Version : Export pivot table to individual PDF



G10
02-12-2020, 12:29 AM
Hi All,

I need some help regarding VBA to Export pivot table to individual PDF based on each value in the filter field.
Below is the screenshot of my pivot table:
25969
I'm getting the following message when i'm running the VBA: "microsoft visual basic error 400"

VBA CODE:

Sub PDF_Indivudual_Summaries()
Dim strPath As String
Dim wksSource As Worksheet
Dim PT As PivotTable
Dim pf As PivotField
Dim pi As PivotItem

Set wksSource = Worksheets("Summary")

Set PT = wksSource.PivotTables("PivotTable1")

Set pf = PT.PivotFields("Department_Allocation")

If pf.Orientation <> xlPageField Then
MsgBox "There's no 'Department_Allocation' field in the Report Filter. Try again!", vbExclamation
Exit Sub
End If

strPath = "C:\EXPORTDIRECTORY"

If Right(strPath, 1) <> "" Then strPath = strPath & ""

ActiveWorkbook.ShowPivotTableFieldList = False

PT.PivotCache.MissingItemsLimit = xlMissingItemsNone
PT.PivotCache.Refresh

With pf
.ClearAllFilters
For Each pi In .PivotItems
.CurrentPage = pi.Name
wksSource.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPath & pi.Name & "_Code.pdf"
Next pi
.ClearAllFilters
End With

End Sub

p45cal
02-12-2020, 09:19 AM
This line looks dodgy to me:

If Right(strPath, 1) <> "" Then strPath = strPath & ""
Try changing to:

If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
or:

If Right(strPath, 1) <> Application.PathSeparator Then strPath = strPath & Application.PathSeparator
…and if you're using a Mac version, come back.

G10
02-14-2020, 03:52 AM
Thanks p45cal. problem solved :bow: