Solved

Excel Pivot Table Filters using VBA

Posted on 2015-02-19
7
345 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
  • 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 50

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

809 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