ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Pivottable code problem (https://www.excelbanter.com/excel-programming/359780-pivottable-code-problem.html)

Rayo K

Pivottable code problem
 
Hi,

I am having a problem with my pivottable code. What I want to do is show all
items for which a certain field is blank. I want it to be able to update and
continue filtering with a single macro. My code sets all items in the field
to be invisible and then makes the (blank) items visible. I came up with the
initial code by recording a macro, but the default macro action individually
sets each tiem. Since the specific items will keep changing (they are dates).
I need to set items to be invisible without knowing beforehand what they are.

So, without further ado, my code:

Private Sub CommandButton1_Click()

Dim x As Integer, itemNum As Long

'creates a pointer to pivottable "unreconciled" or whatever its name will
end up being
Dim URPivot As PivotTable
Set URPivot = ActiveSheet.PivotTables(1)

'Refresh the pivottable
URPivot.PivotCache.Refresh

'reset the field item filter
itemNum = URPivot.PivotFields("Date Invoiced").PivotItems.Count
With URPivot.PivotFields("Date Invoiced")
For x = 0 To itemNum - 1
.PivotItems(x).Visible = False #########exception!!!!##########
Next x
.PivotItems("(blank)").Visible = True
End With

End Sub

So the problem is on the line where I noted the exception, it crashes and I
cant figure out why. Any ideas?
Thanks!



All times are GMT +1. The time now is 11:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com