Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9210139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:59:54+00:00 2026-06-18T00:59:54+00:00

I’m trying to select one report filter, in this case Canada. That means the

  • 0

I’m trying to select one report filter, in this case Canada. That means the rest must be made invisible.
This code works without issue:

Public Sub FilterPivotTable()

    With ActiveSheet.PivotTables("Epidemiology").PivotFields("COUNTRY")

        .PivotItems("Canada").Visible = True
        .PivotItems("USA").Visible = False
        .PivotItems("Germany").Visible = False
        .PivotItems("France").Visible = False

    End With

End Sub

However, I’m trying to prepare for when we add other countries to our “Epidemiology” pivot table, so I tried to have a for loop. This code doesn’t work:

With ActiveSheet.PivotTables("Epidemiology").PivotFields("COUNTRY")

    .PivotItems("Canada").Visible = True

    For Each Pi In .PivotItems
        If Pi.Value = "CANADA" Then
            Pi.Visible = True
        Else
            Pi.Visible = False
        End If
    Next Pi

End With

It gives me an error on the Pi.Visible = False line. The error that I get is Run-time error '1004': Unable to set the Visible property of the PivotItem class

Why doesn’t it work inside a for loop?!

Frustratingly, all the examples I find online use similar syntax. (Some use an index, but I tried that and got the same error.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:59:55+00:00Added an answer on June 18, 2026 at 12:59 am

    In a pivottable filter, you must have at least one item selected at all times. Even if you intend to select one later in the code.

    With Pt.PivotFields("COUNTRYSCENARIO")
    
     ' Sets all filters to true, resetting it.
     .ClearAllFilters
    
     ' This is necessary if you want to select any options
     ' other than "All Pivot Items = Visible" and 
     ' "OnlyOneSpecificPivotItem = Visible"
     .EnableMultiplePageItems = True
    
     If .PivotItems.Count > 0 Then
    
         ' goofy but necessary
         Set firstPi = .PivotItems(1)
    
             For Each Pi In .PivotItems                                
    
                 ' Make sure that that first pivot item is visible.
                 ' It gets mad if it's already visible and you
                 ' set it to visible with firstPi.Visible = True
                 ' ...pretty silly
    
                 If firstPi.Visible = False Then firstPi.Visible = True
    
                 ' Don't loop through firstPi
                 If Pi.Value <> firstPi.Value Then
    
                     If Pi.Value = opt1 Or Pi.Value = opt2 Or Pi.Value = opt3 Then
                         Pi.Visible = True
    
                     ElseIf Pi.Visible = True Then
    
                         Pi.Visible = False
    
                     End If
    
                 End If
    
              Next Pi
    
              ' Finally perform the check on the first pivot item   
              If firstPi = opt1 Or firstPi = opt2 Or firstPi = opt3 Then
                  firstPi.Visible = True
              Else
                  firstPi.Visible = False
              End If
           End If
    End With
    

    Notice that if you try to select nothing, e.g. opt 1 = “” and opt2 = “” and opt3 = “”, you will have that same error: you must have at least one pivot item selected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.