Excel VBA Pivot Table Filter












0















I am trying to do a VBA code for Microsoft Excel to filter our a few items out of many (e.g. 3 items out of 100) from a pivot table. So far the method I've been using is by declaring items I do not want to be false such as:



.PivotItems("Item A").Visible = True
.PivotItems("Item B").Visible = True
.PivotItems("Item C").Visible = False
.PivotItems("Item D").Visible = False
.PivotItems("Item E").Visible = False


However this gets tedious in the future as there maybe more items added to the table and which means I would need to edit the codes every time there is a new item added.



Is there a way to filter out two or more items without using



.Visible=false 


such as



pf.CurrentPage = "Item A" + "Item B" 









share|improve this question



























    0















    I am trying to do a VBA code for Microsoft Excel to filter our a few items out of many (e.g. 3 items out of 100) from a pivot table. So far the method I've been using is by declaring items I do not want to be false such as:



    .PivotItems("Item A").Visible = True
    .PivotItems("Item B").Visible = True
    .PivotItems("Item C").Visible = False
    .PivotItems("Item D").Visible = False
    .PivotItems("Item E").Visible = False


    However this gets tedious in the future as there maybe more items added to the table and which means I would need to edit the codes every time there is a new item added.



    Is there a way to filter out two or more items without using



    .Visible=false 


    such as



    pf.CurrentPage = "Item A" + "Item B" 









    share|improve this question

























      0












      0








      0








      I am trying to do a VBA code for Microsoft Excel to filter our a few items out of many (e.g. 3 items out of 100) from a pivot table. So far the method I've been using is by declaring items I do not want to be false such as:



      .PivotItems("Item A").Visible = True
      .PivotItems("Item B").Visible = True
      .PivotItems("Item C").Visible = False
      .PivotItems("Item D").Visible = False
      .PivotItems("Item E").Visible = False


      However this gets tedious in the future as there maybe more items added to the table and which means I would need to edit the codes every time there is a new item added.



      Is there a way to filter out two or more items without using



      .Visible=false 


      such as



      pf.CurrentPage = "Item A" + "Item B" 









      share|improve this question














      I am trying to do a VBA code for Microsoft Excel to filter our a few items out of many (e.g. 3 items out of 100) from a pivot table. So far the method I've been using is by declaring items I do not want to be false such as:



      .PivotItems("Item A").Visible = True
      .PivotItems("Item B").Visible = True
      .PivotItems("Item C").Visible = False
      .PivotItems("Item D").Visible = False
      .PivotItems("Item E").Visible = False


      However this gets tedious in the future as there maybe more items added to the table and which means I would need to edit the codes every time there is a new item added.



      Is there a way to filter out two or more items without using



      .Visible=false 


      such as



      pf.CurrentPage = "Item A" + "Item B" 






      excel vba excel-vba






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 2:56









      KeithKeith

      32




      32
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You can use an array of items:



          Dim arr() As Variant

          ' Array of items to be kept visible (or hidden depending on your code)
          arr = Array("Item 1", "Item 2", "Item 2")

          For Each itm In pt.PivotFields("Value").PivotItems
          If Not IsError(Application.Match(itm.Caption, arr, 0)) Then
          itm.Visible = True
          Else
          itm.Visible = False
          End If
          Next





          share|improve this answer
























          • This is exactly what I was looking for! Thanks a bunch!

            – Keith
            Nov 15 '18 at 9:53











          • Welcome and please mark the question as answered.

            – Michal Rosa
            Nov 15 '18 at 10:13



















          0














          You'll have to adapt pivot table and field names but this works on my test:



          Sub filter_pivot()
          Dim i As Integer
          ActiveSheet.PivotTables("PivotTable1").ClearAllFilters
          With ActiveSheet.PivotTables("PivotTable1").PivotFields("CLCL")
          For i = 1 To .PivotItems.Count
          Debug.Print .PivotItems(i)
          Select Case .PivotItems(i).Name
          Case Is = "Item C"
          .PivotItems(i).Visible = False
          Case Is = "Item D"
          .PivotItems(i).Visible = False
          Case Is = "Item E"
          .PivotItems(i).Visible = False
          End Select
          Next i
          End With
          End Sub


          We're still using .Visible but it does not matter how many new items you add.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311772%2fexcel-vba-pivot-table-filter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You can use an array of items:



            Dim arr() As Variant

            ' Array of items to be kept visible (or hidden depending on your code)
            arr = Array("Item 1", "Item 2", "Item 2")

            For Each itm In pt.PivotFields("Value").PivotItems
            If Not IsError(Application.Match(itm.Caption, arr, 0)) Then
            itm.Visible = True
            Else
            itm.Visible = False
            End If
            Next





            share|improve this answer
























            • This is exactly what I was looking for! Thanks a bunch!

              – Keith
              Nov 15 '18 at 9:53











            • Welcome and please mark the question as answered.

              – Michal Rosa
              Nov 15 '18 at 10:13
















            0














            You can use an array of items:



            Dim arr() As Variant

            ' Array of items to be kept visible (or hidden depending on your code)
            arr = Array("Item 1", "Item 2", "Item 2")

            For Each itm In pt.PivotFields("Value").PivotItems
            If Not IsError(Application.Match(itm.Caption, arr, 0)) Then
            itm.Visible = True
            Else
            itm.Visible = False
            End If
            Next





            share|improve this answer
























            • This is exactly what I was looking for! Thanks a bunch!

              – Keith
              Nov 15 '18 at 9:53











            • Welcome and please mark the question as answered.

              – Michal Rosa
              Nov 15 '18 at 10:13














            0












            0








            0







            You can use an array of items:



            Dim arr() As Variant

            ' Array of items to be kept visible (or hidden depending on your code)
            arr = Array("Item 1", "Item 2", "Item 2")

            For Each itm In pt.PivotFields("Value").PivotItems
            If Not IsError(Application.Match(itm.Caption, arr, 0)) Then
            itm.Visible = True
            Else
            itm.Visible = False
            End If
            Next





            share|improve this answer













            You can use an array of items:



            Dim arr() As Variant

            ' Array of items to be kept visible (or hidden depending on your code)
            arr = Array("Item 1", "Item 2", "Item 2")

            For Each itm In pt.PivotFields("Value").PivotItems
            If Not IsError(Application.Match(itm.Caption, arr, 0)) Then
            itm.Visible = True
            Else
            itm.Visible = False
            End If
            Next






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 3:40









            Michal RosaMichal Rosa

            1,3191814




            1,3191814













            • This is exactly what I was looking for! Thanks a bunch!

              – Keith
              Nov 15 '18 at 9:53











            • Welcome and please mark the question as answered.

              – Michal Rosa
              Nov 15 '18 at 10:13



















            • This is exactly what I was looking for! Thanks a bunch!

              – Keith
              Nov 15 '18 at 9:53











            • Welcome and please mark the question as answered.

              – Michal Rosa
              Nov 15 '18 at 10:13

















            This is exactly what I was looking for! Thanks a bunch!

            – Keith
            Nov 15 '18 at 9:53





            This is exactly what I was looking for! Thanks a bunch!

            – Keith
            Nov 15 '18 at 9:53













            Welcome and please mark the question as answered.

            – Michal Rosa
            Nov 15 '18 at 10:13





            Welcome and please mark the question as answered.

            – Michal Rosa
            Nov 15 '18 at 10:13













            0














            You'll have to adapt pivot table and field names but this works on my test:



            Sub filter_pivot()
            Dim i As Integer
            ActiveSheet.PivotTables("PivotTable1").ClearAllFilters
            With ActiveSheet.PivotTables("PivotTable1").PivotFields("CLCL")
            For i = 1 To .PivotItems.Count
            Debug.Print .PivotItems(i)
            Select Case .PivotItems(i).Name
            Case Is = "Item C"
            .PivotItems(i).Visible = False
            Case Is = "Item D"
            .PivotItems(i).Visible = False
            Case Is = "Item E"
            .PivotItems(i).Visible = False
            End Select
            Next i
            End With
            End Sub


            We're still using .Visible but it does not matter how many new items you add.






            share|improve this answer






























              0














              You'll have to adapt pivot table and field names but this works on my test:



              Sub filter_pivot()
              Dim i As Integer
              ActiveSheet.PivotTables("PivotTable1").ClearAllFilters
              With ActiveSheet.PivotTables("PivotTable1").PivotFields("CLCL")
              For i = 1 To .PivotItems.Count
              Debug.Print .PivotItems(i)
              Select Case .PivotItems(i).Name
              Case Is = "Item C"
              .PivotItems(i).Visible = False
              Case Is = "Item D"
              .PivotItems(i).Visible = False
              Case Is = "Item E"
              .PivotItems(i).Visible = False
              End Select
              Next i
              End With
              End Sub


              We're still using .Visible but it does not matter how many new items you add.






              share|improve this answer




























                0












                0








                0







                You'll have to adapt pivot table and field names but this works on my test:



                Sub filter_pivot()
                Dim i As Integer
                ActiveSheet.PivotTables("PivotTable1").ClearAllFilters
                With ActiveSheet.PivotTables("PivotTable1").PivotFields("CLCL")
                For i = 1 To .PivotItems.Count
                Debug.Print .PivotItems(i)
                Select Case .PivotItems(i).Name
                Case Is = "Item C"
                .PivotItems(i).Visible = False
                Case Is = "Item D"
                .PivotItems(i).Visible = False
                Case Is = "Item E"
                .PivotItems(i).Visible = False
                End Select
                Next i
                End With
                End Sub


                We're still using .Visible but it does not matter how many new items you add.






                share|improve this answer















                You'll have to adapt pivot table and field names but this works on my test:



                Sub filter_pivot()
                Dim i As Integer
                ActiveSheet.PivotTables("PivotTable1").ClearAllFilters
                With ActiveSheet.PivotTables("PivotTable1").PivotFields("CLCL")
                For i = 1 To .PivotItems.Count
                Debug.Print .PivotItems(i)
                Select Case .PivotItems(i).Name
                Case Is = "Item C"
                .PivotItems(i).Visible = False
                Case Is = "Item D"
                .PivotItems(i).Visible = False
                Case Is = "Item E"
                .PivotItems(i).Visible = False
                End Select
                Next i
                End With
                End Sub


                We're still using .Visible but it does not matter how many new items you add.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 3:36

























                answered Nov 15 '18 at 3:27









                Display nameDisplay name

                54416




                54416






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311772%2fexcel-vba-pivot-table-filter%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Xamarin.form Move up view when keyboard appear

                    JBPM : POST request for execute process go wrong