Solved

Excel Pivot Table Filters using VBA

Posted on 2015-02-19
7
291 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 48

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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 …

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now