Update Pivot Table Filter With VBA - excel

I am trying to filter a pivot table based on the filter of another pivot table on a different sheet with a different data source.
I have two sheets (Main and Size Actual - Style) and want to filter by "Style". The Main sheet is where I want to choose the styles to filter by and from there I want to have the other sheet update with those selected styles filtered (would help if the filtering occurred with a button). I have changed some code to account for the field (Style) that I want to filter for. Right now I have the Main sheet pivot table resetting to show all values when a filter on style is selected on Size Actual - Style. I need that to be switched and instead of clearing the filter I want it to update with the selected styles from the filter.
Option Explicit
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
On Error Resume Next
Dim wsMain As Worksheet
Dim ws As Worksheet
Dim ptMain As PivotTable
Dim pt As PivotTable
Dim pfMain As PivotField
Dim pf As PivotField
Dim pi As PivotItem
Dim bMI As Boolean
On Error Resume Next
Set wsMain = ActiveSheet
Set ptMain = Target
Application.EnableEvents = False
Application.ScreenUpdating = False
For Each pfMain In ptMain.PageFields
bMI = pfMain.EnableMultiplePageItems
For Each ws In ThisWorkbook.Worksheets
For Each pt In ws.PivotTables
If ws.Name & "_" & pt <> wsMain.Name & "_" & ptMain Then
pt.ManualUpdate = True
Set pf = pt.PivotFields("Style")
bMI = pfMain.EnableMultiplePageItems
With pf
.ClearAllFilters
Select Case bMI
Case False
.CurrentPage = pfMain.CurrentPage.Value
Case True
.CurrentPage = "(All)"
For Each pi In pfMain.PivotItems
.PivotItems("Style").Visible =
pi.Visible
Next pi
.EnableMultiplePageItems = bMI
End Select
End With
bMI = False
Set pf = Nothing
pt.ManualUpdate = False
End If
Next pt
Next ws
Next pfMain
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
I am trying to have the filter on the second sheet update its filter based on the selection on the first sheets style filter.

Related

How to add a printer setting into this VBA Code?

I found the below VBA code on the internet which is very helpful, but I would like to add an extra step to change the settings on the printer to "Fit All Columns on One Page". How Can I do that?
Sub PrintFirstFilterItems()
'downloaded from contextures.com
'prints a copy of pivot table
'for each item in
'first Report Filter field
On Error Resume Next
Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Set ws = ActiveSheet
Set pt = ws.PivotTables(1)
Set pf = pt.PageFields(1)
If pf Is Nothing Then Exit Sub
For Each pi In pf.PivotItems
pt.PivotFields(pf.Name) _
.CurrentPage = pi.Name
ActiveSheet.PrintOut 'for printing
'ActiveSheet.PrintPreview 'for testing
Next pi
End Sub
I need the code to change the printer settings to this
I am new on VBA, and all I did was search on google how to change it but nothing worked.
Try the following...
Application.PrintCommunication = False
With ActiveSheet
With .PageSetup
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
End With
Application.PrintCommunication = True

Filter pivot table based on another pivot table filter on a different sheet with a different data source

I have 2 different pivot tables on different Excel sheets using different data sources. They both have a "Style" column that can be filtered. When I filter for style on one pivot table I want the other pivot table to automatically (or with a button) filter for those same selected styles.
I am currently working on this code I found on a different forum. In the objects section in VBA I have:
Sub ChangePivotFilter()
Dim WS As Excel.Worksheet
Dim aWB As Excel.Workbook
Dim myPivot As Excel.PivotTable
Dim myPivotField As Excel.PivotField
Set aWB = ActiveWorkbook
For Each WS In aWB.Worksheets
For Each myPivot In WS.PivotTables
Set myPivotField = Nothing
On Error Resume Next
Set myPivotField = myPivot.PivotFields(“Style”)
myPivotField.CurrentPage = “Style”
Next myPivot
Next WS
End Sub
In the module section in VBA I have:
Sub Button1_Click()
Dim s$
Application.EnableEvents = False
If Target.Name = "PivotTable1" Then
With ThisWorkbook.PivotTables
Let s = .Item("PivotTable1").PivotFields("Style").CurrentPage
.Item("PivotTable2").PivotFields("Style").ClearAllFilters
.Item("PivotTable2").PivotFields("Style").CurrentPage = s
End With
Else
With ThisWorkbook.PivotTables
Let s = .Item("PivotTable2").PivotFields("Style").CurrentPage
.Item("PivotTable1").PivotFields("Style").ClearAllFilters
.Item("PivotTable1").PivotFields("Style").CurrentPage = s
End With
End If
Application.EnableEvents = True
End Sub

Excel Pivot Table, VBA, change all Field Values to Count Numbers

