Guest User

Untitled

a guest
Jan 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. Sub Pivot2Create()
  2.  
  3. 'Declare Variables
  4. Dim PSheet As Worksheet
  5. Dim DSheet As Worksheet
  6. Dim PCache As PivotCache
  7. Dim PTable As PivotTable
  8. Dim PRange As Range
  9. Dim LastRow As Long
  10. Dim LastCol As Long
  11. Dim wb1 As Workbook
  12.  
  13.  
  14. Set wb1 = ThisWorkbook
  15.  
  16. Application.ScreenUpdating = False
  17. 'Insert a New Blank Worksheet
  18. On Error Resume Next
  19. Application.DisplayAlerts = False
  20. Worksheets("Second Pivot").Delete
  21. Sheets.Add Before:=ActiveSheet
  22. ActiveSheet.Name = "Second Pivot"
  23. Application.DisplayAlerts = True
  24. Set PSheet = Worksheets("Second Pivot")
  25. Set DSheet = Worksheets("Current Report")
  26.  
  27. 'Define Data Range
  28. LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
  29. LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
  30. Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
  31.  
  32. 'Define Pivot Cache
  33. Set PCache = ActiveWorkbook.PivotCaches.Create _
  34. (SourceType:=xlDatabase, SourceData:=PRange). _
  35. CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
  36. TableName:="Uniper3rdParty")
  37.  
  38. 'Insert Blank Pivot Table
  39. Set PTable = PCache.CreatePivotTable _
  40. (TableDestination:=PSheet.Cells(1, 1), TableName:="Uniper3rdParty")
  41.  
  42. 'Insert Row Fields
  43. With ActiveSheet.PivotTables("Uniper3rdParty").PivotFields("Pers.No.")
  44. .Orientation = xlRowField
  45. .RowAxisLayout (xlTabularRow)
  46. .Position = 1
  47. End With
  48.  
  49. With ActiveSheet.PivotTables("Uniper3rdParty").PivotFields("Last name First name")
  50. .Orientation = xlRowField
  51. .RowAxisLayout (xlTabularRow)
  52. .Position = 2
  53. End With
  54.  
  55. 'Insert Column Fields
  56. With ActiveSheet.PivotTables("Uniper3rdParty").PivotFields("Wage Type Long Text")
  57. .Orientation = xlColumnField
  58. .Position = 1
  59. End With
  60.  
  61. 'Insert Data Field
  62. With ActiveSheet.PivotTables("Uniper3rdParty").PivotFields("Amount")
  63. .Orientation = xlDataField
  64. .NumberFormat = "#,##0.00"
  65. End With
  66.  
  67. With Psheet.PivotTables("Uniper3rdParty")
  68. 'What you asked
  69. .RowAxisLayout xlTabularRow
  70. 'Personally, I would add the below rows but feel free to remove them
  71. dim pf as PivotField
  72. For Each pf In .PivotFields
  73. pf.Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
  74. Next pf
  75. .ShowDrillIndicators = False
  76. End With
  77.  
  78. Sub FormatPivot()
  79. With ActiveSheet.PivotTables("Uniper3rdParty")
  80. .InGridDropZones = True
  81. .RowAxisLayout xlTabularRow
  82. End With
  83. ActiveSheet.PivotTables("Uniper3rdParty").PivotFields("Pers.No.").Subtotals = _
  84. Array(False, False, False, False, False, False, False, False, False, False, False, False)
  85. End Sub
Add Comment
Please, Sign In to add comment