Need Help with VBA Code for automated report

electriclunch

New Member
Joined
May 24, 2015
Messages
3
Hi All,

I run a weekly report for my department that pulls from a pivot table. The macro I used filters on the name for each distributor and saves a pdf file of the data for each one in a separate folder. An issue I am encountering is that it will save a PDF for a distributor even if there is no data returned from the pivot. How can I stop my macro from creating a PDF when there is no data tied to a distributor?


Below is my current code:

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

Set wksSource = Worksheets("PivotSummaryTable")

Set PT = wksSource.PivotTables("PivotTable2")

Set pf = PT.PivotFields("Agent Name")

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

strPath = "D:\Report\Distributors"

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 & ".pdf"
Next pi
.ClearAllFilters
End With

End Sub



Any help is greatly appreciated!!!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,215,277
Messages
6,124,010
Members
449,139
Latest member
sramesh1024

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