+ Reply to Thread
Results 1 to 4 of 4

Display only selected fields in a PivotTable with VBA

  1. #1
    S30 via OfficeKB.com
    Guest

    Display only selected fields in a PivotTable with VBA

    Hi all,

    I would like to display dates in a pivottable that match with dates on a list.


    I am one step away, but not sure where I made a mistake


    'count number of dates in a pivot talbe
    intCountRows1 = ActiveSheet.PivotTables("PivotTable3").PivotFields("Date").
    PivotItems.Count

    'count number of dates in a list
    intCountRows2 = Application.WorksheetFunction.CountA(Sheet1.Range("G:G"))

    'tick all dates to be displayed
    For i = 1 To intCountRows1
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("Date")
    .PivotItems(i).Visible = True
    End With
    Next

    'compare a list against the dates in a pivottable and display only those that
    match.
    a = 2

    Do While a <= intCountRows2

    For i = 1 To intCountRows1

    strField = Sheet1.Cells(a, 7).Value

    With ActiveSheet.PivotTables("PivotTable3").PivotFields("Date")

    If .PivotItems(i).Value = strField Then
    .PivotItems(i).Visible = True
    a = a + 1
    Else
    .PivotItems(i).Visible = False
    End If

    End With
    Next

    Loop

    End Sub

    Thanks very much for your help in advance

    Best regards

    Slav

    --
    Message posted via OfficeKB.com
    http://www.officekb.com/Uwe/Forums.a...mming/200605/1

  2. #2
    Tom Ogilvy
    Guest

    RE: Display only selected fields in a PivotTable with VBA


    for each pvtItm in
    ActiveSheet.PivotTables("PivotTable3").PivotFields("Date").PivotItems
    pvtItm.Visible = False
    for a = 1 to intCountRows2
    strField = Sheet1.Cells(a, 7).Value
    If pvtItm.Value = strField Then
    pvtItm.Visible = True
    exit for
    Next
    Next PvtItm

    --
    Regards,
    Tom Ogilvy


    "S30 via OfficeKB.com" wrote:

    > Hi all,
    >
    > I would like to display dates in a pivottable that match with dates on a list.
    >
    >
    > I am one step away, but not sure where I made a mistake
    >
    >
    > 'count number of dates in a pivot talbe
    > intCountRows1 = ActiveSheet.PivotTables("PivotTable3").PivotFields("Date").
    > PivotItems.Count
    >
    > 'count number of dates in a list
    > intCountRows2 = Application.WorksheetFunction.CountA(Sheet1.Range("G:G"))
    >
    > 'tick all dates to be displayed
    > For i = 1 To intCountRows1
    > With ActiveSheet.PivotTables("PivotTable3").PivotFields("Date")
    > .PivotItems(i).Visible = True
    > End With
    > Next
    >
    > 'compare a list against the dates in a pivottable and display only those that
    > match.
    > a = 2
    >
    > Do While a <= intCountRows2
    >
    > For i = 1 To intCountRows1
    >
    > strField = Sheet1.Cells(a, 7).Value
    >
    > With ActiveSheet.PivotTables("PivotTable3").PivotFields("Date")
    >
    > If .PivotItems(i).Value = strField Then
    > .PivotItems(i).Visible = True
    > a = a + 1
    > Else
    > .PivotItems(i).Visible = False
    > End If
    >
    > End With
    > Next
    >
    > Loop
    >
    > End Sub
    >
    > Thanks very much for your help in advance
    >
    > Best regards
    >
    > Slav
    >
    > --
    > Message posted via OfficeKB.com
    > http://www.officekb.com/Uwe/Forums.a...mming/200605/1
    >


  3. #3
    S30 via OfficeKB.com
    Guest

    RE: Display only selected fields in a PivotTable with VBA

    Tom Ogilvy wrote:
    >
    >for each pvtItm in
    >ActiveSheet.PivotTables("PivotTable3").PivotFields("Date").PivotItems
    > pvtItm.Visible = False
    > for a = 1 to intCountRows2
    > strField = Sheet1.Cells(a, 7).Value
    > If pvtItm.Value = strField Then
    > pvtItm.Visible = True
    > exit for
    > Next
    >Next PvtItm
    >
    >> Hi all,
    >>

    >[quoted text clipped - 47 lines]
    >>
    >>


    Thank you very much!!!

    --
    Message posted via http://www.officekb.com

  4. #4
    Forum Contributor
    Join Date
    01-18-2005
    Location
    Auckland New Zealand
    MS-Off Ver
    Office Professional 2007
    Posts
    295

    Re: Display only selected fields in a PivotTable with VBA

    Tom, even though this was posted 5 years ago, it's a fantastic help to my current project.
    Mike

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1