J
Joel Mills
Below is a sample of my code. During testing I determined that , there are
two way's the user can create an error; by not retrieving the data for the
data base and by running the Macro a second time. The macro is designed to
create a pivot table on the fly (once). What I need help with is revising
the code to only show the message box if there is an error and then exiting
the sub once they click the okay button on the message box. Now I get the
error message even if no error occurs while running the Sub. After I click
okay it finished the Sub Procedure.
Joel Mills
Sub CreatePivotTable()
Dim PTCache As PivotCache
Dim PT As PivotTable
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase,
SourceData:= _
"Database")
On Error Resume Next
MsgBox "Did you Copy the Export and Run Cleanup?" _
& vbCrLf & "If not then run them before running this Macro"
_
& vbCrLf & " If you have then you must Delete" _
& vbCrLf & "the (Pivot) Worksheet before running this Macro"
Set PT = PTCache.CreatePivotTable(TableDestination:="",
TableName:="PercentTable")
With PT
.PivotFields("Week Ending").Orientation = xlColumnField
.PivotFields("Target Early % Comp.").Orientation = xlDataField
.PivotFields("Target Late % Comp.").Orientation = xlDataField
.PivotFields("Target Planned % Comp.").Orientation = xlDataField
.ColumnGrand = False
.RowGrand = False
End With
ActiveSheet.Name = "Pivot"
End Sub
two way's the user can create an error; by not retrieving the data for the
data base and by running the Macro a second time. The macro is designed to
create a pivot table on the fly (once). What I need help with is revising
the code to only show the message box if there is an error and then exiting
the sub once they click the okay button on the message box. Now I get the
error message even if no error occurs while running the Sub. After I click
okay it finished the Sub Procedure.
Joel Mills
Sub CreatePivotTable()
Dim PTCache As PivotCache
Dim PT As PivotTable
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase,
SourceData:= _
"Database")
On Error Resume Next
MsgBox "Did you Copy the Export and Run Cleanup?" _
& vbCrLf & "If not then run them before running this Macro"
_
& vbCrLf & " If you have then you must Delete" _
& vbCrLf & "the (Pivot) Worksheet before running this Macro"
Set PT = PTCache.CreatePivotTable(TableDestination:="",
TableName:="PercentTable")
With PT
.PivotFields("Week Ending").Orientation = xlColumnField
.PivotFields("Target Early % Comp.").Orientation = xlDataField
.PivotFields("Target Late % Comp.").Orientation = xlDataField
.PivotFields("Target Planned % Comp.").Orientation = xlDataField
.ColumnGrand = False
.RowGrand = False
End With
ActiveSheet.Name = "Pivot"
End Sub