Link to home
Start Free TrialLog in
Avatar of Edward Pamias
Edward PamiasFlag for United States of America

asked on

Excel Macro - to create Pivot and export to PDF

I need a MACRO to create a pivot Table and export that to a PDF or show as image in another tab then export it.

Worksheet name = 8 Week INC Trend
Report Filter I need , "8 Week Trend", "Task Type"
Column Labels I need "Week Ending"
Row Labels I need "Assigned to"
Values I need "number"

NOTE I do have a name range for my data TAB called "RANGE"
Avatar of Norie
Norie

Can you upload a sample workbook so we can see how the data is structured on the sheet '8 Week INC Trend'?

PS I'm assuming that's the sheet where the source data for the pivot table is located.
Avatar of Edward Pamias

ASKER

I cannot upload the data from the sheet. :(  When I make a pivot, I click insert, pivot table,  hit F3 to select named range "RANGE" then let it open a new tab to create pivot table on. I will try to send a screen shot of the columns.
Sorry it is so small but that's the best I can do with snipping tool
Edward

Have you tried recording a macro when you create a pivot table from the source data?

That would at least give you a start on things.
Edward,

Post a snip of manually Created Pivot.
What do you need of Values of Numbers?
Count or Sum?
User generated image

I need the told numbers  - see snipit.
Try below which is a count of Numbers:
Sub CreatePivotAndPDF()
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
With Application
    .ScreenUpdating = False
    .DisplayStatusBar = True
    .StatusBar = "!!! Please Be Patient...Updating Records !!!"
    .EnableEvents = False
    .Calculation = xlManual
End With
'Delete Preivous Pivot Table Worksheet & Insert a New Blank Worksheet With Same Name
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("8 Week INC Trend")
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="FilteredPivotTable")
'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), TableName:="FilteredPivotTable")
'Insert Page Fields
With ActiveSheet.PivotTables("FilteredPivotTable").PivotFields("8 Week Trend")
 .Orientation = xlPageField
 .Position = 1
End With
With ActiveSheet.PivotTables("FilteredPivotTable").PivotFields("Task Type")
    .Orientation = xlPageField
    .Position = 1
End With
'Insert Row Fields
With ActiveSheet.PivotTables("FilteredPivotTable").PivotFields("Assigned To")
    .Orientation = xlRowField
    .Position = 1
End With
'Insert Column Fields
With ActiveSheet.PivotTables("FilteredPivotTable").PivotFields("Week Ending")
    .Orientation = xlColumnField
    .Position = 1
End With
'Insert Data Field
With ActiveSheet.PivotTables("FilteredPivotTable").PivotFields("Number")
    .Orientation = xlDataField
    .Position = 1
    .Function = xlCount
    .NumberFormat = "#,##0"
    .Name = "Numbers"
End With
'Format Pivot Table
ActiveSheet.PivotTables("FilteredPivotTable").CompactLayoutRowHeader = "Assigned Person"
ActiveSheet.PivotTables("FilteredPivotTable").CompactLayoutColumnHeader = "Weeks"
ActiveSheet.PivotTables("FilteredPivotTable").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("FilteredPivotTable").TableStyle2 = "PivotStyleMedium15"
ActiveSheet.Columns.AutoFit

With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=ThisWorkbook.Path & "\" & .Name, OpenAfterPublish:=True
End With

With Application
    .ScreenUpdating = True
    .DisplayStatusBar = True
    .StatusBar = False
    .EnableEvents = True
    .Calculation = xlAutomatic
End With
End Sub

Open in new window

What about the selection of Page Fields? Should it be Valid & Incident?
When I create the pivot and add 8 week trend. The selections are blank field or Valid. I select valid so I can get just 8 weeks of totals. Then I drop weekend ending in the columns. I put assigned to, in Row Labels, and Numbers in the values section.
In this case, I would advice, you should have separate VBAs, one for creating Pivot, make the selection then run another vba for creating PDF.
ASKER CERTIFIED SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The Data is on sheet Page 1. I have a feeling I need to change something. :(
Initially you said
Worksheet name = 8 Week INC Trend
I fixed it!
I just changed the sheet name and it works!! Thank you!
I got more questions coming up. Stay tuned!
You're Always Welcome Edward! Pleasure is all mine!
Thanks again Shum and Norie!
Shums, I modified the last Macro to give me tasks. I am slowly getting it.