Conditional Variable for Pivot Table Filter

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
Is it possible to make var1 in the following code conditional to its existence or not?

Basically i'm trying to use a pivot table to help produce a report on attendees for each day of our event. var2 will always be a selection but var1 will eventually have Friday Only, Saturday Only, Sunday Only passes, however those aren't issued yet.

When testing, the current code runs into issues if a day specific pass isn't issued yet so i'd like to make var1 conditional but can't seem to figure out how to do that.

Code:
    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
                        .ClearAllFilters
                    End If
            End Select
        Next
    End With
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Rule #13:
We prefer that members do not cross-post questions to other forums, but when this does occur members should do the following:
- Post the details of your question on our forum. Do NOT simply post a link/re-direct to the question in another forum with no details posted here.
- Make it clear that you have cross-posted and provide links to the cross-posts.
Cross-posted questions that do not comply may be deleted or locked. For a discussion on the issues with cross-posting, see this link: Excelguru Help Site - A message to forum cross posters

I sent you an example in the other thread, in addition to the explanation, it would help if you comment what data you have in S1, if it is empty and what data you have in column B and what you expect as a result.
Either way, I made an adjustment, if it helps.

Code:
Sub fil()


    Dim var1 As String, var2 As String
    Dim n As Long
    
    Application.ScreenUpdating = False
    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
            If var1 <> "" Then
                
                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
            Else
                Select Case Pi.Name
                    Case 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
            
            
            End If
        Next
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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