Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pivot table macro error

Status
Not open for further replies.

Navvy

Technical User
Apr 12, 2002
64
US
Hello,

I have recorded three macros which create a pivot table one after the other on the sheet "Results". For some reason, sometimes it works and other times it crashes. I think it has something to do with the pivot table name - but I changed these. Here's my code:

Sub CreateGrid()

Application.CommandBars("PivotTable").Visible = False
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveWorkbook.ShowPivotTableFieldList = False



ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"Data!R1C1:R116C13").CreatePivotTable TableDestination:= _
"[Inter.xls]Results!R2C1", TableName:="DateTable"
', DefaultVersion:= _
'xlPivotTableVersion10
ActiveSheet.PivotTables("DateTable").AddFields RowFields:="Party", _
ColumnFields:="Type"
ActiveSheet.PivotTables("DateTable").PivotFields("Type").Orientation = _
xlDataField
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveWorkbook.ShowPivotTableFieldList = False
Application.CommandBars("PivotTable").Visible = False



End Sub
Sub CreateMth()

Application.CommandBars("PivotTable").Visible = False
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveWorkbook.ShowPivotTableFieldList = False



Sheets("Month").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"Month!R1C1:R116C13").CreatePivotTable TableDestination:= _
"[Inter.xls]Results!R35C1", TableName:="MonthTable", DefaultVersion:= _
xlPivotTableVersion10
ActiveSheet.PivotTables("MonthTable").AddFields RowFields:="Party", _
ColumnFields:="Type"
ActiveSheet.PivotTables("MonthTable").PivotFields("Type").Orientation = _
xlDataField
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveWorkbook.ShowPivotTableFieldList = False
Application.CommandBars("PivotTable").Visible = False



End Sub
Sub CreateWk()

Application.CommandBars("PivotTable").Visible = False
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveWorkbook.ShowPivotTableFieldList = False

Sheets("Week").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"Week!R1C1:R116C13").CreatePivotTable TableDestination:= _
"[Inter.xls]Results!R75C1", TableName:="WeekTable", DefaultVersion:= _
xlPivotTableVersion10
ActiveSheet.PivotTables("WeekTable").AddFields RowFields:="Party", _
ColumnFields:="Type"
ActiveSheet.PivotTables("WeekTable").PivotFields("Type").Orientation = _
xlDataField
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveWorkbook.ShowPivotTableFieldList = False
Application.CommandBars("PivotTable").Visible = False


End Sub



 
and where, pray, does it crash ??
and what is the error message ??

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Hi Geoff,

The error message is:
Unable to get the PivotTables property of the Worksheet class.

On debug:
ActiveSheet.PivotTables("MonthTable").AddFields RowFields:="Party", _
ColumnFields:="Type"
 
I think this may be your problem:

Sheets("Month").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"Month!R1C1:R116C13").CreatePivotTable TableDestination:= "[Inter.xls]Results!R35C1",

The ACTIVE sheet is "Month" but the pivot table is on "Results" therefore:

ActiveSheet.PivotTables("MonthTable").AddFields RowFields:="Party", _
ColumnFields:="Type"
is trying to find a pivot table called monthtable on the "Month" sheet when it is actually on the "Results" sheet

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top