Problem
On the OrderDates Chart sheet, you have a list of categories in the Pivot Chart report filter.
Instead of selecting the next category from the drop-down list to view its chart, you’d like scrolling buttons on the chart, so you can quickly view each category’s data. This problem is based on the PrintCat.xlsmsample workbook.
Solution
You can add arrow shapes to the chart, and assign a macro to each shape, and then click the arrows to select the next or previous report filter item.
1. Add the following two procedures to a regular code module:
Sub PivotPageNext() Dim CountPI As Long Dim i As Long Dim pt As PivotTable Dim pf As PivotField Dim pi As PivotItem
Set pt = ActiveChart.PivotLayout.PivotTable Set pf = pt.PageFields("Category")
CountPI = 1 i = 1
For Each pi In pf.PivotItems
If pf.CurrentPage.Name = "(All)" Then CountPI = 0
Exit For End If
If pi.Name = pf.CurrentPage.Name Then Exit For
End If
CountPI = CountPI + 1 Next pi
For i = CountPI + 1 To pf.PivotItems.Count + 1 On Error Resume Next
If i = pf.PivotItems.Count + 1 Then pf.CurrentPage = "(All)"
Exit For End If
pf.CurrentPage = pf.PivotItems(i).Name If Err.Number = 0 Then
Exit For End If Next i
Set pf = Nothing Set pt = Nothing End Sub
Sub PivotPagePrev() Dim CountPI As Long Dim i As Long Dim pt As PivotTable Dim pf As PivotField Dim pi As PivotItem
Set pt = ActiveChart.PivotLayout.PivotTable Set pf = pt.PageFields("Category")
CountPI = 1
For Each pi In pf.PivotItems
If pf.CurrentPage.Name = "(All)" Then CountPI = pf.PivotItems.Count + 1 Exit For
End If
If pi.Name = pf.CurrentPage.Name Then Exit For CountPI = CountPI + 1
Next pi
For i = CountPI - 1 To 0 Step -1 On Error Resume Next
pf.CurrentPage = pf.PivotItems(i).Name If Err.Number = 0 Then Exit For If i = 0 Then
pf.CurrentPage = "(All)"
Exit For End If Next i
Set pf = Nothing Set pt = Nothing End Sub
2. Select the OrderDates Chart sheet, and on the Ribbon’s Insert tab, in the Illustrations group, click Shapes.
3. Click the Left Arrow shape, and then click the chart, to add that shape.
4. On the chart, right-click the Left Arrow shape, and then click Assign Macro.
5. In the list of macros, select PivotPagePrev, and then click OK.
6. Add a Right Arrow shape, and assign it the PivotPageNext macro.
7. To scroll through the report filter items, click the arrows (see Figure 11-9).
Figure 11-9.Scrolling arrows at the top of a pivot chart
How It Works
The two macros check the current report filter item and calculate its position in the list of items. If the Left Arrow shape is clicked, the PivotPagePrev macro runs, and the visible item with the next lower number is shown. If the Right Arrow shape is clicked, the PivotPageNext macro runs, and the visible item with the next higher number is shown.
■ symbols
% Difference From custom calculation, 49, 65
% of Column custom calculation, 53
% Of custom calculation, 48
% of Row custom calculation, 52
% of Total custom calculation, 54
< less than symbol, 92
> greater than symbol, 92 [ ] square brackets, 181
* wildcard, 29
? wildcard, 29
■ A
Access queries
preparing source data and, 13 source file and, 146
adding items, with calculated items, 57 Advanced Filters, 9
amounts, calculating percent difference between, 64
applying PivotTable styles, 72 area charts, 191
ascending sorts, 22 AutoFilters, 9
Average summary function, 43 AVERAGE worksheet function, 43 axis title, 200
■ B
bar charts, 191 Between filter, 28
black-and-white printing, 173 blank cells, counting, 45
blank rows, as separations for field items, 96 Bottom 10 filtering, 36
Bottom 10 Items conditional formatting option, 84
Bubble charts, 191, 193 buttons
hiding, 81
scrolling, for pivot charts, 233
■ C
calculated fields, 56–67 vs. calculated items, 56 correcting results in, 67 creating, 61
count of unique items, 66
grand total, correcting for, 65 identifying, 63
list of, creating, 67 modifying, 62 removing, 63 calculated items, 56–69
adding to fields with grouped items, 64 vs. calculated fields, 56
created accidentally, 67 errors messages and, 105 identifying, 63
index numbers, using, 59 list of, creating, 67, 105 modifying, 58, 60 removing, 59
with no data, hiding, 135 calculations, 41–69
custom, 46–56 formulas and, 56–69
summary functions and, 41–45 captions, hiding, 81
Cartesian products, 135 categories, sorting, 21
category axis, of pivot charts, 189 cell references, 179–182
cells
Highlight Cell Rules option and, 85 Merge and Center Cells with Labels option
and, 97 changing. Seeediting charts. Seepivot charts chart templates, 194 chart title, 199 cities, sorting, 21, 24
“Client Safety options…” error message, 152 Collapse/Expand indicator, 131
collapsing
items in selected fields, 131 row labels, 130
color scales, 81, 87 column charts, 191 column headings
freezing, 98
repeating across pages, 167 column labels, 9, 16
blank, adding N/A to, 133 filters for, 30
subtotals and, 118
column width, lost formatting and, 79 237
columns
% of Column custom calculation and, 53 Index custom calculation and, 55 monthly, 9
new, automatically including with source data, 141
organizing data in, 4
comments, adding to pivot table cells, 129 common filters, 32
Compact Form, 93
conditional formatting, 81–92 between two values, 85 bottom 10 items, 84 color scale, 81 data bars, 87, 226 data range, changing, 89 date period, 86
icon set, 82
order of rules, changing, 91 pivot charts and, 198 removing (clearing), 92 connections, enabling, 156 Contains filter, 29
contextual tooltips, 129
Count Numbers summary function, 43, 44 Count summary function, 41, 43, 213
blank cells and, 45 source data errors and, 44 COUNT worksheet function, 43 COUNTA worksheet function, 43 COUNTIF formula, 66
Create a Single Page Field for Me option, 17 creating
calculated items/calculated fields, 61, 105 chart templates, 194
custom lists, 25
custom PivotTable styles, 74
customized copies of pivot tables, 187 Excel Tables, 6
lists of formulas, 67 pivot charts, 189–196, 203 pivot tables, 1–20
current month, conditional formatting for, 86 custom calculations, 46-56
custom lists, 25, 27
custom number formats, 92 custom order, sorting in, 25
■ D
data
creating pivot tables from, 15 extracting, 173–186
grouping/totaling, 101–121 normalizing 11–12
organizing in rows/columns, 4 preparing source data and, 4, 8 data bars, 87, 226
data connections, enabling, 156
database password, storing in connection string, 155
date filters, 28, 30, 32 dates
current month, conditional formatting for, 86
data ranges, filtering for, 38 errors and, 185
filtering data by, 31
formatted, summarizing, 112 future, filtering for, 38
grouping, 101, 104, 107–113, 134 nonconsecutive, 33
pivot charts and, 201
Report Filter formatting and, 98
Running Total custom calculation and, 51 subtotal labels and, 99
default PivotTable style, changing, 74 deleting. See removing
difference between amounts, calculating, 64 Difference From custom calculation, 46 disabling
pivot table features, 228 security features, 230 Show Details feature, 230
Show Report Filter Pages feature, 160 displaying
all field items, 134 multiple value fields, 114 seconds, hundredths of, 121 subtotals, 115, 118
districts, sorting, 8, 21 Document Themes, 77 Does Not End With filter, 29 doughnut charts, 191
Down, Then Over option, Report Filters and, 125
drag-and-drop functionality, 137 drop-downs, disabling, 228 dynamic filters, 32
■ E
editing
calculated fields, 62
calculated item formulas in cells, 60 calculated items, 58
default PivotTable style, 74 grand total label text, 100 layouts, 93, 229
order of rules, 91 pivot tables, 123–138 ranges of data, 89 Report Layouts, 93 source Access file, 146 source CSV file, 147 source Excel Table, 145
subtotal date format, 99 subtotal label text, 99
electronic format, sharing information and, 3 empty cells, showing zero, 80
Enable Selection option, 78, 97 error values, hiding, 79 errors
calculated items and, 105 custom subtotals and, 183 dates and, 101, 185
Excel Field Names Not Valid, 8 GetPivotData formulas and, 179 GetPivotData function and, 182–186 numbers groupings and, 102 missing items and, 182 OLAP cubes and, 152 pivot charts and, 194 PivotTable reports and, 222 event code, 206
Excel 2007
new functions with, 71, 183 object model and, 205
Excel Field Names Not Valid error message, 8 Excel Lists (Excel 2003), 6, 15, 18
Excel Macro-Enabled Workbooks, 205 Excel Tables, 6
preparing source data and, 6, 9 source, 139, 144
external ranges of data, 143 extracted data
deleting sheets created, 176 formatting, 175
extracting data, 173–186 extracting underlying data, 173
■ F
field items
blank, extracting data for, 182 displaying all, 134
printing each on a new page, 172 separating with blank rows, 96 field labels, centering vertically, 96 field list order, 20
field names
showing on pivot charts, 199 changing in source data, 132 fields
source data and, 2 using multiple, 113 file size, reducing, 164
filter lists, clearing old items from, 132 filtered data, preparing source data and, 8 filtering
by selection, 35 date range, 38
nonconsecutive dates, 33 pivot charts, 195
pivot tables, 28–37 row field values, 32 top items, 36 filters
clearing old items from, 132 for dates, 32
for row labels, 30
manual filter, including new items, 34 multiple filters, applying, 29
removing from pivot fields, 220 report filter items, hiding, 37 row label text, 28
using more than one at a time, 29 fiscal quarter, grouping dates by, 108 formatting
extracted data, 175 hyperlinks, 98
losing when refreshing, 79 pivot charts and, 198 pivot tables, 71–100 formulas, 56–69
automatic insertion for, 176 changing Solve Order and, 68 creating list of, 67
determining type of, 63 freezing column/row headings, 98 functions, new with Excel 2007, 183 future dates, filtering for, 38
■ G
GetPivotData function, 167, 176–186 blank field items, 183
cell references and, 179–182 custom subtotals, 183 date references, 185 errors and, 182–186 missing items, 182
referencing other workbooks, 179 referring to a pivot table, 186 turning off, 178
value fields, cell references, 182 grand total values
changing label text for, 100 correcting for calculated field, 65 filtering, 32
hiding specific, 120
simulating an additional, 119
greater than (>), custom number formats and, 92
Group Selection command, 106 grouping
another pivot table affected, 110 calculated items, error message, 105 data, 101–113
dates, 104, 107–113, 134 dates by month, 107 dates outside the range, 112
Finditfasterathttp://superindex.apress.com/
error message, 101 fiscal quarter, 108 months and weeks, 110 numbers, 102
report filter, 104 showing all months, 134 starting date, 107 text items, 106
■ H
heading cells, blank, 8 heading rows, freezing, 98
headings, repeating across pages, 167 hiding
buttons, 81 error values, 79 grand total values, 120 items, 16, 36, 37, 135 labels/captions, 81 highest values
data bars for, 87 highlighting, 81
Highlight Cell Rules conditional formatting option, 85
hours, totaling, 121 hyperlinks, formatting, 98
■ I
I Will Create the Page Fields option, 17 icon sets, 82, 87
IFERROR function, 183 indentation, for row labels, 94 Index custom calculation, 55 INDEX function, 143
index numbers, in calculated items, 59 INDIRECT function, 186
Information Rights Management, 3 inner fields, 21
innermost fields, row/column label subtotals and, 118
items, 134–136
adding, with calculated items, 57 bottom, filtering for, 36
grouping, in Report Filters, 104 hiding, 16, 36, 37, 135
left-to-right sorting for, 24 limits on, 162
missing, preventing errors for, 182 new, sorting for, 23
old, clearing from filter lists, 132 in selected fields, collapsing, 131 top, filtering for, 36, 221
troubleshooting sorting of, 27 unique, counting, 66
with no data, hiding, 135
■ K
keyboard shortcuts, for macros, 209, 212
■ L
label filters, 28, 30, 32
labels, 9, 16. See also column labels; row labels
blank, adding N/A to, 133
for grand total values, changing, 100 hiding, 81
merged, changing alignment for, 97 subtotals and, 99, 118
vertical centering and, 96 layouts
changing, 93, 229 deferred update for, 154 pivot charts and, 197
pivot table performance improvement and, 164
preventing changes to, 229 left-to-right sorting, 24
legend fields, of pivot charts, 189
less than (<), custom number formats and, 92
Less Than Or Equal To filter, 29 limits, 162
line breaks, displaying, 97 line charts, 191
locating
source Access file, 146 source data, 1
source Excel Table, 139 source pivot table, 202 lowest values
data bars for, 87 highlighting, 81, 84
■ M
macro recorder, 205, 208 macros, 4
changing recorded code and, 212 keyboard shortcuts for, 209, 212 modifying recorded code, 212 recording, 208–213
running, 206, 212 manual filters, 30, 34 Manual sorts, 23
MAX worksheet function, 43 Max summary function, 43 Median function, 42
Merge and Center Cells with Labels option, 97
Microsoft Access queries preparing source data and, 13 source file and, 146
Min summary function, 43
MIN worksheet function, 43 Mode function, 42
modifying.See also editing calculated fields, 62
calculated item formulas in cells, 60 calculated items, 58
pivot tables, 123–138 monthly columns, 9 months
current, conditional formatting for, 86 grouping, 107, 110, 134
moving
pivot charts, 203 source Excel Table, 144
Multiple Consolidation Ranges, 11, 16 multiple fields, sorting and, 21 multiple filters, 29
multiple text columns, normalizing data and, 12
multiple values for a field, 113
multiple value fields, displaying vertically, 114
■ N
N/A, adding as text to blank row/column labels, 133
naming
Excel Tables, 6 worksheets, 188 new items out of order, 23 nonconsecutive dates, 33 normalizing data, 10
multiple text columns and, 12 single text columns and, 11 numbers.See also dates
custom number formats and, 92 grouping, 102
pivot charts and, 198
Report Filter formatting and, 98
■ O
OFFSET function, 142 OLAP cubes
“Client Safety options…” error message and, 152
preparing source data and, 14
OLAP-based pivot tables, cell references in, 180
order dates, 105, 112
order of rules, changing for conditional formatting, 91
OrderDate field, 101–113, 118 outer fields, 21
Outline Form, 94
Over, Then Down option, Report Filters and, 125
■ P
page fields, 16-18 page labels, 16 passwords, 2
database, 155
workbook protection and, 161 worksheet protection and, 225 Paste Special Values, caution for, 96 percent
% Difference From custom calculation and, 49, 65
% of Column custom calculation and, 53
% Of custom calculation and, 48
% of Row custom calculation and, 52
% of Total custom calculation and, 54 filtering for, 37
Percentile function, 42
performance, ways to improve, 163 permissions, 3
pie charts, 191
pivot caches, 111, 223, 224 PivotChart Field List, 196 PivotChart Filter Pane, 196 pivot charts, 189–204
adding text boxes to, 200 combination pivot chart, 203 converting to static charts, 199 chart title, 199
chart subtype, selecting, 191 chart type, selecting, 191 column charts, 191 creating, 189–196, 203 data table, formatting, 198 default chart type, 193 field names, showing, 199 filtering, 195
grand totals, 198 keyboard shortcut, 193 layout affects pivot table, 197 moving, 203
normal chart from pivot table data, 194 number format, 198
planning, 189 refreshing, 201 removing, 204
scrolling buttons for, 233 series order, 197
sorting, 197
source pivot table, locating, 202 static chart, converting to, 199 subtypes of, 192
templates, 194 types of, 191
years, multiple series, 201 pivot fields, modifying, 129–133
Finditfasterathttp://superindex.apress.com/
pivot items
hiding items with no data, 135 showing all field items, 134 trailing spaces, 136 pivot tables
creating, 1–20
customized copies of, 187 deferred layout update for, 154 deleting, 137
disabling features for, 228 drag-and-drop, allowing, 137 filtering, 28–37
formatting, 71–100 grouping dates and, 110
lost formatting when refreshing, 79 modifying, 123–138
new features with Excel 2007, 71 overlapping, refreshes and, 153 pivot charts created from, 189–204 printing, 167–173, 208, 231 privacy and, 160
programming, 205–236
referenced in other workbooks by GetPivotData formula, 179 referring from one to another, 186 refreshing, 149–154
security/protection and, 155, 161 sharing, 2
sorting, 21–28
source for pivot chart, locating, 202 styles for.SeePivotTable styles updating, 139–154, 226 PivotChart Filter pane, 195
PivotTable and PivotChart wizard, 10, 15 PivotTable Field List, 18, 195
PivotTable styles, 71–77 applying/canceling, 72
copying to a different workbook, 76 custom, 7, 74
default, changing, 74 how they work, 72 removing (clearing), 73 themes, impacting, 77 PivotTable wizard, 10, 15 planning a pivot table, 1-4 preparing source data, 4–14 previewing styles, 71 print area
resetting automatically, 231 adjusting automatically, 170
printed format, sharing information and, 3 printing pivot tables 167–173
black and white, 173
for each report filter item, 170, 232 recording macros and, 208 resetting print area and, 231
starting each item on a new page, 172
privacy, 160
Product summary function, 43 PRODUCT worksheet function, 43 programming pivot tables, 205–236
changing recorded code and, 212 pivot cache and, 223
protection, 157–161
preventing changes to pivot tables and, 157
protected worksheets, refreshing pivot tables and, 152, 225
Show Report Filter Pages feature and, 160
■ Q
QAT (Quick Access Toolbar), 10 quick styles.SeePivotTable styles
■ R
radar charts, 191 ranges of data
changing, 89 external, 143 formatting, 85
outside-of-range dates and, 112 records, limits and, 162
re-creating source data, 174 reducing file size, 164 refreshing
after queries have been executed, 153 all pivot tables in a workbook, 151 automatically when source data changes,
226
defer layout update, 154 losing formatting, 79 every 30 minutes, 150 pivot charts, 201
pivot tables, 149–154, 225, 230 protected sheet, 152
stopping refreshes and, 150 two tables overlap, 153 when a file opens, 149 removing
calculated fields, 63 calculated items, 59 conditional formatting, 92 filters, from pivot fields, 220 formulas, from accidentally created
calculated items, 67 pivot charts, 204
pivot tables, 137 PivotTable styles, 73 renaming Values fields, 127 repeating
headings, across pages, 167 report filters, across pages, 167 row labels, 95, 167
report filters, 18, 37 arranging, 124
changing selection in related tables, 218 grouping items and, 104
lost formatting and, 79 number format and, 98 pivot table printing and, 232 preventing selection of (All) in, 227 repeating across pages, 167 scrolling through items and, 233 shifts when adding, 123
worksheet printing and, 170 Report Layouts. Seelayouts resources for further reading
Excel 2007 object model, 205 Information Rights Management, 3 Microsoft Access queries, 13 normalization, 10
OLAP cube error message, 152 OLAP cubes, 14, 162
programming Excel, 205 security/privacy, 161
statistical function improvements in Excel, 43
VAR/VARP improvements, 44 Reverse Icon Order option, 84 row headings, freezing, 98 row labels, 9, 16
blank, adding N/A to, 133 centering vertically, 96 collapsing, 130, 131 filtering, 28
grand total value filtering and, 32 indentation for, 94
merged, changing alignment, 97 repeating, 95, 167
selection filtering and, 35 sorting, 21
subtotals and, 118 rows
% of Row custom calculation and, 52 Index custom calculation and, 55 new, automatically including with source
data, 141 organizing data in, 4 rules
Data Bar, 88
order of, changing for conditional formatting, 91
ranges of data, conditional formatting and, 89
running macros, 206
Running Total custom calculation, 50
■ S
sales data
OLAP cubes and, 14 by region, 15 summarizing, 1, 9 sales order data, 8
by district, 8
Microsoft Access and, 13 summarizing, 4
sample code, using, 205 sample files, folder for, 139 Scatter charts, 191, 193 Screen Tip feature, 71
seconds, displaying hundredths of, 121 secured network folders, 3
security, 3, 155–157
disabling security features and, 230 resources for additional reading and, 161 Selection, Enable, 78
sensitive data, 2
separate sheets, using Excel data on, 15 series order, pivot charts and, 197 sharing pivot tables, 2
Shortest Bar, data bars and, 88 Show Details feature, 173–176
disabling, 230
naming/formatting, 214
Show Report Filter Pages feature, disabling, 160
single text columns, normalizing data and, 11 Solve Order, changing, 68
sorting
custom order, 25 left-to-right, 24
new items out of order, 23 pivot charts, 197
pivot tables, 21–28 row labels, 21
troubleshooting for, 27 values, 22
source Access file, 146 source CSV file, 147 source data
automatically including new data with, 141, 143
changing the source Access file, 146 changing the source CSV file, 147 changing the source Excel Table, 145 custom number formats and, 92 errors in, 44
extracting, 173
field names in, changing, 132 filtered Excel data, 8
locating the source Access file, 146 locating the source Excel Table, 139 location of, 1
Finditfasterathttp://superindex.apress.com/
moving the source Excel Table, 144 number fields in, 128
number of records and, 162 preparing, 4–14
re-creating, 174 update frequency of, 2
updating pivot tables and, 139–149, 226 source Excel Table, 139, 144
spaces, ignoring when summarizing data, 136
square brackets [ ], in field/item names, 181 starting date, grouping dates and, 107, 109 static charts, converting pivot charts to, 199 StDev summary function, 43
STDEV worksheet function, 43 StDevp summary function, 44 Stock charts, 191, 193
strong passwords, 3 styles. SeePivotTable Styles.
subtotals 115–119 creating multiple, 117 custom, errors and, 183 data format for, changing, 99 displaying, at bottom of group, 115 formatting, Enable Selection option and,
78
label text for, changing, 99 preventing from appearing, 116 removing from source data, 5 sorting and, 22
sum, filtering for, 37
Sum summary function, 41, 213 summarizing
data, ignoring trailing spaces and, 136 formatted dates, 112
summary functions, 41–45, 213 changing for all value fields, 213 counting blank cells, 45 defaulting to Sum or Count, 41 errors in the source data, 44 surface charts, 191
■ T
table. SeeExcel Tables Tabular Form, 94
templates, for pivot charts, 194 text
for grand total labels, changing, 100 pivot charts and, 200
replacing with cell references, 179 for subtotal labels, changing, 99 in Values area, displaying, 128 text files, preparing source data and, 14 text items, grouping, 106
themes, 77
time field, totaling hours, 121 time formats, 121
tooltips, 129
Top 10 filtering, 36, 221 Top/Bottom Rules list, 84 totals. See alsogrand total values
% of Total custom calculation and, 54 for data, 101, 119–121
for hours, 121
Index custom calculation and, 55 removing from source data, 5
Running Total custom calculation and, 50 trailing spaces, ignoring when summarizing
data, 136
troubleshooting item sorting, 27
■ U
unique count of items, 66
update frequency for source data, 2 updating pivot tables, 139–154
refreshing and, 149–154
OLAP-based pivot tables and, 152 source data and, 139–149
■ V
value filters, 30
values. See alsogrand total values formatting specific range of, 85 value fields and, 113
Values area
changing content in, 126, 222 displaying text in, 128
empty cells, showing zero/character in, 80 Values fields
arranging vertically, 127
changing summary function for, 213 modifying, 126–129
multiple value fields, displaying vertically, 114
renaming, 127 text, showing, 128
using cell references for, 181 Var summary function, 44 VAR worksheet function, 44 Varp summary function, 44 VARP worksheet function, 44
VBA (Visual Basic for Applications), 208 VBE (Visual Basic Editor), 205
visual impact, adding to data via conditional formatting, 81–92
■ W
week number, 110
weeks, grouping dates by, 108, 110 wildcard characters, 29
wizards
PivotChart, 10, 15 PivotTable, 10, 15