Hi there
I am building on from this here
Attached is the file I am working off.
I have the below macro that basically makes all the dates invisible(i.e. it removes the dates from the pivot chart). I am assuming all the dates are visible.
Sub ShowAllItemsks1()
Application.ScreenUpdating = False '' this makes all the visible dates
'' invisible except for one and comes with an error
Dim ws As Worksheet
Dim objPT As PivotTable
Dim objPTField As PivotField
Dim objPTItem As PivotItem
Dim LastDate As Date
LastDate = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp)
MsgBox "Last Date is:" & LastDate & vbCrLf
For Each ws In Worksheets
ws.Visible = xlSheetVisible
For Each objPT In ws.PivotTables
MsgBox "Pivot table name: " & objPT.Name & vbCrLf & "Sheet name: " & ws.Name & vbCrLf & "test"
With objPT
'
' Run through all the row fields
For Each objPTField In .RowFields
'
' Get the hidden items and make them visible
For Each objPTItem In objPTField.VisibleItems
objPTItem.Visible = False
Next 'objPTItem
Next 'objPTField
End With
Application.ScreenUpdating = True
Next objPT
Next ws
End Sub
But I get the following error, which is as I understand it to be, because this is the last date that is visible thereby it is not allowed to be made invisible.
Run time error’1004’: Unable to set the Visible property of the PivotItem class
Which points to this bit of code for debug
objPTItem.Visible = False
Now what I want to do is make only one date disappear by changing the above code to
objPTItem(LastDate).Visible = False
which gives the following
Sub ShowAllItemsks3()
Application.ScreenUpdating = False '' this makes all the visible dates
'' invisible except for one and comes with an error
Dim ws As Worksheet
Dim objPT As PivotTable
Dim objPTField As PivotField
Dim objPTItem As PivotItem
Dim LastDate As Date
LastDate = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp)
MsgBox "Last Date is:" & LastDate & vbCrLf
For Each ws In Worksheets
ws.Visible = xlSheetVisible
For Each objPT In ws.PivotTables
MsgBox "Pivot table name: " & objPT.Name & vbCrLf & "Sheet name: " & ws.Name & vbCrLf & "test"
With objPT
'
' Run through all the row fields
For Each objPTField In .RowFields
'
' Get the hidden items and make them visible
For Each objPTItem In objPTField.VisibleItems
objPTItem(LastDate).Visible = False
Next 'objPTItem
Next 'objPTField
End With
Application.ScreenUpdating = True
Next objPT
Next ws
End Sub
Which gives a compile error: Wrong number of arguments or invalid property assignment
Can anyone help here please?
Specifically, Can anyone suggest how I can make one date invisible as I am trying to do in Sub ShowAllItemsks3() above?
Thanks
Bookmarks