Updating Recorded Macro for Pivot Table to Work for Various Data Sets

cash1257

New Member
Joined
Feb 13, 2015
Messages
2
*I have VERY limited experience with coding*

I am trying to use VBA to insert a pivot table. I am having trouble modifying the recorded macro adding the pivot table. When I try to repeat the macro, I am getting an error "Runtime error 1004"


My table has the following columns (starting at A5):
[TABLE="width: 889"]
<tbody>[TR]
[TD="class: xl65, width: 91"]


Maintainer[/TD]
[TD="class: xl65, width: 77"]

Territory
ID[/TD]
[TD="class: xl65, width: 72"]

Trouble
Code[/TD]
[TD="class: xl65, width: 81"]

Trouble
Ticket#[/TD]
[TD="class: xl65, width: 121"]


Trouble Description[/TD]
[TD="class: xl65, width: 72"]
Affected
Resource
/DOT#[/TD]
[TD="class: xl65, width: 103"]


Subdivision[/TD]
[TD="class: xl65, width: 105"]
Ticket
Description
Summary[/TD]
[TD="class: xl65, width: 48"]

Line
Seg[/TD]
[TD="class: xl65, width: 73"]

Mile
Post[/TD]
[TD="class: xl65, width: 46"]

Start
Date[/TD]
[/TR]
</tbody>[/TABLE]

In the pivot table, I need:
-"Maintainer" set as a filter
-"Mile Post" set as a column
-"Trouble Code" set as a row
-Sum of Mile Post as the value

When I generate the recorded Macro of adding the pivot table, I get the following:

Sub Macro1()

Sheets.Add
ActiveWorkbook.Worksheets("Sheet5").PivotTables("PivotTable22").PivotCache. _
CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
, DefaultVersion:=xlPivotTableVersion15
Sheets("Sheet1").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("" & Chr(10) & "" & Chr(10) & "" & Chr(10) & "Maintainer")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("" & Chr(10) & "" & Chr(10) & "Mile" & Chr(10) & "Post")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("" & Chr(10) & "" & Chr(10) & "Mile" & Chr(10) & "Post"), "Count of " & Chr(10) & "" & Chr(10) & "Mile" & Chr(10) & "Post", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields("" & Chr(10) & "" & Chr(10) & "Trouble" & Chr(10) & "Code")
.Orientation = xlRowField
.Position = 1
End With
End Sub




I have tried for hours to fix this. Any help would be appreciated!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Are you building a PivotTable from an existing PivotTable? What is 'Sheet5' in your workbook?

If you are not trying to build from an existing PivotTable, can you provide some additional information about your data source? For example, your table starting in A5, what sheet is it in?

I wrote some code that will probably make your life easier, but just need those extra pieces of information.
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,629
Members
452,661
Latest member
Nonhle

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