cgeorge4
Board Regular
- Joined
- Jul 24, 2011
- Messages
- 91
Using Excel 2007 at home / Excel 2010 at work
Window 7
The code below is a recorded code that creates a pivot table on the same sheet as the data. I added and changed some lines of code where necessary with no problem.
PROBLEM: I'm not sure where to place the first "error handling" line of code - or exactly what type of error handling code it should be.
I need a message box to alert the user that a pivot table already exists on the sheet. The exact message should be as follows:
"A Supplier Pivot Table(s) already exists. Please delete and run code again."
The message box should just have a button for "Okay". Then closes.
The user will manually clear or delete the pivot tables that need to be cleared or deleted from the sheet - after that they will run the code again.
IMPORTANT: I don't want to add a code to delete all existing pivots on the sheet because there are 3 to 4 small pivots on the sheet that should not be removed.
Thanks for your help in advance
Thanks!
Window 7
The code below is a recorded code that creates a pivot table on the same sheet as the data. I added and changed some lines of code where necessary with no problem.
PROBLEM: I'm not sure where to place the first "error handling" line of code - or exactly what type of error handling code it should be.
I need a message box to alert the user that a pivot table already exists on the sheet. The exact message should be as follows:
"A Supplier Pivot Table(s) already exists. Please delete and run code again."
The message box should just have a button for "Okay". Then closes.
The user will manually clear or delete the pivot tables that need to be cleared or deleted from the sheet - after that they will run the code again.
IMPORTANT: I don't want to add a code to delete all existing pivots on the sheet because there are 3 to 4 small pivots on the sheet that should not be removed.
Thanks for your help in advance

Code:
Sub pivot()'
Dim FinalRow As Long
Dim FinalCol As Long
Application.ScreenUpdating = False
With ActiveSheet
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
FinalCol = Cells(1, Columns.Count).End(xlToLeft).Row
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"PSRV!R1C2:R" & FinalRow & "C16", Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="PSRV!R23C20", TableName:="PivotTable9", DefaultVersion _
:=xlPivotTableVersion12
Sheets("APSRV").Select
Cells(24, 21).Select
ActiveSheet.PivotTables("PivotTable9").AddDataField ActiveSheet.PivotTables( _
"PivotTable9").PivotFields("Site Code"), "Count of Site", xlCount
With ActiveSheet.PivotTables("PivotTable9").PivotFields("Usual vs Open")
.Orientation = xlPageField
.Position = 1
End With
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveSheet.PivotTables("PivotTable9").PivotFields("Usual vs Open"). _
CurrentPage = "(All)"
With ActiveSheet.PivotTables("PivotTable9").PivotFields("Usual vs Open")
.PivotItems("Open").Visible = True
.PivotItems("Rented").Visible = False
End With
ActiveSheet.PivotTables("PivotTable9").PivotFields("Usual vs Open"). _
EnableMultiplePageItems = True
ActiveSheet.PivotTables("PivotTable9").TableStyle2 = "PivotStyleLight1"
End With
ActiveWorkbook.ShowPivotTableFieldList = False
With ActiveSheet.PivotTables("PivotTable9").PivotFields("Site")
.Orientation = xlRowField
.Position = 1
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub