Date PivotItem Visible Property throws error

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,912
Office Version
  1. 365
Platform
  1. Windows
Hi All

This one has me stumped. The code below works for all pivot fields that are NOT date fields. As soon as I loop through the PivotItems in the date fields I get a Type Mismatch error on the highlighted line. This error will occur for any date item so fails on the first iteration.

Code:
Private Sub lbxChosenFields_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Dim lngPosition As Long
    Dim rngField As Range
    Dim pvi As PivotItem
    Dim vararrHiddenValues As Variant
    Dim lngListItem As Long
    
    With Me.lbxChosenFields
        If .ListIndex = -1 Then Exit Sub
        mstrFIELDNAME = .List(.ListIndex)
    End With
    
    With mpvtREPORT.PivotFields(mstrFIELDNAME)
        lngPosition = .Position
        .Position = 1
        For Each pvi In .PivotItems
            [B][COLOR="Red"]If pvi.Visible = False Then[/COLOR][/B]
                If IsEmpty(vararrHiddenValues) Then
                    ReDim vararrHiddenValues(1)
                    vararrHiddenValues(1) = pvi.Value
                Else
                    ReDim Preserve vararrHiddenValues(UBound(vararrHiddenValues) + 1)
                    vararrHiddenValues(UBound(vararrHiddenValues)) = pvi.Value
                End If
                pvi.Visible = True
            End If
        Next pvi
    End With
    'more code here
End Sub

What's confusing is that the property clearly states True when I look in the Locals window.

Further, in debug mode and in the immediate window the following returns Error 2042, but does not throw Type Mismatch:

Code:
?mpvtreport.pivotfields("date").pivotitems(1).visible

Richard Schollar has already pointed out that I can just format the field as a double and then process that, but I would really like to know the cause of this issue because changing to double means I have to adapt alot of other code too.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I think it's a bug - I've seen it reported in a few posts now (though I can't recreate it)
Like the new title by the way!
 
Last edited:
Upvote 0
Thanks Rory. So it'll have to be the long complicated horrible fix then. :(

I think Richard's title should be changed to 'Can't do it myself so I hired someone better than me'. :biggrin:
 
Upvote 0
I don't. You gave it to me, so, in the immortal words of Bros, I owe you nothing (mmm nothin', nothin' at aaaallll.)
 
Upvote 0
Given your avatar, there must be something appropriate in 'Dark Side of the Moon'...
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top