Results 1 to 4 of 4

Thread: Select items in Page fields in Pivot table using VBA
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Mar 2011
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Select items in Page fields in Pivot table using VBA

    Hello
    Using Excel 2010

    I have these two pieces of code below that put 2 fields into the page area of a pivot table.
    I want to choose 2 out of 40 items in OrderID
    I want to choose 1 out of 20 items in CustomerID

    Is this possible without having to write true vs false code for each item in VBA?

    Any alternative code greatly appreciated.

    'Insert Page Fields
    With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("OrderID")
    .Orientation = xlPageField
    .Position = 1
    End With

    With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("CustomerID")
    .Orientation = xlPageField
    .Position = 2
    End With

    Thank you
    Eric

  2. #2
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    33,480
    Post Thanks / Like
    Mentioned
    46 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Select items in Page fields in Pivot table using VBA

    For CustomerID you can just set the currentpage to the value you want. For the other one, you'll have to loop.

  3. #3
    New Member
    Join Date
    Mar 2011
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Select items in Page fields in Pivot table using VBA

    Quote Originally Posted by RoryA View Post
    For CustomerID you can just set the currentpage to the value you want. For the other one, you'll have to loop.
    Thank you.
    Could you provide me the actual VBA code how to select just one item and then also how to do a loop?

    Thank you
    Eric

  4. #4
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    33,480
    Post Thanks / Like
    Mentioned
    46 Post(s)
    Tagged
    5 Thread(s)

    Default Re: Select items in Page fields in Pivot table using VBA

    For a single value:

    Code:
    ActiveSheet.PivotTables("SalesPivotTable").PivotFields("CustomerID").currentpage = "some value"
    For the loop:

    Code:
    Dim pi as pivotitem
    with ActiveSheet.PivotTables("SalesPivotTable").PivotFields("OrderID")
    .clearallfilters
    for each pi in .pivotitems
    if pi.name = "item 1" or pi.name = "Item2" then
    pi.visible = true
    else
    pi.visible = false
    end if
    next pio
    end with

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •