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

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

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

VBA to select all and deselect 0 & blank in one field on pivot tables across multiple sheets

This is a re-post of my problem here because I dun goofed and thought I solved it when my VBA didn't work.
Essentially what I want the VBA to do is go to the pivot tables across multiple sheets, in the quantity field I want it to select all and then deselect 0 and blank. This will select new data in the filter when I have refreshed it with the new input data for the report I do on a weekly basis. The code is as below
Sub Main()
With ThisWorkbook
FilterOutZeroAndBlanks .Worksheets("Cairns Table").PivotTables("PivotTable1")
FilterOutZeroAndBlanks .Worksheets("Other Table").PivotTables("PivotTable1")
End With
End Sub
Public Sub FilterOutZeroAndBlanks(pvt As PivotTable)
Dim pvtField As PivotField
Set pvtField = pvt.PivotFields("Quantity")
Dim item As PivotItem
Dim counter As Long
Dim targetCounter As Long
With pvtField
For Each item In .PivotItems
If item.Visible Then counter = counter + 1
Next item
If .PivotItems("0").Visible And .PivotItems("(blank)").Visible Then
targetCounter = 2
ElseIf .PivotItems("0").Visible Or .PivotItems("(blank)").Visible Then
targetCounter = 1
End If
If Not targetCounter = counter Then
.PivotItems("0").Visible = False
.PivotItems("(blank)").Visible = False
End If
End With
End Sub
Blockquote
Try this:
Public Sub FilterOutZeroAndBlanks(pvt As PivotTable)
With pvt.PivotFields("Quantity")
.ShowAllItems = True 'unhide all
.PivotItems("0").Visible = False
.PivotItems("(blank)").Visible = False
End With
End Sub

Macro on combo box and Pivot filtering take too long to complete

I have a macro that work but it takes too long to complete - 45sec.
What it does is to take a combo box result to update a pivot table filtering.
Do you know what I did wrong and how could I improve it please?:)
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim wsChart As Worksheet
Dim wsPivot As Worksheet
Dim SelGeo As Variant
'turn on automatic update / calculation in the Pivot Table
Set wsChart = ThisWorkbook.Sheets("Graph Data")
Set wsPivot = ThisWorkbook.Sheets("PCW_pivot")
Set pt = ThisWorkbook.Sheets("PCW_pivot").PivotTables("Pivot_table1")
Set SelGeo = ThisWorkbook.Sheets("Graph Data").Range("SelGeo")
pt.ManualUpdate = True
Application.ScreenUpdating = False
ThisWorkbook.Sheets("PCW_pivot").PivotTables("Pivot_table1").ClearAllFilters
If ThisWorkbook.Sheets("Graph Data").Range("SelGeo") = WW Then
Exit Sub
End If
For Each pi In pt.PivotFields("Geo").PivotItems
Select Case pi.Name
Case [SelGeo]
pi.Visible = True
Case Else
pi.Visible = False
End Select
Next pi
'turn on automatic update / calculation in the Pivot Table
pt.ManualUpdate = False
Application.ScreenUpdating = True
End Sub
Thank you very much.
You can try turning off calculation. Also, consider testing for the negative of your test... this allows for code that can run from start to end naturally without needed to Exit Sub in the middle
ThisWorkbook.Sheets("PCW_pivot").PivotTables("Pivot_table1").ClearAllFilters
If ThisWorkbook.Sheets("Graph Data").Range("SelGeo") <> WW Then
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each Pi In pt.PivotFields("Geo").PivotItems
Select Case Pi.Name
Case [SelGeo]
Pi.Visible = True
Case Else
Pi.Visible = False
End Select
Next Pi
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End If
End Sub

Update Pivot Table Filter With VBA

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.

Disabling other slicers in excel when slicer is pressed

