Sub Macro2()
Dim LastRow As Long
Sheets.Add(After:=Sheets("Pivot Table")).Name = "Credits"
Sheets("Pivot Table").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Type").CurrentPage = _
"(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Type")
.PivotItems("Clawback").Visible = False
.PivotItems("Credit Note").Visible = True
.PivotItems("Invoice").Visible = False
.PivotItems("Unallocated Cash").Visible = True
End With
Cells.Select
Selection.Copy
Sheets("Credits").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Rows("1:3").Select
Selection.ClearContents
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Range("F:F,H:H,J:J,L:L,N:N,P:P").Select
Selection.Delete Shift:=xlToLeft
Range("K4:L4").Select
Selection.Cut
Range("K5").Select
ActiveSheet.Paste
Range("E4:J4").Select
Selection.Cut
Range("E5").Select
ActiveSheet.Paste
Range("K5").Select
ActiveCell.FormulaR1C1 = "Grand Total"
'Setup overdue column
Columns("L:L").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("L5").Select
ActiveCell.FormulaR1C1 = "Overdue"
Range("L6").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-6]:RC[-2])"
Selection.AutoFill Destination:=Range("L6:L" & Range("K" & Rows.Count).End(xlUp).Row)
'Setup 90+ column
Columns("M:M").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("M5").Select
ActiveCell.FormulaR1C1 = "90+"
Range("M6").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-5]:RC[-3])"
Selection.AutoFill Destination:=Range("M6:M" & Range("K" & Rows.Count).End(xlUp).Row)
Range("N5").Select
ActiveCell.FormulaR1C1 = "Unallocated Cash"
'Delete grand total line from data.
LastRow = Cells(Rows.Count, 11).End(xlUp).Row
ActiveSheet.Range("A5:N" & LastRow).AutoFilter Field:=1, Criteria1:= _
"Grand Total"
ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 1).EntireRow.Select
Selection.Delete Shift:=xlUp
ActiveSheet.AutoFilter.ShowAllData
'Put border around all of the data
Range("A5").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
'Colour headings with company colours & white font.
Range("A5:N5").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 8075096
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Range("A5:N5").Select
Selection.Font.Bold = True
'Insert new column for neatness
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.ColumnWidth = 0.92
'Setup report title
Range("B2:O2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ActiveCell.FormulaR1C1 = "Aged Debtors Report - Credit Items"
With Selection.Font
.Name = "Calibri"
.Size = 22
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Selection.Font.Bold = True
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Range("B2:O2").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
'Insert further column for neatness
Rows("3:3").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.RowHeight = 6
'Setup subtotal section
Range("E4").Select
ActiveCell.FormulaR1C1 = "Totals"
Selection.Font.Bold = True
LastRow = Cells(Rows.Count, "O").End(xlUp).Row
MsgBox LastRow
Range("O4").Select
ActiveCell.FormulaR1C1 = "=SUBTOTAL(9,R[3]C:R[" & LastRow & "]C)"
Selection.AutoFill Destination:=Range("F4:O4"), Type:=xlFillDefault
Range("E4:O4").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("F4:O4").Select
Selection.Font.Bold = True
Selection.NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
'Format numbers within data to appear as currency.
ActiveSheet.Range("F7:O" & LastRow).Select
Selection.NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
'Autofit all columns
Cells.Select
Cells.EntireColumn.AutoFit
End Sub