Hello,
I did a search for this concept before posting it here. I'm new to VB and don't know much about the programming concepts. However, by recording the macro, i'm able to modify a VB code for creating a pivot table in an excel 2003.
----------------------------------------------------------------------------------
Purpose: I have an excel sheet with around 4000 rows of data that usually gets update once in two weeks. It also has around 15 columns. I came up with a macro to create a pivot table for this data.
----------------------------------------------------------------------------------
What I did:
Sub CreatePivot()
' Creates a PivotTable report from the table on Sheet1
' 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
' Create the PivotTable object based on the Input data on Sheet1.
Set objTable = Sheet7.PivotTableWizard
' Specify row and column fields.
Set objField = objTable.PivotFields("Region")
objField.Orientation = xlRowField
objField.Position = 1
objField.PivotItems("AP").Visible = True
objField.PivotItems("NA").Visible = True
objField.PivotItems("EU").Visible = True
objField.PivotItems("None").Visible = False
objField.PivotItems("(blank)").Visible = False
objField.EnableMultiplePageItems = True
Set objField = objTable.PivotFields("Prod")
objField.Orientation = xlRowField
objField.Position = 2
objField.PivotItems("Aeropostale").Visible = True
objField.PivotItems("American Eagle").Visible = True
objField.PivotItems("Banana Republic").Visible = True
objField.PivotItems("Victoria Secret").Visible = True
objField.PivotItems("Hugo Boss").Visible = True
objField.PivotItems("Levis").Visible = True
objField.PivotItems("(blank)").Visible = False
objField.PivotItems("Victoria Secret").Position = 1
objField.PivotItems("American Eagle").Position = 2
objField.PivotItems("Hugo Boss").Position = 3
objField.EnableMultiplePageItems = True
' Specify a data field with its summary
' function and format.
Set objField = objTable.PivotFields("Proposal")
objField.Orientation = xlDataField
objField.Function = xlCount
objField.NumberFormat = " #,##0"
' Specify a page field.
Set objField = objTable.PivotFields("Prop Status")
objField.Orientation = xlPageField
objField.PivotItems("Quoted").Visible = True
objField.PivotItems("Quoted w/ Spec").Visible = True
objField.PivotItems("Quoted w/o Spec").Visible = True
objField.PivotItems("Quoted P Only").Visible = True
objField.PivotItems("Others").Visible = False
objField.PivotItems("Eng Review").Visible = False
objField.PivotItems("Feas 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
' Preview the new PivotTable report.
ActiveSheet.PrintPreview
' Prompt the user whether to delete the PivotTable.
Application.DisplayAlerts = False
If MsgBox("Delete the PivotTable?", vbYesNo) = vbYes Then
ActiveSheet.Delete
End If
Application.DisplayAlerts = True
End Sub
--------------------------------------------------------------------------------
Issue:
It is working fine (i'm assuming - yet to do more tests) when all the pivot items in the pivot fields are part of the data. I deleted couple of rows in which the pivot item "(blank)" in the pivotfield "Purch. Reg" is not available. Usually such type of value can be part of the input data. I wonder this would be the same issue when any pivot field or other pivot item is missing in the input will result in the same error.
---------------------------------------------------------------------------------
Can some one please explain me what concepts should I learn to handle such errors. Thanks in advance.
I did a search for this concept before posting it here. I'm new to VB and don't know much about the programming concepts. However, by recording the macro, i'm able to modify a VB code for creating a pivot table in an excel 2003.
----------------------------------------------------------------------------------
Purpose: I have an excel sheet with around 4000 rows of data that usually gets update once in two weeks. It also has around 15 columns. I came up with a macro to create a pivot table for this data.
----------------------------------------------------------------------------------
What I did:
Sub CreatePivot()
' Creates a PivotTable report from the table on Sheet1
' 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
' Create the PivotTable object based on the Input data on Sheet1.
Set objTable = Sheet7.PivotTableWizard
' Specify row and column fields.
Set objField = objTable.PivotFields("Region")
objField.Orientation = xlRowField
objField.Position = 1
objField.PivotItems("AP").Visible = True
objField.PivotItems("NA").Visible = True
objField.PivotItems("EU").Visible = True
objField.PivotItems("None").Visible = False
objField.PivotItems("(blank)").Visible = False
objField.EnableMultiplePageItems = True
Set objField = objTable.PivotFields("Prod")
objField.Orientation = xlRowField
objField.Position = 2
objField.PivotItems("Aeropostale").Visible = True
objField.PivotItems("American Eagle").Visible = True
objField.PivotItems("Banana Republic").Visible = True
objField.PivotItems("Victoria Secret").Visible = True
objField.PivotItems("Hugo Boss").Visible = True
objField.PivotItems("Levis").Visible = True
objField.PivotItems("(blank)").Visible = False
objField.PivotItems("Victoria Secret").Position = 1
objField.PivotItems("American Eagle").Position = 2
objField.PivotItems("Hugo Boss").Position = 3
objField.EnableMultiplePageItems = True
' Specify a data field with its summary
' function and format.
Set objField = objTable.PivotFields("Proposal")
objField.Orientation = xlDataField
objField.Function = xlCount
objField.NumberFormat = " #,##0"
' Specify a page field.
Set objField = objTable.PivotFields("Prop Status")
objField.Orientation = xlPageField
objField.PivotItems("Quoted").Visible = True
objField.PivotItems("Quoted w/ Spec").Visible = True
objField.PivotItems("Quoted w/o Spec").Visible = True
objField.PivotItems("Quoted P Only").Visible = True
objField.PivotItems("Others").Visible = False
objField.PivotItems("Eng Review").Visible = False
objField.PivotItems("Feas 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
' Preview the new PivotTable report.
ActiveSheet.PrintPreview
' Prompt the user whether to delete the PivotTable.
Application.DisplayAlerts = False
If MsgBox("Delete the PivotTable?", vbYesNo) = vbYes Then
ActiveSheet.Delete
End If
Application.DisplayAlerts = True
End Sub
--------------------------------------------------------------------------------
Issue:
It is working fine (i'm assuming - yet to do more tests) when all the pivot items in the pivot fields are part of the data. I deleted couple of rows in which the pivot item "(blank)" in the pivotfield "Purch. Reg" is not available. Usually such type of value can be part of the input data. I wonder this would be the same issue when any pivot field or other pivot item is missing in the input will result in the same error.
---------------------------------------------------------------------------------
Can some one please explain me what concepts should I learn to handle such errors. Thanks in advance.