VBA to select Dates Pivot Table

DalPai

New Member
Joined
Aug 13, 2018
Messages
29
I have a PivotTable with values filtered by dates. Dates range from 01/01/2018 to 12/31/2019.


But I need to select only dates until the value of E4 cell (eg.: 03/29/2019).


It is not difficult to do manually, but I wanted to try using a macro to speed things up.


Recording the beginning of the macro, I got this:

Code:
Sub teste()
'
' teste Macro
'


'
    ActiveSheet.PivotTables("Tabela dinâmica5").PivotFields("Data Arrumada"). _
        CurrentPage = "(All)"
    With ActiveSheet.PivotTables("Tabela dinâmica5").PivotFields("Data Arrumada")
        .PivotItems("4/27/2018").Visible = False
        .PivotItems("6/29/2018").Visible = False
        .PivotItems("7/13/2018").Visible = False
        .PivotItems("7/27/2018").Visible = False
    End With
End Sub

But I don't know much about VBA, so I'm kind of lost.


Any help?

Best Regards
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try this


Code:
Sub Select_Dates()
    Dim pi As PivotItem
    Dim n As Long
    
    Application.ScreenUpdating = False
    n = 0
    With ActiveSheet.PivotTables("Tabela dinâmica5").PivotFields("Data Arrumada")
        .ClearAllFilters
        
        For Each pi In .PivotItems
            If CDate(pi) > Range("E4").Value Then
                n = n + 1
                If n < .PivotItems.Count Then
                    pi.Visible = False
                Else
                    MsgBox "No match"
                    .ClearAllFilters
                End If
            End If
        Next
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,224
Members
453,025
Latest member
Hannah_Pham93

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