*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!
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!