I am trying to automate some pieces of my reports for work. I want to have a macro that will take all the raw data in a worksheet and create/design the same pivot table every time.
I have been able to create a normal table in this way by avoiding absolutes such as Ctrl+shft+ down arrow to select all the data but with Pivot Tables I run into this error:
Just to be as specific as possible. With my macro use I have been unable to:
- Create a Pivot Table
- Rename a worksheet
- Sort the values from largest to smallest.
In all my macros, these 3 things will cause it to crash in a run time error.
Is there anyway to modify the code to allow these three things to work?
Here is my Macro code. The three things I want to accomplish are in this pivot table macro. Sorry for any miss clicks in the creation of the macro. Thanks for any help you guys might provide.
Sub Create_pivotTable()
'
' Create_pivotTable Macro
'
'
Cells.Select
Sheets.Add
ActiveWorkbook.Worksheets("Pivot Table").PivotTables("PivotTable6").PivotCache. _
createPivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
, DefaultVersion:=xlPivotTableVersion14
Sheets("Sheet1").Select
Cells(3, 1).Select
Sheets("Sheet1").Select
Sheets("Sheet1").Move Before:=Sheets(5)
Sheets("Sheet1").Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Debit Party Name")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Credit Party Name")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Original Amount"), "Count of Original Amount", _
xlCount
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Transaction Date"), "Count of Transaction Date", _
xlCount
Sheets("Sheet1").Name = "Piv Tab"
Rows("1:2").Select
Range("A2").Activate
Selection.Delete Shift:=xlUp
Range("A1").Select
ActiveSheet.PivotTables("PivotTable1").CompactLayoutRowHeader = _
"ORIGINATORS | BENEFICIARY'S"
Range("B1").Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"Count of Original Amount")
.Caption = "Amount"
.Function = xlSum
End With
Range("C1").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Transaction Date") _
.Caption = "Count"
Range("B2").Select
Range(Selection, Selection.End(xlDown)).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Amount")
.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
End With
Range("B2").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Debit Party Name"). _
AutoSort xlDescending, "Amount", ActiveSheet.PivotTables("PivotTable1"). _
PivotColumnAxis.PivotLines(1), 1
Range("B4").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Credit Party Name"). _
AutoSort xlDescending, "Amount", ActiveSheet.PivotTables("PivotTable1"). _
PivotColumnAxis.PivotLines(1), 1
Columns("D:D").ColumnWidth = 45.86
Range("D1").Select
ActiveCell.FormulaR1C1 = "Analysis"
Range("B1").Select
ActiveSheet.PivotTables("PivotTable1").TableStyle2 = "PivotStyleMedium3"
Range("A1").Select
Selection.Copy
Range("D1").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Font.Bold = True
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Columns("C:C").Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("D7").Select
End Sub