Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am trying to write macros that create two pivot tables from separate sheets back to back, and group them by month but every time I try to run my code I keep getting an error. Here is my code:
Sub PivotTable()
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Sheets("Information").UsedRange).CreatePivotTable TableDestination:="Pivot!R1C4", TableName:="PivotTable", DefaultVersion:=xlPivotTableVersion10
With Sheets("Pivot").PivotTables("PivotTable").PivotFields("PN")
.Orientation = xlRowField
.Position = 1
End With
With Sheets("Pivot").PivotTables("PivotTable").PivotFields("Commit")
.Orientation = xlColumnField
.Position = 1
End With
Sheets("Pivot").PivotTables("PivotTable").AddDataField Sheets("Pivot").PivotTables("PivotTable").PivotFields("Qty"), "Sum", xlSum
End Sub
Sub GroupPivot()
Dim therange As Range
Dim PT As PivotTable
Set PT = Sheets("Pivot").PivotTables("PivotTable")
Set therange = PT.PivotFields("Commit").DataRange.Cells(1)
therange.Select
Selection.Group Start:=True, End:=True, Periods:=Array(False, False, False, _
False, True, False, False)
End Sub
Sub PivotTableNY()
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Sheets("InformationNextYear").UsedRange).CreatePivotTable TableDestination:="PivotNextYear!R1C4", TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
With Sheets("PivotNextYear").PivotTables("PivotTable1").PivotFields("Material")
.Orientation = xlRowField
.Position = 1
End With
With Sheets("PivotNextYear").PivotTables("PivotTable1").PivotFields("Deliv. Date")
.Orientation = xlColumnField
.Position = 1
End With
Sheets("PivotNextYear").PivotTables("PivotTable1").AddDataField Sheets("PivotNextYear").PivotTables("PivotTable1").PivotFields("Open Quantity"), "Sum", xlSum
End Sub
Sub GroupPivotNY()
Dim myrange As Range
Dim PT As PivotTable
Set PT = Sheets("PivotNextYear").PivotTables("PivotTable1")
Set myrange = PT.PivotFields("Material").DataRange.Cells(1)
myrange.Select
Selection.Group Start:=True, End:=True, Periods:=Array(False, False, False, _
False, True, False, False)
End Sub
I keep getting this error message:
"Rum time Error '1004': Select Method of Range class failed."
The error appears in line: therange.Select
under Sub GroupPivot.
Can someone help me resolve this error?
–
–
–
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.