How to deselect a pivot item name which has < > for dates

anabuan

New Member
Joined
Aug 7, 2014
Messages
13
I am trying to create a pivot table that would show all the months in a year even if it doesn't have any data then later on this will be a basis for my charts. So I went I click on the "show items with no data" in field settings layout & print. However, excel automatically shows less than a specific date and greater than a specific date. e.g. <30/06/2013 and >23/06/2014. I was trying to get rid of those by naming a specific cell that contains <30/06/2013 as min and >23/06/2014 as max. Then I am trying to create a vba that would automatically de-select those by looking into those named cell. However, there is always an error.

I am just a beginner in vba so I would be grateful if you could help me. Thanks.

Below is my code;

Dim min As String
Dim max As String
Dim pvtItem As PivotItem


min = Range("min").Value
max = Range("max").Value


With ActiveSheet.PivotTables("PivotTable7").PivotFields("Years")
.ShowAllItems = True
For Each pvtItem In .PivotItems

pvtItem.Visible = pvtItem.Name = "min"
pvtItem.Visible = (pvtItem.Name = max)

Next
End With
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
It sounds like you are Grouping that field by Years, and you want to hide the first and last groups that start with the character ">" or "< ".
Is that correct?

You can filter for those items without the need for named ranges by stepping through the PivotItems and looking at the first character.

Code:
Sub HideItems()
'---Sets manual PT filters to show all pivotitems in field except
'     those starting with "<" or ">"
 Dim i As Long
 Dim sFirst As String
    
 With ActiveSheet.PivotTables("PivotTable7").PivotFields("Years")
   For i = 1 To .PivotItems.Count
      sFirst = Left(.PivotItems(i), 1)
      If IsError(.PivotItems(i).Visible) Then
         .PivotItems(i).Visible = False
      Else
         If .PivotItems(i).Visible = (InStr(1, "<>", sFirst) > 0) Then
            .PivotItems(i).Visible = Not (.PivotItems(i).Visible)
         End If
      End If
   Next i
 End With
End Sub
 
Upvote 0
Thanks, Jerry. It works! But if I use it in different PivotTable and just change the name of Pivot Table unable to set the visible property of the pivotitem class error occurs...
 
Last edited:
Upvote 0
Error says unable to set the visible property of the pivot item class it it stops in

.PivotItems(i).Visible = Not (.PivotItems(i).Visible)
 
Last edited:
Upvote 0
When the debugger stops, paste this expression in the Immediate Window of the VBE:

?ActiveSheet.PivotTables("PivotTable7").PivotFields("Years").PivotItems(i).Visible

Modify PivotTable7 to match the name your PivotTable
Hit enter and post what value come back (or what error).

Do the same with:
?i
 
Upvote 0
Sorry, but I've got to wrap up for the night. I'll be glad to help more tomorrow (or perhaps someone else is online who will join the discussion).
 
Upvote 0
When I copy your code to immediate window and hit enter error is "Runtime error 1004 Unable to get the PivotTables property of the worksheet class". No worries its understandable that you have to sleep as its already late.. it maybe a long day for you :)

Thanks for helping people like me...
 
Last edited:
Upvote 0
Try this modified version. It uses For Each instead of the Index numbers of the PivotItems.

Code:
Sub HideItems()
'---Sets manual PT filters to show all pivotitems in field except
'     those starting with "<" or ">"
 Dim i As Long
 Dim pvi As PivotItem
 Dim sFirst As String
 
 With ActiveSheet.PivotTables("PivotTable7").PivotFields("Years")
   For Each pvi In .PivotItems
      Debug.Print pvi.Caption & " Records: " & pvi.RecordCount
      sFirst = Left(pvi, 1)
      If IsError(pvi.Visible) Then
         pvi.Visible = False
      Else
         If pvi.Visible = (InStr(1, "<>", sFirst) > 0) Then
            pvi.Visible = Not (pvi.Visible)
         End If
      End If
   Next pvi
 End With
End Sub

The Debug.Print statement will print some info to the Immediate Window that could be helpful. If the code above errors, please post the info sent to the Immediate Window.
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,947
Members
449,198
Latest member
MhammadishaqKhan

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