willyboy58
Technical User
I created a pivot table by recording the macro. I did not stop to think that the next month’s data would not be of the same range size.
'the following is part of the original code produced by the recorded macro:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= "'Branch 3'!R1C1:R1646C5"
.CreatePivotTable TableDestination:="", TableName:="PivotTable6"
Given that the range size can change, I changed my code to the following:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
Activecell.CurrentRegion.Select).CreatePivotTable _
TableDestination:="", TableName:="PivotTable6"
The new code selects the proper range of cells as it should, but I get the message: “Invalid procedure call or argument.” In the code module area, the little arrow to the left is at the line “ TableDestination:="", TableName:="PivotTable6". What can I do to fix this?
If needed, the complete procedure follows:
Sheets("Branch 3"
.Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
Activecell.CurrentRegion.Select).CreatePivotTable _
TableDestination:="", TableName:="PivotTable6"
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable6"
.SmallGrid = False
With ActiveSheet.PivotTables("PivotTable6"
.PivotFields("number"
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable6"
.PivotFields("name"
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable6"
.PivotFields("job"
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("PivotTable6"
.PivotFields("date"
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable6"
.PivotFields("hours"
.Orientation = xlDataField
.Position = 1
End With
ActiveSheet.Name = "Pivot Table 3"
Application.CommandBars("PivotTable"
.Visible = False
End Sub
TIA. Bill
'the following is part of the original code produced by the recorded macro:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= "'Branch 3'!R1C1:R1646C5"
Given that the range size can change, I changed my code to the following:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
Activecell.CurrentRegion.Select).CreatePivotTable _
TableDestination:="", TableName:="PivotTable6"
The new code selects the proper range of cells as it should, but I get the message: “Invalid procedure call or argument.” In the code module area, the little arrow to the left is at the line “ TableDestination:="", TableName:="PivotTable6". What can I do to fix this?
If needed, the complete procedure follows:
Sheets("Branch 3"
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
Activecell.CurrentRegion.Select).CreatePivotTable _
TableDestination:="", TableName:="PivotTable6"
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable6"
With ActiveSheet.PivotTables("PivotTable6"
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable6"
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable6"
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("PivotTable6"
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable6"
.Orientation = xlDataField
.Position = 1
End With
ActiveSheet.Name = "Pivot Table 3"
Application.CommandBars("PivotTable"
End Sub
TIA. Bill