Change Pivot Table filter based on value on a different tab

angil0126

New Member
Joined
Apr 9, 2018
Messages
8
I have a workbook that contains a table (populated using ODBC connection to my ERP database), and two hidden pivot tables. There is code that copies the pivot tables and pastes as values on a different spreadsheet. This is done because there are some calculations that needed to be added that a pivot table can't handle. I need to change the pivot tables filter to the month that is entered on the main table. So a user would type '03/01/2023' and the two pivot tables would change their filter and the vba code that copies/pastes/etc would run afterwards. So I need to do all of this on a change of the values in Sheet 1. I can't seem to get it to work. Can anyone assist?
The code for copy/paste/etc works fine, it's just the changing of the filter on the two pivot tables. It is executing the vba, but I'm getting an error "Application-defined or object-defined error" The debug highlights the last line: myPivotField.CurrentPage = filterValue.

Sheet1 Name = "Trend" the field where the user is entering the month is B1
Sheet2 Name = "By Area Pivot" - This has PivotTable1 Filter filed = WIPMonth
Sheet 3 Name = "By PM Pivot" - This has PivotTable2 Filter filed = WIPMonth

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$B$1" Then

    Dim myPivotField As PivotField
    Dim myPivotField2 As PivotField
    Dim filterValue As String
   
    Set myPivotField = Sheets("By Area Pivot").PivotTables("PivotTable1").PivotFields("WIPMonth")
    filterValue = ActiveWorkbook.Sheets("Trend").Range("B1").Value
   

    myPivotField.CurrentPage = filterValue
   

   
End If

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I'm not sure what you mean. There aren't any calculations, just a field where a user will update the month. The filters need to change when that field changes
 
Upvote 0
Don't convert the value to a string first, instead:
VBA Code:
myPivotField.CurrentPage = ActiveWorkbook.Sheets("Trend").Range("B1").Value
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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