Macro Pivot Table Issue

rbyrne8760

New Member
Joined
Jul 12, 2013
Messages
2
Hello,

I am creating a macro that creates several pivot tables. These tables each filter out a set of data. If the report I run the macro on, one day, has no results for one of those sets of data, the macro results in an error. The error states "Unable to set visible property of PivotItem class" in Excel 2007. When I go to debug this, I am brought to this string of information.


Code:
    ActiveSheet.PivotTables("PivotTable5").PivotFields("Repair Unit").CurrentPage _
        = "(All)"
    'With ActiveSheet.PivotTables("PivotTable5").PivotFields("Repair Unit")
     '   .PivotItems("CAMERAS").Visible = False
      '  .PivotItems("HENKE").Visible = False
       ' .PivotItems("INSIDE REPAIR").Visible = False
        '.PivotItems("OEM REPAIR").Visible = False
    
     Set PF = ActiveSheet.PivotTables("PivotTable5") _
        .PivotFields("Repair Unit")
    
    PF.ClearAllFilters
    
    With PF
        For Each PI In .PivotItems
            If Not (PI.Caption Like "OUTSIDE REPAIR") Then
                PI.Visible = False
            End If
        Next PI
    
    End With

Can anyone help me with this? If there are no results to filter out for that day, I simply want the results box for that category to show up blank
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi there, welcome to the board!

I would use a function to determine if the field exists, like so..

Code:
Function FIELDHASDATA(ByVal Field As PivotField) As Boolean
    On Error Resume Next
    FIELDHASDATA = CBool(Field.DataRange.Value = "(blank)")
    On Error GoTo 0
End Function

Then you just call it in your routine in an If/Then clause, like so...

Code:
Sub TEST()
    On Error Resume Next
    If FIELDHASDATA(ActiveSheet.PivotTables("PivotTable1").PivotFields("Repair Unit")) = True Then
        'do stuff to the field here
    End If
    On Error GoTo 0
End Sub

Notice the error handling in the second routine, because if the field doesn't exist it will throw an error.

HTH
 
Upvote 0
Hello Zack,

Thanks for the function and if/then clause! However, when I insert the function code, it wants me to insert an end sub, thus creating two different macros in the middle of mine. Is there a way I can avoid this? Thanks!
 
Upvote 0
You're probably calling it wrong. Do it like I posted, or post your code so I can see what's going on.
 
Upvote 0

Forum statistics

Threads
1,216,090
Messages
6,128,765
Members
449,467
Latest member
sdafasfasdf

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