Hello,
---------------------------------------------------------------------------------------
Purpose: Using a macro, I would like to create a pivot table from input source; but in an existing sheet
---------------------------------------------------------------------------------------
Couple of Requirements:
(1) I will always have a sheet created by name "YourPivotTables" in the same workbook that serves as a destination to this newly created pivot table
(2) I would like to erase the contents in this sheet before new pivot table gets created
(3) The input data gets changed once in 2 weeks - different rows, but same columns
---------------------------------------------------------------------------------------
What I did:
Sub CreatePivot()
' Creates a PivotTable report by using the PivotTableWizard method
' with the PivotFields method to specify the fields in the PivotTable.
Dim objTable As PivotTable, ObjField As PivotField
' Select the sheet and first cell of the table that contains the data.
ActiveWorkbook.Sheets("Input Data".Select Range("B1".Select
Worksheets("YourPivotTables".Select
Cells.Delete
On Error GoTo errhandler
' Create the PivotTable object based on the Input data.
Set objTable = Sheet6.PivotTableWizard(xlDatabase, Range("B1", _
"YourPivotTables", "Proposal Counts"
' Specify row and column fields.
Set ObjField = objTable.PivotFields("Product"
ObjField.Orientation = xlRowField
ObjField.Position = 1
ObjField.PivotItems("AE".Visible = True
ObjField.PivotItems("Eng".Visible = True
ObjField.PivotItems("EngPI".Visible = True
ObjField.PivotItems("Plant".Visible = True
ObjField.PivotItems("PlantPI".Visible = True
ObjField.PivotItems("PGS".Visible = True
ObjField.PivotItems("(blank)".Visible = False
ObjField.PivotItems("EngPI".Position = 1
ObjField.PivotItems("PlantPI".Position = 2
ObjField.PivotItems("PGS".Position = 3
ObjField.EnableMultiplePageItems = True
' Specify a data field with its summary
' function and format.
Set ObjField = objTable.PivotFields("New Unit Value"
ObjField.Orientation = xlDataField
ObjField.Function = xlAverage
ObjField.NumberFormat = " #,##0"
' Specify a page field.
Set ObjField = objTable.PivotFields("Proposal Status"
ObjField.Orientation = xlPageField
ObjField.PivotItems("Quoted-Budget".Visible = True
ObjField.PivotItems("Quoted-Firm w/ Spec".Visible = True
ObjField.PivotItems("Quoted-Firm w/o Spec".Visible = True
ObjField.PivotItems("Quoted-Perf Only".Visible = True
ObjField.PivotItems("Accessory Quote".Visible = False
ObjField.PivotItems("Engineering Review".Visible = False
ObjField.PivotItems("Feasiblity Review".Visible = False
ObjField.PivotItems("No Bid".Visible = False
ObjField.PivotItems("On Hold".Visible = False
ObjField.PivotItems("Remove Eng Review".Visible = False
ObjField.PivotItems("(blank)".Visible = False
ObjField.PivotItems("Received".Visible = True
ObjField.EnableMultiplePageItems = True
Set ObjField = objTable.PivotFields("New Unit Value"
ObjField.Orientation = xlPageField
ObjField.PivotItems("(blank)".Visible = False
ObjField.PivotItems("$".Visible = False
ObjField.EnableMultiplePageItems = True
errhandler:
If Err.Number <> 0 Then
Select Case Err.Description
Case "Unable to get the PivotItems property of the PivotField class": Resume Next
Case Else: MsgBox "Something went wrong: Error#" & Err.Number & vbCrLf & _ Err.Description, vbCritical, "Error", Err.HelpFile, Err.HelpContext
End Select
End If
Application.DisplayAlerts = True
End Sub
---------------------------------------------------------------------------------------
Issues:
Error mesg: Method'PivotTableWizard' of object '_Worksheet' failed
---------------------------------------------------------------------------------------
Can some one please help me where my code is having an issue? Thanks in advance!
---------------------------------------------------------------------------------------
Purpose: Using a macro, I would like to create a pivot table from input source; but in an existing sheet
---------------------------------------------------------------------------------------
Couple of Requirements:
(1) I will always have a sheet created by name "YourPivotTables" in the same workbook that serves as a destination to this newly created pivot table
(2) I would like to erase the contents in this sheet before new pivot table gets created
(3) The input data gets changed once in 2 weeks - different rows, but same columns
---------------------------------------------------------------------------------------
What I did:
Sub CreatePivot()
' Creates a PivotTable report by using the PivotTableWizard method
' with the PivotFields method to specify the fields in the PivotTable.
Dim objTable As PivotTable, ObjField As PivotField
' Select the sheet and first cell of the table that contains the data.
ActiveWorkbook.Sheets("Input Data".Select Range("B1".Select
Worksheets("YourPivotTables".Select
Cells.Delete
On Error GoTo errhandler
' Create the PivotTable object based on the Input data.
Set objTable = Sheet6.PivotTableWizard(xlDatabase, Range("B1", _
"YourPivotTables", "Proposal Counts"
' Specify row and column fields.
Set ObjField = objTable.PivotFields("Product"
ObjField.Orientation = xlRowField
ObjField.Position = 1
ObjField.PivotItems("AE".Visible = True
ObjField.PivotItems("Eng".Visible = True
ObjField.PivotItems("EngPI".Visible = True
ObjField.PivotItems("Plant".Visible = True
ObjField.PivotItems("PlantPI".Visible = True
ObjField.PivotItems("PGS".Visible = True
ObjField.PivotItems("(blank)".Visible = False
ObjField.PivotItems("EngPI".Position = 1
ObjField.PivotItems("PlantPI".Position = 2
ObjField.PivotItems("PGS".Position = 3
ObjField.EnableMultiplePageItems = True
' Specify a data field with its summary
' function and format.
Set ObjField = objTable.PivotFields("New Unit Value"
ObjField.Orientation = xlDataField
ObjField.Function = xlAverage
ObjField.NumberFormat = " #,##0"
' Specify a page field.
Set ObjField = objTable.PivotFields("Proposal Status"
ObjField.Orientation = xlPageField
ObjField.PivotItems("Quoted-Budget".Visible = True
ObjField.PivotItems("Quoted-Firm w/ Spec".Visible = True
ObjField.PivotItems("Quoted-Firm w/o Spec".Visible = True
ObjField.PivotItems("Quoted-Perf Only".Visible = True
ObjField.PivotItems("Accessory Quote".Visible = False
ObjField.PivotItems("Engineering Review".Visible = False
ObjField.PivotItems("Feasiblity Review".Visible = False
ObjField.PivotItems("No Bid".Visible = False
ObjField.PivotItems("On Hold".Visible = False
ObjField.PivotItems("Remove Eng Review".Visible = False
ObjField.PivotItems("(blank)".Visible = False
ObjField.PivotItems("Received".Visible = True
ObjField.EnableMultiplePageItems = True
Set ObjField = objTable.PivotFields("New Unit Value"
ObjField.Orientation = xlPageField
ObjField.PivotItems("(blank)".Visible = False
ObjField.PivotItems("$".Visible = False
ObjField.EnableMultiplePageItems = True
errhandler:
If Err.Number <> 0 Then
Select Case Err.Description
Case "Unable to get the PivotItems property of the PivotField class": Resume Next
Case Else: MsgBox "Something went wrong: Error#" & Err.Number & vbCrLf & _ Err.Description, vbCritical, "Error", Err.HelpFile, Err.HelpContext
End Select
End If
Application.DisplayAlerts = True
End Sub
---------------------------------------------------------------------------------------
Issues:
Error mesg: Method'PivotTableWizard' of object '_Worksheet' failed
---------------------------------------------------------------------------------------
Can some one please help me where my code is having an issue? Thanks in advance!