I have three slicers operating on a pivot table and pivot chart in excel. However, the filters placed from the two other slicers has to be cleared, when one of the slicers is pressed, assuring that only one slicer is operating at the same time. I think this has to be solved using VBA, listening for a click then executing code, other than that I have no idea as I have never worked with Excel or VBA before.
Anyone got any suggestions on how I would do this?
Working out what Slicer got clicked is very tricky indeed, because the only application event that gets raised by clicking on a slicer is the PivotTable_Update event. This event tells us which PivotTable the slicer is connected to, but not which field in that PivotTable got filtered. So if you have multiple silcers connected to a PivotTable, you can't tell which one was just clicked on.
I came up with a very convoluted workaround that I posted at http://dailydoseofexcel.com/archives/2014/07/10/what-caused-that-pivottableupdate-episode-iv/ that will get you part the way there: it will tell you which field in a PivotTable just got updated, and then you just need to iterate through all the slicers connected to that PivotTable and clear them if they don't have the same sourcename.
I'll see if I can code something up in due course, but I'm pretty busy at present so I can't promise a fast resolution.
Note that you can assign a macro directly to a slicer that gets triggered when a user clicks on it, and from that you can determine which slicer it is. But unfortunately that macro interferes with the slicer itself: a user can no longer actually operate the slicer to actually change anything.
---UPDATE---
Here's some code that does what you want. There's a lot of different modules here, because the routine code calls quite a few other generic routines I use. And at it's heart is a routine that works out which particular field of a PivotTable gets updated, and that doesn't care if multilpe fields are filtered.
You call it with this event handler, which goes in the ThisWorkbook module for the book in the Visual Basic Editor:
Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
Slicers_OneSlicerOnly Target
End Sub
And that calls these other functions in turn. You don't have to amend anything, and this works on any PivotTables or Slicers you add to this workbook.
Function Slicers_OneSlicerOnly(target As PivotTable)
Dim sField As String
Dim slr As Slicer
Dim sSlicer As String
Dim bEnableEvents As Boolean
Dim bScreenUpdating As Boolean
Dim bManualupdate As Boolean
Dim lCalculation As Long
Dim bRecordLayout As Boolean
Dim sLayout_New As String
Dim sLayout_Old As String
Dim lng As Long
With Application
bEnableEvents = .EnableEvents
bScreenUpdating = .ScreenUpdating
lCalculation = .Calculation
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
bManualupdate = target.ManualUpdate
target.ManualUpdate = True
sField = Pivots_FieldChange(target)
If sField <> "" Then
For Each slr In target.Slicers
sSlicer = slr.SlicerCache.SourceName
If sSlicer <> sField Then
If Not target.PivotFields(sSlicer).AllItemsVisible Then
target.PivotFields(sSlicer).ClearAllFilters
bRecordLayout = True
End If
End If
Next slr
End If
target.ManualUpdate = bManualupdate
If bRecordLayout Then
PivotChange_RecordLayout target, sLayout_New
With target
lng = InStr(.Summary, "[Layout]")
sLayout_Old = Mid(.Summary, lng + Len("[Layout]"), InStr(.Summary, "[/Layout]") - Len("[Layout]") - lng)
.Summary = Replace(.Summary, "[Layout]" & sLayout_Old & "[/Layout]", "[Layout]" & sLayout_New & "[/Layout]")
End With
End If
With Application
.EnableEvents = bEnableEvents
.ScreenUpdating = bScreenUpdating
.Calculation = lCalculation
End With
End Function
Public Function Pivots_FieldChange(target As PivotTable) As String
' Description: Works out what caused a PivotTableUpdate event, and if caused by someone changing a filter returns the
' name of the PivotField that was filtered.
' Programmer: Jeff Weir
' Contact: weir.jeff#gmail.com or jeff.weir#HeavyDutyDecisions.co.nz
' Inputs: PivotTable
' Outputs: String
' Name/Version: Date: Ini: Modification:
' PivotChange_20140712 20140712 JSW Initial programming as per http://dailydoseofexcel.com/archives/2014/07/10/what-caused-that-pivottableupdate-episode-iv/
' PivotChange_20140723 20140423 JSW Restructured code as per http://dailydoseofexcel.com/archives/2014/07/23/broken-arrow/
' PivotChange_20140802 20140802 JSW Added: If sLastUndoStackItem = "Filter" Or sLastUndoStackItem = "Slicer Operation" Then
' so that Filter routines only get called in response to filtering
' Pivots_FieldChange 20151016 JSW Changed the way info is saved in .summary
Dim sLastUndoStackItem As String
Dim sField As String
Dim sPossibles As String
Dim sLayout_New As String
Dim sLayout_Old As String
On Error Resume Next 'in case the undo stack has been wiped or doesn't exist
sLastUndoStackItem = Application.CommandBars(14).FindControl(ID:=128).List(1) 'Standard Commandbar, undo stack
On Error GoTo 0
If sLastUndoStackItem = "Filter" Or sLastUndoStackItem = "Slicer Operation" Then
sField = PivotChange_CompareLayout(target, sLayout_New, sLayout_Old)
If sField = "" Then sField = PivotChange_EliminationCheck(target, sPossibles)
If sField = "" Then sField = PivotChange_UndoCheck(target, sPossibles)
If sLayout_Old = "" Then
target.Summary = "[Layout]" & sLayout_New & "[/Layout]"
Else
target.Summary = Replace(target.Summary, "[Layout]" & sLayout_Old & "[/Layout]", "[Layout]" & sLayout_New & "[/Layout]")
End If
End If
Pivots_FieldChange = sField
Debug.Print Now() & vbTab & "Pivots_FieldChange:" & vbTab & sField
End Function
Function PivotChange_RecordLayout(pt As PivotTable, ByRef sLayout_New As String) As Boolean
Dim pf As PivotField
For Each pf In pt.PivotFields
With pf
Select Case .Orientation
Case xlRowField, xlColumnField
sLayout_New = sLayout_New & .Name & "|" & .VisibleItems.Count & "|" & .VisibleItems(1).Name & "||"
Case xlPageField
'pf.VisibleItems.Count doesn't work on PageFields
'So for PageFields we’ll record what that PageField’s filter currently displays.
'#DEV# Maybe it's quick to iterate through the .VisibleItems collection (if there is one) and count?
sLayout_New = sLayout_New & .Name & "|" & .LabelRange.Offset(, 1).Value & "|" & .EnableMultiplePageItems & "||"
End Select
End With
Next pf
End Function
Function PivotChange_CompareLayout(pt As PivotTable, ByRef sLayout_New As String, ByRef sLayout_Old As String) As String
Dim i As Long
Dim lng As Long
Dim vLayout_Old As Variant
Dim vLayout_New As Variant
PivotChange_RecordLayout pt, sLayout_New
With pt
lng = InStr(.Summary, "[Layout]")
If lng > 0 Then
sLayout_Old = Mid(.Summary, lng + Len("[Layout]"), InStr(.Summary, "[/Layout]") - Len("[Layout]") - lng)
If sLayout_Old <> sLayout_New Then
vLayout_Old = Split(sLayout_Old, "||")
vLayout_New = Split(sLayout_New, "||")
For i = 0 To UBound(vLayout_Old)
If vLayout_Old(i) <> vLayout_New(i) Then
PivotChange_CompareLayout = Split(vLayout_Old(i), "|")(0)
Exit For
End If
Next i
End If
Else:
'Layout has not yet been recorded.
'Note that we only update .Summary at the end of the main function,
' so we don't wipe the UNDO stack before the PivotChange_UndoCheck routine
End If
End With
End Function
Function PivotChange_EliminationCheck(pt As PivotTable, ByRef sPossibles As String) As String
'Check all the visible fields to see if *just one of them alone* has
' neither .AllItemsVisible = True nor .EnableMultiplePageItems = false.
' If that's the case, then by process of elimination, this field
' must be the one that triggered the change, as changes to any of the
' others would have been identified in the code earlier.
Dim pf As PivotField
Dim lngFields As Long
lngFields = 0
On Error Resume Next ' Need this to handle DataFields and 'Values' field
For Each pf In pt.PivotFields
With pf
If .Orientation > 0 Then 'It's not hidden or a DataField
If .EnableMultiplePageItems And Not .AllItemsVisible Then
If Err.Number = 0 Then
'It *might* be this field
lngFields = lngFields + 1
sPossibles = sPossibles & .Name & ";"
Else: Err.Clear
End If
End If
End If
End With
Next
On Error GoTo 0
If lngFields = 1 Then PivotChange_EliminationCheck = Left(sPossibles, Len(sPossibles) - 1)
End Function
Function PivotChange_UndoCheck(pt As PivotTable, sPossibles) As String
Dim i As Long
Dim dicFields As Object 'This holds a list of all visible pivotfields
Dim dicVisible As Object 'This contains a list of all visible PivotItems for a pf
Dim varKey As Variant
Dim pf As PivotField
Dim pi As PivotItem
Dim bidentified As Boolean
Dim lngVisibleItems As Long
Application.EnableEvents = False
'Create master dictionary
Set dicFields = CreateObject("Scripting.Dictionary")
'Cycle through all pivotfields, excluding totals
For i = 0 To UBound(Split(sPossibles, ";")) - 1
'Create dicVisible: a dictionary for each visible PivotField that contain visible PivotItems
Set dicVisible = CreateObject("Scripting.Dictionary")
Set pf = pt.PivotFields(Split(sPossibles, ";")(i))
With pf
If .Orientation <> xlPageField Then
For Each pi In .VisibleItems
With pi
dicVisible.Add .Name, .Name
End With
Next pi
Else:
'Unfortunately the .visibleitems collection isn't available for PageFields
' e.g. SomePageField.VisibleItems.Count always returns 1
' So we'll have to iterate through the pagefield and test the .visible status
' so we can then record just the visible items (which is quite slow)
For Each pi In .PivotItems
With pi
If .Visible Then
dicVisible.Add .Name, .Name
End If
End With
Next pi
End If 'If .Orientation = xlPageField Then
'Write dicVisible to the dicFields master dictionary
dicFields.Add .Name, dicVisible
End With
Next i
Application.Undo
For Each varKey In dicFields.keys
Set pf = pt.PivotFields(varKey)
Set dicVisible = dicFields.Item(varKey)
'Test whether any of the items that were previously hidden are now visible
If pf.Orientation <> xlPageField Then
For Each pi In pf.VisibleItems
With pi
If Not dicVisible.exists(.Name) Then
PivotChange_UndoCheck = pf.Name
bidentified = True
Exit For
End If
End With
Next
Else 'pf.Orientation = xlPageField
lngVisibleItems = dicVisible.Count
i = 0
For Each pi In pf.PivotItems
With pi
If .Visible Then
If Not dicVisible.exists(.Name) Then
PivotChange_UndoCheck = pf.Name
bidentified = True
Exit For
Else: i = i + 1 'this is explained below.
End If
End If
End With
Next
' For non-PageFields, we know that the number of .VisibleItems hasn't changed.
' But we *don't* know that about Pagefields, and an increase in the amount of
' .VisibleItems won't be picked up by our Dictionary approach.
' So we'll check if the overall number of visible items changed
If Not bidentified And i > lngVisibleItems Then
PivotChange_UndoCheck = pf.Name
Exit For
End If
End If
If bidentified Then Exit For
Next
'Resore the original settings
With Application
.CommandBars(14).FindControl(ID:=129).Execute 'Standard Commandbar, Redo command
.EnableEvents = True
End With
End Function
End Sub

Resources