Pivot in VBA

chiragpandya

New Member
Joined
Feb 26, 2018
Messages
5
Trying to make the following code work but not able to create the pivot
Any ideas?

Code:
Sub Pivott()


    Dim PSheet As Worksheet
    Dim DSheet As Worksheet
    Dim PCache As PivotCache
    Dim PTable As PivotTable
    Dim PRange As Range
    Dim LastRow As Long
    Dim LastCol As Long


On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Segment Summary").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "Segment Summary"
Application.DisplayAlerts = True
Set PSheet = Worksheets("Segment Summary")
Set DSheet = Worksheets("Working File")


LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column






Set PRange = DSheet.Cells(9, 1).Resize(LastRow, LastCol)


Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="SalesPivotTable")




Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="SalesPivotTable")




With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Segment")
.Orientation = xlRowField
.Position = 1
End With


With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Cur CSP")
.Orientation = xlColumnField
.Position = 1
End With




With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Actual_Sales")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Revenue "
End With


With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Actual_Cost")
.Orientation = xlDataField
.Position = 2
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Revenue "
End With


With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Actual_Cost_Support")
.Orientation = xlDataField
.Position = 3
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Revenue "
End With






End Sub
 
Last edited by a moderator:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top