Hi All. I tried creating a macro that generates a pivot table and sticks it in a new tab when a button is pressed. It sort of worked but had issues. So here is what I am trying to accomplish:
1. Push a button on a "GUI" tab to create the pivot table (ie, the button is on another tab than the data and eventual pivot table)
2. Old pivot table is deleted and new is created in the "Pivot" tab based on the data in the "Data" tab.
3. Row fields are "Preferred Supplier", "M-spec", and "PO or Plan".
4. Column Field is "Due Date".
5. Data field is "OpenQty".
6. Date must be grouped by month
Here is the code I have that is creating the table in a new (random) tab. Problem is, I can't figure out how to specify which tab to create the table on, and I can't figure out how to group by month.
Sub Pivot2()
Dim WSD As Worksheet
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim PRange As Range
Dim FinalRow As Long
Set WSD = Worksheets("PO")
Dim WSR As Worksheet
' Delete any prior pivot tables
For Each PT In WSD.PivotTables
PT.TableRange2.Clear
Next PT
' Define input area and set up a Pivot Cache
FinalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row
FinalCol = WSD.Cells(1, Application.Columns.Count). _
End(xlToLeft).Column
Set PRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:= _
xlDatabase, SourceData:=PRange.Address)
' Create the Pivot Table from the Pivot Cache
Set PT = PTCache.CreatePivotTable("", TableName:="PivotTable1")
' Turn off updating while building the table
PT.ManualUpdate = True
' Set up the row fields
PT.AddFields RowFields:=Array("M-Spec", "Prefered Supplier", "PO or Plan"), ColumnFields:="DueDate"
' Set up the data fields
With PT.PivotFields("OpenQty")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
'.NumberFormat = "#,##0"
End With
' Ensure that you get zeroes instead of blanks in the data area
PT.NullString = "0"
' Calc the pivot table to allow the date label to be drawn
PT.ManualUpdate = False
PT.ManualUpdate = True
' Calc the pivot table
PT.ManualUpdate = False
PT.ManualUpdate = True
End Sub
Bookmarks