+ Reply to Thread
Results 1 to 4 of 4

Select Max Value in a Pivot Field

  1. #1
    Registered User
    Join Date
    01-17-2013
    Location
    Austin, TX
    MS-Off Ver
    Excel 2007
    Posts
    84

    Select Max Value in a Pivot Field

    I’m trying to write a macro that will select the largest value in the PivotItem field for “Workweek”. However, I get this error message with the Auto_Update macro.

    Run-time error ‘1004’:
    Unable to set the Visible property of the PivotItem class
    Thanks for the help. I attached the workbook to this post.
    Attached Files Attached Files

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Select Max Value in a Pivot Field

    Hi rbirch,

    I believe the error happens because you can't turn off the visible settings for ALL fields. You need to have one on at all times.

    I have a much easier way to do this problem, if I understand it correctly.

    First create a Dynamic Named Range of your data so it the range will expand when you add data to the table.
    Then change your Pivot Data Source to the name of the DNR.

    Now Create a Pivot table when you filter on the Maximum Value in Revenue. This means it will only show the maximum revenue row.

    Finally create an Event Macro that will Refresh all Pivots when something changes in the Revenue column.

    I believe this is what you are trying to accomplish. See the attached where I've done this.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    01-17-2013
    Location
    Austin, TX
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Select Max Value in a Pivot Field

    Wow, that is a good idea. I "fixed" it with On Error Resume Next

  4. #4
    Registered User
    Join Date
    01-17-2013
    Location
    Austin, TX
    MS-Off Ver
    Excel 2007
    Posts
    84

    Re: Select Max Value in a Pivot Field

    Sub Auto_Update()

    Dim wk1 As Worksheet
    Set wk1 = ActiveWorkbook.Worksheets("Records")

    Dim piItem As PivotItem
    Dim lngMaxValue As Double
    Set piItem = Nothing

    wk1.PivotTables("PivotTable1").ClearTable

    With wk1.PivotTables("PivotTable1").PivotFields("Workweek")
    .Orientation = xlPageField
    .Position = 1
    End With

    For Each piItem In wk1.PivotTables(1).PivotFields("Workweek").PivotItems
    On Error Resume Next
    If piItem.Value > lngMaxValue Then lngMaxValue = piItem.Value
    With wk1.PivotTables("PivotTable1").PivotFields("Workweek") 'added with statement in the loop
    .PivotItems(lngMaxValue).Visible = False
    .EnableMultiplePageItems = True
    End With
    Next piItem

    With wk1.PivotTables("PivotTable1").PivotFields("Workweek") 'added with
    .Orientation = xlPageField
    .Position = 1
    .PivotItems(lngMaxValue).Visible = True
    .EnableMultiplePageItems = True
    End With


    Debug.Print "Max value is " & lngMaxValue


    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Select Max Value in a Pivot Field
    By rbirch in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-08-2015, 04:50 PM
  2. Select Pivot Field Item
    By ShareTheGlobe in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-30-2013, 08:02 AM
  3. Select Multiple Pivot Field Items in VBA
    By JungleJme in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-07-2013, 01:36 PM
  4. Pivot Field Select
    By adgors in forum Excel General
    Replies: 5
    Last Post: 11-24-2010, 05:04 AM
  5. Field Select in a Pivot Table
    By Vicki in forum Excel General
    Replies: 2
    Last Post: 05-12-2006, 03:35 PM

Tags for this Thread

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