Unable to access visible property of Pivot Items class

G

Guest

Hi,

with the following code I get run-time error 1004 with the above description:

Set pt = ActiveSheet.PivotTables("Pivot_Table")
Set pf = pt.PivotFields("Pivot Class")

With pf
.Orientation = xlColumnField
.Position = 1
For Each pi In pf.PivotItems
If pi.Name = "#N/A" Or "1" Then
pi.Visible = False
Else: pi.Visible = True
End If
Next pi
End With

The error occurs at the line

pi.Visible = False

funnily enough when the test condition of the If statement has failed, i.e.
execution should have skipped this line.

Also, the Orientation property of the PivotField for some reason changes to
xlHidden, after it has been explicitly set to xlColumnField before entering
the If statement. I have a suspicion that this may be behind it, but I don't
know of a way to stop this particular property changing.

Any suggestions?

Thanks
 
D

Debra Dalgleish

To prevent the error, set the Sort for the field to Manual. For example:

With pf
.AutoSort xlManual, "Pivot Class"
.Orientation = xlColumnField
 
G

Guest

Thanks very much Debra, this was exactly the solution needed. This of course
then helped me to discover the glaring error in my condition, where I asked
Excel to check whether "1" was true, rather than checking pi.Name = "1" :)

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top