Macro to select multiple items in a pivot table

muhleebbin

Active Member
Joined
Sep 30, 2017
Messages
252
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Can someone help with the following code?

At this moment in time I don't have anything that fits var1 and var3 but they will eventually come up. Is there a way to still have this code work but then have var1 and var3 not effect the pivot until there is data for that?

Also is there a way where A1 in sheet2 doesn't need to hit enter for it to take effect? A1 is currently a data validation of the dates available

Thanks for your help!

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 
'This line stops the worksheet updating on every change, it only updates when cell
'A1 is touched
If Intersect(Target, Range("A1:A2")) Is Nothing Then Exit Sub
 
Dim var1 As String, var2 As String
var1 = Worksheets("Sheet1").Range("S1")
var2 = "THREE DAY PASS"
    With Worksheets("Sheet1").PivotTables("PivotTable19").PivotFields("Day(s)") '.PivotFilters
        .ClearAllFilters
        For Each Pi In .PivotItems
            Select Case Pi.Name
                Case var1, var2
                    Pi.Visible = True
                Case Else
                    Pi.Visible = False
            End Select
        Next
    End With
    
Dim var3 As String
var3 = Worksheets("Sheet1").Range("S2")
    With Worksheets("Sheet1").PivotTables("PivotTable20").PivotFields("Allocated Day") '.PivotFilters
        .ClearAllFilters
        For Each Pi In .PivotItems
            Select Case Pi.Name
                Case var3
                    Pi.Visible = True
                Case Else
                    Pi.Visible = False
            End Select
        Next
    End With
 
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Re: Help with a macro to select multiple items in a pivot table

In the pivot table at least one record you must select.
The following checks if there is no selection, then sends you a msg.


Code:
    Dim var1 As String, var2 As String
    Dim n As Long
    
    var1 = Worksheets("Sheet1").Range("S1")
    var2 = "THREE DAY PASS"
    n = 0
    With Worksheets("Sheet1").PivotTables("PivotTable19").PivotFields("Day(s)") '.PivotFilters
        .ClearAllFilters
        For Each Pi In .PivotItems
            Select Case Pi.Name
                Case var1, var2
                    Pi.Visible = True
                Case Else
                    n = n + 1
                    If n < .PivotItems.Count Then
                        Pi.Visible = False
                    Else
                        MsgBox "No match"
                        .ClearAllFilters
                    End If
            End Select
        Next
    End With
 
Upvote 0
Re: Help with a macro to select multiple items in a pivot table

Hey Dante

Thanks for the input, I guess I should clarify what this is going to be used for and why I asked for the method where var1 and var3 could be temporarily unavailable.

This pivot selection helps populate attendees for an event. At the moment only "Three Day Passes" have been issued but eventually Friday, Saturday and Sunday only passes will be issued. Var1 will be those options eventually. On sheet2 cell a1 is a data validation to select which report to view: "Friday, Saturday or Sunday" attendees.

I'm hoping to be able to still select from that cell which report to view while there may not be data yet for a particular day. Right now if there's no data for let's say Sunday, selecting Sunday doesn't update the pivot properly.

Hope that makes more sense.
 
Upvote 0
Re: Help with a macro to select multiple items in a pivot table

The macro works for me, if var1 or var2 have data, perform the appropriate filter, check the data of var1 matches in letters and spaces with the elements of the table, if it is a number, check that the elements are also numbers, if it is a date, check that the elements are also date and have the same date format.
It is complicated to work with numbers and dates, perform a simple test with a letter, put in var1 = "a", in your data put some letters "a". Do the test
 
Upvote 0
Re: Help with a macro to select multiple items in a pivot table

I'm not having an issue with matching data type (mine are just words ie THREE DAY PASS, Friday Only etc.). It's more if there is no data for a specific date i.e. Sunday only. When I go to select the Friday Report but then select Sunday, if there's no Sunday Only passes then the report doesn't fill properly. It keeps the previous day's filter, in this case Friday.

I'm trying to make it where var1 (date specific filter) isn't necessary if there's no data for it.
 
Last edited:
Upvote 0
Re: Help with a macro to select multiple items in a pivot table

If there is no data to filter then it does not filter anything.
 
Upvote 0
Re: Help with a macro to select multiple items in a pivot table

There will eventually be data for that as stated before....."At the moment only "Three Day Passes" have been issued but eventually Friday, Saturday and Sunday only passes will be issued"

Which is why i'm looking for a solution to this
 
Upvote 0
Re: Help with a macro to select multiple items in a pivot table

Did you do the simple test?
 
Upvote 0
Re: Help with a macro to select multiple items in a pivot table

Simple test? Sorry not sure what you're referring to there

Testing i've done so far includes all 4 entries which runs normally. However if I only include 3 entries: "Three Day Pass, Friday Only, Saturday Only" to see what happens when I select Sunday. In that instance, Sunday brings up the previous day selected report. Even further testing would be switching Friday Only to Sunday Only (let's say someone made an input error) that runs into a debug issue where it's looking for var1.

Basically i'm trying to dummy proof this report so that it can pull the correct data set for an attendee report for any given day.
 
Last edited:
Upvote 0
Re: Help with a macro to select multiple items in a pivot table

Actually I think I know where to go with this....

I'll test when I get back to the office
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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