Help with macro

INOPOOL

New Member
Joined
Feb 3, 2011
Messages
43
I am trying to create a macro for a pivot table i am creating, but it gives me an error message whenver i try to run the macro. This is my code:

Sheets.Add
ActiveWorkbook.Worksheets("Summary").PivotTables("PivotTable3").PivotCache. _
CreatePivotTable TableDestination:="Sheet4!R3C1", TableName:="PivotTable3" _
, DefaultVersion:=xlPivotTableVersion12
Sheets("Sheet4").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields( _
"Cost_Center_(Division)")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Department")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Employee_Number"), "Count of Employee_Number", _
xlCount
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
when you create a macro to run pivot tables it is usually best to create a more unique name

PT3 instead of the excel generated PivotTable3
 
Upvote 0
Error message is Run-time error '5':

Invalid procedure call or argument.

This is the new code:
Sub Macro1()
'
' Macro1 Macro
'
'
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Employee Roster- Active Employe!R1C1:R2547C5", Version:= _
xlPivotTableVersion12).CreatePivotTable TableDestination:="Sheet1!R3C1", _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion12
Sheets("Sheet1").Select
Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Employee_Number"), "Count of Employee_Number", _
xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"Cost_Center_(Division)")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Department")
.Orientation = xlColumnField
.Position = 1
End With
End Sub
 
Upvote 0
in the vb editor do a replace
find: PivotTable1
replace PT1

what line does it give you the error?
 
Upvote 0
Did the replacement...here goes the code:

Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=

"Employee Roster- Active Employe!R1C1:R2547C5", Version:= _
xlPivotTableVersion12).CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PT1", DefaultVersion:=xlPivotTableVersion12
Sheets("Sheet1").Select
Cells(3, 1).Select
ActiveSheet.PivotTables("PT1").AddDataField ActiveSheet.PivotTables( _
"PT1").PivotFields("Employee_Number"), "Count of Employee_Number", _
xlCount
With ActiveSheet.PivotTables("PT1").PivotFields( _
"Cost_Center_(Division)")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PT1").PivotFields("Department")
.Orientation = xlColumnField
.Position = 1
End With
End Sub

--When I hit the debug in the error pop up box, it highlights the lines that I highlighted in red...maybe those are the error lines???
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top