Pivot Table code

IThelp

New Member
Joined
Jan 2, 2017
Messages
32
Code:
'+----------------------------+
'|  SALES (£) BY DAY BY WEEK  |
'+----------------------------+
Sub salesByDayByWeek_PivotFields()
Dim weekNum As Integer


On Error Resume Next

Sheets("SalesDBPivot").Select
Dim pvt As PivotTable
Dim pf As PivotField

Set pvt = ActiveSheet.PivotTables("PivotTable1")
pvt.ClearTable
    
'Turn on Automatic updates/calculations --like screenupdating to speed up code
    pvt.ManualUpdate = True
    
'Add item to the Report Filter (xlPageField / "Filter")
    
    With pvt.PivotFields("WeekNo")
        .Orientation = xlPageField
        .Position = 1
    End With
  
'Add item to the Column Labels (xlColumnField / "Columns")
    With pvt.PivotFields("DayOfWeek")
        .Orientation = xlColumnField
    End With


    With pvt.PivotFields("DayOfWeek")
        .PivotItems("Saturday").Visible = False
        .PivotItems("Sunday").Visible = False
    End With
    
'Add item to the Row Labels (xlRowField / "Rows")
    With pvt.PivotFields("Item Name For Lookup")
        .Orientation = xlRowField
        .Position = 1
    End With
    
'Add Calculated Field, which is adding item to Values (xlCount / "Values") xlCount could be sum etc
    For Each pf In pvt.PivotFields
       If pf.SourceName = "TotalPrice" Then
          Exit For
       End If
     Next
  pvt.AddDataField pf, "Sum of Total Price", xlSum
  

'Position Item in list
    'pvt.PivotFields("xxx").Position = 1
    
'Format Pivot Field for formatting data
    pvt.PivotFields("Sum of Total Price").NumberFormat = "£#,##0.00"

'Set xxxx Filter
curWeek = Year(Now()) & "-" & (Format(Now(), "ww") + weekNum)

'MsgBox (curWeek)
'curMonth = MonthName(Month(Now()))
    ActiveSheet.PivotTables("PivotTable1").PivotFields("WeekNo").ClearAllFilters
    ActiveSheet.PivotTables("PivotTable1").PivotFields("WeekNo").CurrentPage = curWeek

pvt.ManualUpdate = False
pvt.RefreshTable

LastCol = Range(pvt.TableRange1.Address).Columns.Count + Range(pvt.DataLabelRange.Address).Column - 2
lastrow = Range(pvt.TableRange1.Address).Rows.Count
pvt.PivotFields("Item Name For Lookup").AutoSort xlDescending, "Sum of Quantity", pvt.PivotColumnAxis.PivotLines(LastCol - 1), 1
'pvt.PivotFields("Item Name For Lookup").AutoSort xlDescending, "Sum of Quantity", pvt.PivotColumnAxis.PivotLines(3), 1
    
End Sub

Hi,

I have used this vba code to update a pivot table which contains sales data.

when the pivot table updates, it contains the right products from the sales sheet, However, it for the price, it comes up as 0.
On the pivot field list, the total price is put as the value, when it is put as a row label, it comes up with the correct price. The total price needs to come as the value.

If there is any way you could help it would be much appreciated.

Many thanks
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Your repeated post has been deleted. please do not create duplicate posts
 
Upvote 0

Forum statistics

Threads
1,215,263
Messages
6,123,959
Members
449,135
Latest member
jcschafer209

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