Solved

Excel Pivot Table Filters using VBA

Posted on 2015-02-19
7
498 Views
Last Modified: 2016-02-10
My data will be updated everyday and when the pivot table is refreshed I want to set the Filter "AreaDescription" to a specific value and I want to set the row "Week" to a range of values.  These values will all be based on values in the cells on a different sheet in the workbook.  Below is the code I created, but it is getting an error on both the filter commands.

Sub PivotDefault()
    Dim PvtTbl As PivotTable
    Dim WeekStart As Integer
    Dim WeekEnd As Integer
    Dim Area As String
    
    Set PvtTbl = Worksheets("PivotTable").PivotTables("PivotByArea")
    
    PvtTbl.ClearAllFilters
    
    WeekStart = Worksheets("Misc").Range("D2")
    WeekEnd = Worksheets("Misc").Range("D3")
    Area = Worksheets("Misc").Range("G1")
    
    PvtTbl.PivotFields("AreaDescription").PivotFilters.Add Type:=xlCaptionEquals, _
        Value1:=Area
    
    PvtTbl.PivotFields("Week").PivotFilters.Add Type:=xlValueIsBetween, _
        Value1:=WeekStart, Value2:=WeekEnd
End Sub

Open in new window


Thank you in Advanced!
0
Comment
Question by:jmkbrown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 

Author Comment

by:jmkbrown
ID: 40619564
I changed the code and now I am not getting any errors, but when the code applies the week filter I don't get any records to appear.  When I manually apply the filter it works just fine.  Here is the new code.

Sub PivotDefault()
    Dim PvtTbl As PivotTable
    Dim WeekStart As String
    Dim WeekEnd As String
    Dim Area As String
    
    Set PvtTbl = Worksheets("PivotTable").PivotTables("PivotByArea")
    
    PvtTbl.ClearAllFilters
    
    WeekStart = Worksheets("Misc").Range("E2")
    WeekEnd = Worksheets("Misc").Range("E3")
    Area = Worksheets("Misc").Range("G1")
    
    Sheets("PivotTable").Activate
    
    ActiveSheet.PivotTables("PivotByArea").PivotFields("Week").PivotFilters.Add _
        Type:=xlCaptionIsBetween, Value1:=WeekStart, Value2:=WeekEnd
        
    ActiveSheet.PivotTables("PivotByArea").PivotFields("AreaDescription"). _
        CurrentPage = Area
    
End Sub

Open in new window

0
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40620580
Could you send a dummy?
0
 
LVL 59

Expert Comment

by:Saurabh Singh Teotia
ID: 40621829
I'm Assuming your week start and week end dates are date.. and now when you are applying filter on those dates..it need to be in the same format and thats probably it's not pulling you the data when you apply the filter through macro..

Check the dates format and convert that week start and week end in the same format..

Assuming your dates are in YYYY-MM-DD format so it will look something like this...

WeekStart = format(Worksheets("Misc").Range("D2").value,"YYYY-MM-DD")
    WeekEnd = FORMAT(Worksheets("Misc").Range("D3").VALUE, "YYYY-MM-DD") 

Open in new window

0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:jmkbrown
ID: 40626197
Attached is a sample of the spreadsheet.
Sample.xlsm
0
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 500 total points
ID: 40626756
Their you go..try this..This will do what you are looking for...

Saurabh...
Sample.xlsm
0
 

Author Comment

by:jmkbrown
ID: 40629145
This worked perfectly!  Thank you very much!
0
 

Author Closing Comment

by:jmkbrown
ID: 40629148
It worked!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question