Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How to cycle through displayed PivotItems instead of the VisibleItemscollection

Hi,

I've run into a bit of a pickle with a VBA script.

The script sets formatting on PivotItems by cycling through the
VisibleItems collection. This works fine until I add a page field.
I've figured out that the Visible property of the PivotItem stays True
even though the item may be hidden by the selection on the page field.
The collection is therefore not that useful to me since it will cause
errors with any calls to get a range for any PivotItem hidden from
view by the page field. How can I get around this?

Below is an excerpt of the script in question:
==================================================
01 Dim pvtItems As PivotItems, pvtItem As PivotItem
02 Set pvtItems = pvtTable.PivotFields(myRow).VisibleItems
03 For Each pvtItem In pvtItems
04 pvtItem.LabelRange.Select
05
06 With Selection
07 .Borders.LineStyle = xlNone
08 .Borders(xlEdgeTop).LineStyle = xlContinuous
09 .Borders(xlEdgeTop).Weight = xlHairline
10 .Borders(xlEdgeTop).ColorIndex = xlAutomatic
11 .Borders(xlEdgeBottom).LineStyle = xlContinuous
12 .Borders(xlEdgeBottom).Weight = xlHairline
13 .Borders(xlEdgeBottom).ColorIndex = xlAutomatic
14 End With
15 Next pvtItem
==================================================

The above script will fail on line 04 as soon as it hits an PivotItem
which is hidden from view by any other selections made in the pivot
table (e.g. on the page field).

Your help is greatly appreciated.

- Thomas

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How to cycle through displayed PivotItems instead of theVisibleItems collection

On Apr 22, 11:49*am, Thomas V wrote:
Hi,

I've run into a bit of a pickle with a VBA script.

The script sets formatting on PivotItems by cycling through the
VisibleItems collection. This works fine until I add a page field.
I've figured out that the Visible property of the PivotItem stays True
even though the item may be hidden by the selection on the page field.
The collection is therefore not that useful to me since it will cause
errors with any calls to get a range for any PivotItem hidden from
view by the page field. How can I get around this?

Below is an excerpt of the script in question:
==================================================
01 * * *Dim pvtItems As PivotItems, pvtItem As PivotItem
02 * * *Set pvtItems = pvtTable.PivotFields(myRow).VisibleItems
03 * * *For Each pvtItem In pvtItems
04 * * * * * * *pvtItem.LabelRange.Select
05
06 * * * * * * *With Selection
07 * * * * * * * * * * *.Borders.LineStyle = xlNone
08 * * * * * * * * * * *.Borders(xlEdgeTop).LineStyle = xlContinuous
09 * * * * * * * * * * *.Borders(xlEdgeTop).Weight = xlHairline
10 * * * * * * * * * * *.Borders(xlEdgeTop).ColorIndex = xlAutomatic
11 * * * * * * * * * * *.Borders(xlEdgeBottom).LineStyle = xlContinuous
12 * * * * * * * * * * *.Borders(xlEdgeBottom).Weight = xlHairline
13 * * * * * * * * * * *.Borders(xlEdgeBottom).ColorIndex = xlAutomatic
14 * * * * * * *End With
15 * * *Next pvtItem
==================================================

The above script will fail on line 04 as soon as it hits an PivotItem
which is hidden from view by any other selections made in the pivot
table (e.g. on the page field).

Your help is greatly appreciated.

- Thomas


Hi again,

I've found how to work around the issue. Since the routine causes
predictable errors one can simply trap the errors and work from there.

Here's an updated script:

01 Dim pvtItems As PivotItems, pvtItem As PivotItem
02 Set pvtItems = pvtTable.PivotFields(myRow).VisibleItems
03 For Each pvtItem In pvtItems
04 'Set error handling as we want it
05 Err.Clear
06 On Error Resume Next
07 pvtItem.LabelRange.Select 'this is the line we trap errors for
08 If Err.Number = 0 Then
09 With Selection
10 .Borders.LineStyle = xlNone
11 .Borders(xlEdgeTop).LineStyle = xlContinuous
12 .Borders(xlEdgeTop).Weight = xlHairline
13 .Borders(xlEdgeTop).ColorIndex = xlAutomatic
14 .Borders(xlEdgeBottom).LineStyle = xlContinuous
15 .Borders(xlEdgeBottom).Weight = xlHairline
16 .Borders(xlEdgeBottom).ColorIndex = xlAutomatic
17 End With
18 End If
19 'Turn on regular error handling again
20 Err.Clear
21 On Error GoTo MyErrorHandler
22 Next pvtItem

I hope this is useful to others.

- Thomas
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default How to cycle through displayed PivotItems instead of theVisibleItems collection


I stumbled over a similar problem recently while trying to save
specific pivot table layouts. I solved it by using a case select and
making the pagefield temporarily a rowfield.
....
Dim pt as PivotTable
Dim objPF As PivotField
Dim pfItem As PivotItem

....
For Each objPF In pt.VisibleFields
Select Case objPF.Orientation
Case xlRowField
For Each pfItem In objPF.PivotItems
If pfItem.Visible = True Then
...
End If
Next pfItem
Case xlColumnField
For Each pfItem In objPF.PivotItems
If pfItem.Visible = True Then
...
End If
Next pfItem
Case xlPageField
objPF.Orientation = xlRowField
For Each pfItem In objPF.PivotItems
' Debug.Print pfItem.Parent.Name, pfItem.Name,
pfItem.Visible
If pfItem.Visible = True Then
....
End If
Next pfItem
objPF.Orientation = xlPageField
Case xlDataField

Case xlHidden

End Select
Next objPF
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I make a graph with 2-cycle X 3-cycle log-log graph paper? Charles A. Wilson Charts and Charting in Excel 1 December 17th 09 03:03 AM
Missing PivotItems [email protected][_2_] Excel Programming 3 August 28th 08 10:09 PM
PivotItems Bug?! WhytheQ Excel Programming 2 November 29th 06 04:31 PM
How do I keep result from 1 iteration cycle to use in next cycle? sgl8akm Excel Discussion (Misc queries) 0 July 27th 06 08:28 PM
Add PivotItems to PivotTable / Enabling - Disabling PivotItems Ole[_3_] Excel Programming 1 July 8th 03 03:24 PM


All times are GMT +1. The time now is 12:40 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"