VBA to change Value Fields of Pivot Tables with Slicer

anariskenderli

New Member
Joined
Apr 20, 2023
Messages
3
Office Version
  1. 2021
Platform
  1. Windows
Hello, everyone.

I have a problem on VBA that I can not solve for days. I would be very grateful if anyone could help me on this issue.
ByYear and ByStore Sheets have pivot tables which both names are PivotTable1. I need both pivot tables' Value fields to be changed by slicer without connection with each other. So, in Metadata sheet I created two pivot tables based on two tables and inserted two slicers and replaced them to ByYear and ByStore Sheets. At last I modified a VBA code I found on Internet, put it in ThisWorkbook but it didn't work.
Thanks in advance for any help.

The link for sample xlsm file is:

The modified VBA code is:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)

Dim ptMain As PivotTable
Dim pfMeasure As PivotField
Dim i As Long
Dim rangeName As String

On Error GoTo Errorhandler

If ActiveSheet.Name = "ByYear" Then
rangeName = "Metadata!$B$11"
Set ptMain = Sheets("ByYear").PivotTables("PivotTable1")
ElseIf ActiveSheet.Name = "ByStore" Then
rangeName = "Metadata!$D$11"
Set ptMain = Sheets("ByStore").PivotTables("PivotTable1")
Else
Exit Sub
End If


For Each pfMeasure In ptMain.DataFields
pfMeasure.Orientation = xlHidden
Next

i = 0
Do While Range(rangeName).Offset(i, 0).Value <> ""
ptMain.AddDataField ptMain.PivotFields(Range(rangeName).Offset(i, 0).Value)
i = i + 1
Loop

Exit Sub

Errorhandler:
Debug.Print Now(), Err.Description

End
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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