Consulting

Results 1 to 3 of 3

Thread: VBA PivotTables "Unable to get the PivotItems of the PivotField Class"

  1. #1
    VBAX Newbie
    Joined
    Feb 2015
    Posts
    2
    Location

    VBA PivotTables "Unable to get the PivotItems of the PivotField Class"

    Hey Guys,

    I've been trying to solve this for most of the day and is really annoying. Please help!

    I've been writing a macro to select an additional date in a date filter of a pivot table. I recorded the macro, as such:

    Sub Macro4()
    '
    ' Macro4 Macro
    '
    '
        ActiveSheet.PivotTables("PivotTable2").PivotFields("Order_Date").CurrentPage = _
            "(All)"
        With ActiveSheet.PivotTables("PivotTable2").PivotFields("Order_Date")
            .PivotItems("5/02/2015").Visible = True
        End With
    End Sub
    However, even when I try running that code I get an error. how is that even possible? Recorded macro not performing what it did....

    Any help would be much appreciated. Thanks!
    Last edited by Bob Phillips; 02-05-2015 at 01:33 AM. Reason: Added code tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It is VBA's problem with dates, it is US centric, so records US centric but yours dates in Excel are probably not (which is why it records okay, but then doesn't run).

    What you need to do is force a date formatted as you have in the pivot field. In the example I show below, my date is 2nd May, and my format is d-mmm, you will need to change as per your values

        With ActiveSheet.PivotTables("PivotTable2")
        
            With .PivotFields("Order_Date")
            
                .PivotItems(Format(DateSerial(2015, 5, 2), "d-mmm")).Visible = True
            End With
        End With

    I did try to pick up the field's Numberformat property, there is one, but it seems to be indeterminate.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Feb 2015
    Posts
    2
    Location
    Quote Originally Posted by xld View Post
    It is VBA's problem with dates, it is US centric, so records US centric but yours dates in Excel are probably not (which is why it records okay, but then doesn't run).

    What you need to do is force a date formatted as you have in the pivot field. In the example I show below, my date is 2nd May, and my format is d-mmm, you will need to change as per your values

        With ActiveSheet.PivotTables("PivotTable2")
        
            With .PivotFields("Order_Date")
            
                .PivotItems(Format(DateSerial(2015, 5, 2), "d-mmm")).Visible = True
            End With
        End With

    I did try to pick up the field's Numberformat property, there is one, but it seems to be indeterminate.
    I ran your code (modified date format to "dd/mm/yyyy" and it gives the same error. Not sure why it is playing up.

Posting Permissions

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