I have this VBA marco which changes all the field values to Product.
Dim pt As PivotTable
Dim pf As PivotField
Dim ws As Worksheet
Set ws = ActiveSheet
Set pt = ws.PivotTables(1)
Application.ScreenUpdating = False
pt.ManualUpdate = True
For Each pf In pt.DataFields
pf.Function = xlProduct
Next pf
pt.ManualUpdate = False
Application.ScreenUpdating = True
Set pf = Nothing
Set pt = Nothing
Set ws = Nothing
End Sub
When when I try changing xlProduct to xlCount.Numbers, the code doesn't work. Any idea how to fix this?
xlCountNums is the proper function.
https://www.thespreadsheetguru.com/blog/2014/8/12/quickly-change-pivot-table-field-calculation-from-count-to-sum

Unable to unhide pivot items after setting visible = false once - Excel VBA

I am trying to show / hide pivot items based on a drop down selection.
If you see the code below, cell S3 is a drop down with options - All, Jan, Feb.. Dec. When I change the drop down, I want to show only the pivot items of the selected month.
What is happening here is, once I set an item visibility to false, I am not able to make it visible again. So, the for each loop below simply ignores those items that have been hidden earlier
Private Sub Worksheet_Change(ByVal Target As Range)
Dim pt As PivotTable, counter
Dim pi As PivotItem, msg, mname
Application.EnableEvents = False
If Target.Address = "$S$3" Then
Application.EnableEvents = False
Set pt = Sheet3.PivotTables(1)
mname = Sheet2.Range("S3").Value
pt.RefreshTable
For Each pi In pt.PivotFields("Values").PivotItems
If InStr(pi, mname) > 0 Or mname = "All" Then
pi.Visible = True
Else
pi.Visible = False
End If
Next pi
End If
Application.EnableEvents = True
End Sub
Screenshot of my Pivot Table
Try the code below (explanation inside the code as commnets):
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim pt As PivotTable
Dim pi As PivotItem
Dim pf As PivotField
Dim msg As String, mname As String
'Dim counter ' <-- not used in this piece of code
If Target.Address = "$S$3" Then
Application.EnableEvents = False
' set the PivotTable object
Set pt = Sheet3.PivotTables(1)
mname = Target.Value ' <-- you can use the Target object
pt.RefreshTable
' set the PivotField object
Set pf = pt.PivotFields("Values")
pf.ClearAllFilters ' clear all previous filters from "Values"
' if value is "All", show all items, (no need to run the loop)
If mname <> "All" Then
For Each pi In pf.PivotItems
If Not pi.Name Like "*" & mname & "*" Then ' only if the value is not like the month >> hide the PivotItem
pi.Visible = False
End If
Next pi
End If
End If
Application.EnableEvents = True
End Sub

Using "For Each" for apply same filters for all pivot tables in the worksheet

I guess this is an easy question but since I'm new in VBA I just can't figure it out.
I have this code which goes to Pivot table1, select the DATA Pivot field and filter some values for this field.
Sub Multiple_Filtering()
Dim pf As PivotField
Set pf = ActiveSheet.PivotTables("Pivot table1").PivotFields("DATA")
'Enable filtering on multiple items
pf.EnableMultiplePageItems = True
'Must turn off items you do not want showing
pf.PivotItems("201511").Visible = False
pf.PivotItems("201512").Visible = False
pf.PivotItems("201501").Visible = True
pf.PivotItems("201502").Visible = True
End Sub
What I want to do is make a loop which does the same for all pivot tables in all worksheets in the workbook.
I found this piece of code which uses "For each" sentence to sets the data source for all pivot tables in the workbook, but I can't adapt it for my code.
Dim sht As Worksheet
Dim pvt As PivotTable
For Each sht In ThisWorkbook.Worksheets
For Each pvt In sht.PivotTables
pvt.ChangePivotCache _
ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SourceAddress)
pvt.RefreshTable
Next pvt
Next sht
Any ideas?
Sub Multiple_Filtering()
Dim pf As PivotField
Dim sht As Worksheet
Dim pvt As PivotTable
For Each sht In ThisWorkbook.Worksheets
For Each pvt In sht.PivotTables
Set pf = pvt.PivotFields("DATA")
'Enable filtering on multiple items
pf.EnableMultiplePageItems = True
'Must turn off items you do not want showing
pf.PivotItems("201511").Visible = False
pf.PivotItems("201512").Visible = False
pf.PivotItems("201501").Visible = True
pf.PivotItems("201502").Visible = True
Next pvt
Next sht
End Sub
I believe this should integrate the two pieces of code shown there
Sub Multiple_Filtering()
Dim sht As Worksheet
Dim pvt As PivotTable
Dim pf As PivotField
For Each sht In ThisWorkbook.Worksheets
For Each pvt In sht.PivotTables
Set pf = sht.PivotTables(pvt).Pivotfields("DATA")
'Enable filtering on multiple items
pf.EnableMultiplePageItems = True
'Must turn off items you do not want showing
pf.PivotItems("201511").Visible = False
pf.PivotItems("201512").Visible = False
pf.PivotItems("201501").Visible = True
pf.PivotItems("201502").Visible = True
'Admittedly not sure if this is necessary
pvt.RefreshTable
Next pvt
Next sht
End Sub

Resources