Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel Pivot Table Filters using VBA

Posted on 2015-02-19
7
Medium Priority
?
738 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 53

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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Technology Partners: 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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

610 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