319 40 2MB
English Pages 336 Year 2006
Excel Pivot Tables Recipe Book A Problem-Solution Approach DEBRA DALGLEISH
Excel Pivot Tables Recipe Book: A Problem-Solution Approach Copyright © 2006 by Debra Dalgleish All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-629-6 ISBN-10 (pbk): 1-59059-629-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Ewan Buckingham Technical Reviewer: Don Reamy Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Matt Wade Project Manager: Kylie Johnston Copy Edit Manager: Nicole LeClerc Copy Editor: Liz Welch Assistant Production Director: Kari Brooks-Copony Production Editor: Katie Stence Compositor: Kinetic Publishing Services, LLC Proofreader: Elizabeth Berry Indexer: Valerie Perry Cover Image Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code section.
Contents at a Glance About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
■CHAPTER 1
Creating a Pivot Table
■CHAPTER 2
Sorting and Grouping Pivot Table Data . . . . . . . . . . . . . . . . . . . . 39
■CHAPTER 3
Calculations in a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
■CHAPTER 4
Formatting a Pivot Table
■CHAPTER 5
Extracting Pivot Table Data
■CHAPTER 6
Modifying a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
■CHAPTER 7
Updating a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
■CHAPTER 8
Securing a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
■CHAPTER 9
Pivot Table Limits and Performance . . . . . . . . . . . . . . . . . . . . . . 195
.......................................1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
■CHAPTER 10 Publishing a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 ■CHAPTER 11 Printing a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 ■CHAPTER 12 Pivot Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 ■CHAPTER 13 Programming a Pivot Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
iii
Contents About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
■CHAPTER 1
Creating a Pivot Table 1.1. 1.2. 1.3. 1.4. 1.5. 1.6. 1.7. 1.8. 1.9. 1.10. 1.11. 1.12. 1.13. 1.14. 1.15. 1.16. 1.17. 1.18. 1.19.
.......................................1
Planning a Pivot Table: Getting Started . . . . . . . . . . . . . . . . . . . . 1 Planning a Pivot Table: Accessing the Source Data . . . . . . . . . . . . . 2 Planning a Pivot Table: Source Data Fields . . . . . . . . . . . . . . . . . . . . 3 Planning a Shared Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Preparing the Source Data: Using an Excel List . . . . . . . . . . . . . . . . 5 Preparing the Source Data: Excel List Invalid Field Names . . . . . . 7 Preparing the Source Data: Using a Filtered Excel List . . . . . . . . . 7 Preparing the Source Data: Using an Excel List with Monthly Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Preparing the Source Data: Using an Excel List with Monthly Columns and Text Fields . . . . . . . . . . . . . . . . . . . . . . 10 Preparing the Source Data: Using an Access Query . . . . . . . . . . . 11 Preparing the Source Data: Using an Access Query with Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Preparing the Source Data: Using a Text File . . . . . . . . . . . . . . . . . 12 Preparing the Source Data: Using an OLAP Cube . . . . . . . . . . . . . 12 Preparing the Source Data: Creating an OLAP Cube . . . . . . . . . . . 13 Preparing the Source Data: Using Multiple Consolidation Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Preparing the Source Data: Alternatives to Using Multiple Consolidation Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Preparing the Source Data: Setting Up Multiple Consolidation Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Preparing the Source Data: Benefits of Using Another PivotTable or PivotChart Report . . . . . . . . . . . . . . . . . . . . 14 Preparing the Source Data: Problems Caused by Using Another PivotTable or PivotChart Report . . . . . . . . . . . . . 15 v
vi
■CONTENTS
1.20. Preparing the Source Data: Page Field Settings When Using Another PivotTable or PivotChart Report . . . . . . . . . . . . . . . . 15 1.21. Connecting to the Source Data: Selecting a Large Range in an Excel List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.22. Connecting to the Source Data: Using Arrow Keys in an Excel List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.23. Connecting to the Source Data: Installing Drivers for External Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.24. Connecting to the Source Data: Creating a New Source for External Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.25. Connecting to the Source Data: Excel Hangs When Using External Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.26. Connecting to the Source Data: Using Pages with Multiple Consolidation Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.27. Understanding PivotTable Options: Table Name . . . . . . . . . . . . . . . 23 1.28. Understanding PivotTable Options: Table Naming Rules . . . . . . . . 23 1.29. Understanding PivotTable Options: Row and Column Grand Totals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.30. Understanding PivotTable Options: AutoFormat Table . . . . . . . . . . 24 1.31. Understanding PivotTable Options: Subtotal Hidden Page Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.32. Understanding PivotTable Options: Merge Labels . . . . . . . . . . . . . 25 1.33. Understanding PivotTable Options: Merge Labels Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.34. Understanding PivotTable Options: Preserve Formatting . . . . . . . 27 1.35. Understanding PivotTable Options: Repeat Item Labels on Each Printed Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.36. Understanding PivotTable Options: Page Layout . . . . . . . . . . . . . . 28 1.37. Understanding PivotTable Options: Fields per Column/Fields per Row . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 1.38. Understanding PivotTable Options: Error Values. . . . . . . . . . . . . . . 29 1.39. Understanding PivotTable Options: Empty Cells . . . . . . . . . . . . . . . 30 1.40. Understanding PivotTable Options: Set Print Titles . . . . . . . . . . . . 31 1.41. Understanding PivotTable Options: Mark Totals with * . . . . . . . . . 32 1.42. Understanding PivotTable Options: Save Data with Table Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 1.43. Understanding PivotTable Options: Enable Drill to Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 1.44. Understanding PivotTable Options: Refresh on Open . . . . . . . . . . 34 1.45. Understanding PivotTable Options: Refresh Every n Minutes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
■CONTENTS
1.46. 1.47. 1.48. 1.49.
■CHAPTER 2
Understanding PivotTable Options: Save Password . . . . . . . . . . . . 35 Understanding PivotTable Options: Background Query . . . . . . . . . 36 Understanding PivotTable Options: Optimize Memory . . . . . . . . . 36 Understanding Pivot Table Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Sorting and Grouping Pivot Table Data . . . . . . . . . . . . . . . . . . . . 39 2.1. 2.2. 2.3. 2.4. 2.5. 2.6. 2.7. 2.8. 2.9. 2.10. 2.11. 2.12. 2.13. 2.14. 2.15. 2.16. 2.17. 2.18. 2.19. 2.20. 2.21. 2.22. 2.23. 2.24. 2.25. 2.26. 2.27.
Sorting a Pivot Field: One Row Field. . . . . . . . . . . . . . . . . . . . . . . . . 39 Sorting a Pivot Field: Outer Row Field . . . . . . . . . . . . . . . . . . . . . . . 41 Sorting a Pivot Field: Inner Row Field. . . . . . . . . . . . . . . . . . . . . . . . 43 Sorting a Pivot Field: Renamed Numeric Items . . . . . . . . . . . . . . . 44 Sorting a Pivot Field: New Items Out of Order . . . . . . . . . . . . . . . . 44 Sorting a Pivot Field: Sorting Items Geographically . . . . . . . . . . . . 45 Sorting a Pivot Field: Data Source Order . . . . . . . . . . . . . . . . . . . . . 48 Sorting a Pivot Field When Some Items Won’t Sort Correctly . . . . . 48 Using Top 10 AutoShow: Specifying Top Items Overall . . . . . . . . . 49 Using Top 10 AutoShow: Specifying Items Over a Set Amount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Using Top 10 AutoShow: Referring to a Cell Value . . . . . . . . . . . . . 50 Grouping: Error Message When Grouping Items in a Date Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Grouping: Error Message When Grouping Items in a Numeric Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Grouping: Error Message When Grouping Items in a Date Field with No Blanks or Text . . . . . . . . . . . . . . . . . . . . . . . 52 Grouping the Items in a Page Field. . . . . . . . . . . . . . . . . . . . . . . . . . 52 Grouping the Items in a Page Field: Using an External Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Grouping: Incorrect Error Message About Calculated Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Grouping Text Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Grouping Dates by Week . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Grouping Dates by Fiscal Quarter . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Grouping Renamed Numeric Items . . . . . . . . . . . . . . . . . . . . . . . . . 54 Grouping Months . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Grouping Dates Using the Starting Date . . . . . . . . . . . . . . . . . . . . . 55 Grouping Dates by Months and Weeks . . . . . . . . . . . . . . . . . . . . . . 56 Grouping the Items in a Pivot Table Based on an Existing Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Grouping Dates Outside the Range . . . . . . . . . . . . . . . . . . . . . . . . . 56 Grouping Nonadjacent Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
vii
viii
■CONTENTS
■CHAPTER 3
Calculations in a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3.1. 3.2. 3.3. 3.4. 3.5. 3.6. 3.7. 3.8. 3.9. 3.10. 3.11. 3.12. 3.13. 3.14. 3.15. 3.16. 3.17. 3.18. 3.19. 3.20. 3.21. 3.22. 3.23. 3.24. 3.25. 3.26. 3.27. 3.28.
Using Summary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Using Summary Functions: Default Functions . . . . . . . . . . . . . . . . 63 Using Summary Functions: Counting Blank Cells . . . . . . . . . . . . . 64 Using Custom Calculations: Difference From . . . . . . . . . . . . . . . . . 65 Using Custom Calculations: % Of . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Using Custom Calculations: % Difference From . . . . . . . . . . . . . . 68 Using Custom Calculations: Running Total . . . . . . . . . . . . . . . . . . . 69 Using Custom Calculations: % of Row . . . . . . . . . . . . . . . . . . . . . . . 71 Using Custom Calculations: % of Column . . . . . . . . . . . . . . . . . . . . 72 Using Custom Calculations: % of Total . . . . . . . . . . . . . . . . . . . . . . 72 Using Custom Calculations: Index . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Using Formulas: Calculated Field vs. Calculated Item . . . . . . . . . 74 Using Formulas: Adding Items with a Calculated Item . . . . . . . . . 75 Using Formulas: Modifying a Calculated Item . . . . . . . . . . . . . . . . 76 Using Formulas: Temporarily Removing a Calculated Item. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Using Formulas: Permanently Removing a Calculated Item. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Using Formulas: Using Index Numbers in a Calculated Item. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Using Formulas: Using Relative Position Numbers in a Calculated Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Using Formulas: Modifying a Calculated Item Formula in a Cell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Using Formulas: Creating a Calculated Field . . . . . . . . . . . . . . . . . 80 Using Formulas: Modifying a Calculated Field . . . . . . . . . . . . . . . . 80 Using Formulas: Temporarily Removing a Calculated Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Using Formulas: Permanently Removing a Calculated Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Using Formulas: Determining the Type of Formula . . . . . . . . . . . . 82 Using Formulas: Adding a Calculated Item to a Field With Grouped Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Using Formulas: Calculating the Difference Between Plan and Actual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Using Formulas: Correcting the Grand Total for a Calculated Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Using Formulas: Counting Unique Items in a Calculated Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
■CONTENTS
3.29. Using Formulas: Correcting Results in a Calculated Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 3.30. Using Formulas: Listing All Formulas . . . . . . . . . . . . . . . . . . . . . . . . 85 3.31. Using Formulas: Accidentally Creating a Calculated Item. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 3.32. Using Formulas: Solve Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
■CHAPTER 4
Formatting a Pivot Table 4.1. 4.2. 4.3. 4.4. 4.5. 4.6. 4.7. 4.8. 4.9. 4.10. 4.11. 4.12. 4.13. 4.14. 4.15. 4.16. 4.17. 4.18. 4.19. 4.20. 4.21. 4.22. 4.23. 4.24. 4.25. 4.26. 4.27. 4.28. 4.29. 4.30. 4.31. 4.32.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Using AutoFormat: Applying a Predefined Format . . . . . . . . . . . . . 89 Using AutoFormat: Removing an AutoFormat . . . . . . . . . . . . . . . . 90 Using AutoFormat: Applying a Standard Table AutoFormat . . . . . 90 Using the Enable Selection Option . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Losing Formatting When Refreshing the Pivot Table . . . . . . . . . . 92 Retaining the Source Data Formatting . . . . . . . . . . . . . . . . . . . . . . . 93 Hiding Data Errors on Worksheet . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Hiding Errors When Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Showing Zero in Empty Data Cells . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Using Conditional Formatting in a Pivot Table . . . . . . . . . . . . . . . . 95 Creating Custom Number Formats in the Source Data . . . . . . . . . 96 Totaling Hours in a Time Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Displaying Hundredths of Seconds in a Pivot Table . . . . . . . . . . . . 97 Centering Field Labels Vertically . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Applying an Indented AutoFormat. . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Creating an Indented Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Applying a Tabular AutoFormat . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Displaying Subtotals at the Top of a Group . . . . . . . . . . . . . . . . . . 101 Separating Field Items with Blank Rows . . . . . . . . . . . . . . . . . . . . 102 Turning Off Subtotals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Repeating Row Headings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Retaining Formatting for Temporarily Removed Fields . . . . . . . . 104 Applying Formatting with the Format Painter . . . . . . . . . . . . . . . . 104 Grouping Dates Based on Source Data Formatting . . . . . . . . . . . 105 Changing Alignment for Merged Labels . . . . . . . . . . . . . . . . . . . . 106 Displaying Line Breaks in Pivot Table Cells . . . . . . . . . . . . . . . . . . 107 Showing Only the Top Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Freezing Heading Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Using the Always Display Items Option . . . . . . . . . . . . . . . . . . . . . 108 Applying Number Formatting to Page Fields. . . . . . . . . . . . . . . . . 108 Displaying Hyperlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Changing Total Label Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
ix
x
■CONTENTS
4.33. 4.34. 4.35. 4.36. 4.37. 4.38. 4.39. 4.40. 4.41. 4.42.
■CHAPTER 5
Changing Subtotal Label Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Formatting Date Field Subtotal Labels . . . . . . . . . . . . . . . . . . . . . . 110 Showing Additional Subtotals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Showing Subtotals for Inner Fields . . . . . . . . . . . . . . . . . . . . . . . . 111 Changing the Grand Total Label Text . . . . . . . . . . . . . . . . . . . . . . . 112 Changing Labels for Grand Totals . . . . . . . . . . . . . . . . . . . . . . . . . 112 Displaying Grand Totals at Top of Pivot Table . . . . . . . . . . . . . . . . 112 Hiding Grand Totals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Using a Worksheet Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Displaying Multiple Pivot Tables in a Dashboard . . . . . . . . . . . . . 113
Extracting Pivot Table Data
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
5.1. Using Drill to Details: Extracting Underlying Data . . . . . . . . . . . . 117 5.2. Using Drill to Details: Re-creating Source Data Table . . . . . . . . . 118 5.3. Using Drill to Details: Receiving Error Messages in a Non-OLAP Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 5.4. Using Drill to Details: Receiving the Saved Without Underlying Data Error Message . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 5.5. Using Drill to Details: Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . 120 5.6. Using Drill to Details: New Sheets Are Not Using the Worksheet Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 5.7. Using Drill to Details: Updating Source Data. . . . . . . . . . . . . . . . . 121 5.8. Using Drill to Details: Outputting Specific Fields . . . . . . . . . . . . . 121 5.9. Using Drill to Details: Preventing Sheet Creation . . . . . . . . . . . . . 122 5.10. Using Drill to Details: Deleting Created Sheets . . . . . . . . . . . . . . 122 5.11. Using GetPivotData: Automatically Inserting a Formula . . . . . . . 123 5.12. Using GetPivotData: Turning Off Automatic Insertion of Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 5.13. Using GetPivotData: Referencing Pivot Tables in Other Workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 5.14. Using GetPivotData: Using Cell References Instead of Text Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 5.15. Using GetPivotData: Using Cell References in an OLAP-Based Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 5.16. Using GetPivotData: Preventing Cell Reference Errors for Data_Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 5.17. Using GetPivotData: Preventing Errors in Data_Fields for OLAP-Based Pivot Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 5.18. Using GetPivotData: Extracting Data for Blank Field Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
■CONTENTS
5.19. Using GetPivotData: Preventing Errors for Missing Field Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 5.20. Using GetPivotData: Referencing Two Pivot Tables . . . . . . . . . . . 131 5.21. Using GetPivotData: Preventing Errors for Custom Subtotals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 5.22. Using GetPivotData: Preventing Errors for Date References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 5.23. Using GetPivotData: Referring to a Pivot Table . . . . . . . . . . . . . . 133 5.24. Using Show Pages: Creating Pivot Table Copies . . . . . . . . . . . . . 134 5.25. Using Show Pages: Creating Incorrect Sheet Names . . . . . . . . . 135 5.26. Using Show Pages: Not Creating Sheets for All Items . . . . . . . . . 136 5.27. Using Show Pages: Not Formatting New Sheets . . . . . . . . . . . . 137 5.28. Using Show Pages: Enabling the Show Pages Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
■CHAPTER 6
Modifying a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 6.1. 6.2. 6.3. 6.4. 6.5. 6.6. 6.7. 6.8. 6.9. 6.10. 6.11. 6.12. 6.13. 6.14. 6.15. 6.16. 6.17. 6.18.
Using Page Fields: Shifting Up When Adding Page Fields . . . . . 139 Using Page Fields: Arranging Fields Horizontally . . . . . . . . . . . . . 140 Using Page Fields: Hiding Entries in Page Field Item List . . . . . . 141 Using Page Fields: Hiding Page Field Items in OLAP-Based Pivot Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Using Page Fields: Including Hidden Items in Total . . . . . . . . . . . 142 Using Page Fields: Filtering for a Date Range . . . . . . . . . . . . . . . . 143 Using Page Fields: Filtering for Future Dates . . . . . . . . . . . . . . . . 143 Using Data Fields: Changing Content in the Data Area . . . . . . . . 144 Using Data Fields: Renaming Fields . . . . . . . . . . . . . . . . . . . . . . . . 144 Using Data Fields: Changing the “Total” Field Name . . . . . . . . . . 145 Using Data Fields: Arranging Horizontally . . . . . . . . . . . . . . . . . . . 146 Using Data Fields: Restoring Hidden Fields in the Data Field List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Using Data Fields: Fixing Source Data Number Fields . . . . . . . . 147 Using Data Fields: Showing the Source Text Instead of the Count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Using Pivot Fields: Adding Comments to Pivot Table Cells. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Using Pivot Fields: Showing Detail for Inner Fields . . . . . . . . . . . 149 Using Pivot Fields: Showing Detail for All Items in the Selected Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Using Pivot Fields: Showing Details in OLAP-Based Pivot Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
xi
xii
■CONTENTS
6.19. Using Pivot Fields: Changing Field Names in the Source Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 6.20. Using Pivot Fields: Clearing Old Items from Field Dropdown Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 6.21. Using Pivot Fields: Changing (Blank) Items in Row and Column Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 6.22. Using Pivot Items: Showing All Months for Grouped Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 6.23. Using Pivot Items: Showing All Field Items . . . . . . . . . . . . . . . . . . 154 6.24. Using Pivot Items: Hiding Items with No Data . . . . . . . . . . . . . . . 154 6.25. Using Pivot Items: Ignoring Trailing Spaces When Summarizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 6.26. Using a Pivot Table: Deleting the Entire Table . . . . . . . . . . . . . . . . 155 6.27. Using a Pivot Table: Changing the Automatically Assigned Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
■CHAPTER 7
Updating a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 7.1. Using Source Data: Locating and Changing the Source Excel List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 7.2. Using Source Data: Automatically Including New Data . . . . . . . 161 7.3. Using Source Data: Automatically Including New Data in an External Data Range . . . . . . . . . . . . . . . . . . . . . . . 162 7.4. Using Source Data: Moving the Source Excel List . . . . . . . . . . . . 163 7.5. Using Source Data: Changing the Source Excel List . . . . . . . . . . 164 7.6. Using Source Data: Locating the Source Access File . . . . . . . . . 165 7.7. Using Source Data: Trying to Change an OLAP Source . . . . . . . . 166 7.8. Using Source Data: Changing the Data Source Name File . . . . . 166 7.9. Using Source Data: Changing the Source Access File. . . . . . . . . 167 7.10. Using Source Data: Changing the Source for a Shared Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 7.11. Using Source Data: Changing the Source CSV File . . . . . . . . . . . 169 7.12. Refreshing When a File Opens . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 7.13. Preventing a Refresh When a File Opens . . . . . . . . . . . . . . . . . . . 171 7.14. Refreshing Every 10 Minutes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 7.15. Refreshing All Pivot Tables in a Workbook. . . . . . . . . . . . . . . . . . . 171 7.16. Reenabling the Refreshing External Data Message. . . . . . . . . . . 172 7.17. Problems Obtaining Data When Refreshing . . . . . . . . . . . . . . . . . 173 7.18. Stopping a Refresh in Progress. . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 7.19. New Data Doesn’t Appear When Refreshing an OLAP Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
■CONTENTS
7.20. Refreshing an OLAP Cube Causes Client Safety Options Error Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 7.21. Refreshing Pivot Tables with the Same Pivot Cache . . . . . . . . . 175 7.22. Refreshing Part of a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 7.23. Enabling Automatic Refresh. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 7.24. Refreshing a Pivot Table on a Protected Sheet . . . . . . . . . . . . . . . 176 7.25. Refreshing Automatically When Data Changes . . . . . . . . . . . . . . 177 7.26. Refreshing When Two Tables Overlap . . . . . . . . . . . . . . . . . . . . . . 177 7.27. Refreshing Creates an Error Message After Fields Are Deleted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 7.28. Refreshing Pivot Tables After Queries Have Been Executed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 7.29. Refreshing Creates a Too Many Row or Column Items Error Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 7.30. Refreshing a Scenario Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . 179
■CHAPTER 8
Securing a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 8.1. Using a Password-Protected Data Source . . . . . . . . . . . . . . . . . . 181 8.2. Using a Data Source: No Prompt for Password with OLAP Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 8.3. Using a Data Source: Access Database with User-Level Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 8.4. Protection: Preventing Changes to a Pivot Table . . . . . . . . . . . . . 185 8.5. Protection: Allowing Changes to a Pivot Table on a Protected Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 8.6. Protection: Refreshing or Creating a Pivot Table . . . . . . . . . . . . . 191 8.7. Privacy: Preventing Viewing of Others’ Data . . . . . . . . . . . . . . . . 191 8.8. Privacy: Disabling Drill to Details . . . . . . . . . . . . . . . . . . . . . . . . . . 192 8.9. Privacy: Disabling Show Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
■CHAPTER 9
Pivot Table Limits and Performance . . . . . . . . . . . . . . . . . . . . . . 195 9.1. Understanding Limits: 32,500 Unique Items with External Data Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 9.2. Understanding Limits: 32,500 Unique Items with Excel Data Source. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 9.3. Understanding Limits: Only the First 255 Items Displayed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 9.4. Understanding Limits: 8,000 Items in a Column Field . . . . . . . . 200 9.5. Understanding Limits: Too Many Row or Column Items . . . . . . . 201
xiii
xiv
■CONTENTS
9.6. Understanding Limits: Text Truncated in a Pivot Table Cell . . . . . . 203 9.7. Understanding Limits: Number of Records in the Source Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 9.8. Improving Performance When Changing Layout . . . . . . . . . . . . . 205 9.9. Improving Performance with the Optimize Memory Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 9.10. Reducing File Size: Excel Data Source. . . . . . . . . . . . . . . . . . . . . . 208
■CHAPTER 10 Publishing a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 10.1. Publishing a Pivot Table: Understanding HTML . . . . . . . . . . . . . . 211 10.2. Publishing Without Interactivity: Preparing the Excel File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 10.3. Publishing with Interactivity: Pivot Charts and Pivot Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 10.4. Publishing: Interactive Pivot Table Blocked . . . . . . . . . . . . . . . . . 219 10.5. Using AutoRepublish: The Don’t Show This Message Again Option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
■CHAPTER 11 Printing a Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 11.1. 11.2. 11.3. 11.4. 11.5.
Repeating Pivot Table Headings . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Setting the Print Area to Fit the Pivot Table . . . . . . . . . . . . . . . . . . 224 Compacting the Space Required for Row Labels . . . . . . . . . . . . 225 Printing the Pivot Table for Each Page Item . . . . . . . . . . . . . . . . . 226 Printing Field Items: Starting Each Item on a New Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 11.6. Printing Field Items: Keeping All Rows for an Item on One Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 11.7. Printing Field Items: Including Labels on Each Page . . . . . . . . . 229 11.8. Using Report Manager: Printing Pivot Table Data . . . . . . . . . . . . 230
■CHAPTER 12 Pivot Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 12.1. Stepping Through the Chart Wizard to Create a Pivot Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 12.2. Creating a Normal Chart from Pivot Table Data . . . . . . . . . . . . . . 236 12.3. Restoring Lost Series Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . 238 12.4. Adjusting Hidden Pie Chart Labels . . . . . . . . . . . . . . . . . . . . . . . . . 240 12.5. Formatting Category Axis Date Labels . . . . . . . . . . . . . . . . . . . . . . 241 12.6. Changing Pivot Chart Layout Affects Pivot Table . . . . . . . . . . . . . 242
■CONTENTS
12.7. 12.8. 12.9. 12.10.
Resizing and Moving Pivot Chart Elements . . . . . . . . . . . . . . . . . 242 Including Grand Totals in a Pivot Chart . . . . . . . . . . . . . . . . . . . . . 243 Converting a Pivot Chart to a Static Chart . . . . . . . . . . . . . . . . . . . 243 Using Page Fields: Page Fields with Hidden Items Shows (All) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
■CHAPTER 13 Programming a Pivot Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 13.1. 13.2. 13.3. 13.4. 13.5. 13.6. 13.7. 13.8. 13.9. 13.10. 13.11. 13.12. 13.13. 13.14. 13.15. 13.16. 13.17. 13.18. 13.19. 13.20. 13.21. 13.22. 13.23. 13.24. 13.25. 13.26. 13.27.
Using Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Recording a Macro While Printing a Pivot Table . . . . . . . . . . . . . . 247 Modifying Recorded Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Showing Top 10 Items over a Set Amount . . . . . . . . . . . . . . . . . . 253 Changing the Summary Function for All Data Fields . . . . . . . . . . 255 Hiding Rows with a Zero Total for Calculated Items . . . . . . . . . . 256 Hiding All Pivot Field Subtotals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Naming and Formatting the Drill to Details Sheet . . . . . . . . . . . . 259 Automatically Deleting Worksheets When Closing a Workbook. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Changing the Page Field Selection in Related Tables . . . . . . . . . 263 Clearing Old Items from Field Dropdown Lists . . . . . . . . . . . . . . . 264 Hiding All Items in a Pivot Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Changing Content in the Data Area . . . . . . . . . . . . . . . . . . . . . . . . 266 Identifying a Pivot Table’s Pivot Cache . . . . . . . . . . . . . . . . . . . . . . 267 Changing a Pivot Table’s Pivot Cache . . . . . . . . . . . . . . . . . . . . . . 269 Identifying the Query Used as the Data Source . . . . . . . . . . . . . . 270 Refreshing a Pivot Table on a Protected Sheet . . . . . . . . . . . . . . . 272 Refreshing Automatically When Source Data Changes . . . . . . . . 274 Preventing Selection of (All) in a Page Field . . . . . . . . . . . . . . . . . 274 Disabling Pivot Field Dropdowns . . . . . . . . . . . . . . . . . . . . . . 275 Preventing Layout Changes in a Pivot Table . . . . . . . . . . . . . . . . . 276 Preventing Changes to the Pivot Table . . . . . . . . . . . . . . . . . . . . . 277 Viewing Information on Pivot Caches . . . . . . . . . . . . . . . . . . . . . . . 279 Resetting the Print Area to Include the Entire Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Printing the Pivot Table for Each Page Field . . . . . . . . . . . . . . . . . 282 Reformatting Pivot Charts After Changing the Pivot Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Scrolling Through Page Field Items on a Pivot Chart. . . . . . . . . . 284
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
xv
About the Author ■DEBRA DALGLEISH is a computer consultant in Mississauga, Ontario, Canada, serving local and international clients. Self-employed since 1985, she has extensive experience in designing complex Excel and Access applications, as well as sophisticated Word forms and documents. She has led hundreds of Microsoft Office corporate training sessions, from beginner to advanced level. In recognition of her contributions to the Excel newsgroups, she has received the Microsoft Office Excel MVP award each year since 2001. You can find a wide variety of Excel tips and tutorials, and sample files, on her Contextures website: www.contextures.com/tiptech.html.
xvii
About the Technical Reviewer ■DON REAMEY is a Software Development Engineer for Microsoft’s Office Business Applications Group, where he works on applications that integrate line-of-business systems with Microsoft Office. Don has 16 years of experience in the software industry, with 10 of those years building C++ and Java applications for the financial industry. Don holds a bachelor of science degree in information systems from Pfeiffer University.
xviii
Acknowledgments M
any people helped me as I worked on this book. Above all, love and thanks to Keith, who has believed in me from the start and provided endless encouragement, and to Jason, Sarah, and Neven, for enduring months of stress. (It’s safe to come home now!) Thanks to Andy Pope for introducing me to the wonderful people at Apress: Dominic Shakeshaft, who helped develop the book’s concept; Ewan Buckingham, who created a structure for the book’s material; and Kylie Johnston, who kept everything on track. Thanks also to Don Reamey, for his insightful questions during the technical review; Liz Welch, who polished the text and ensured it was consistent; and my vigilant production editor, Katie Stence. Many thanks to Dave Peterson, from whom I’ve learned much about Excel programming, and who graciously commented on some of the code for this book. Thanks to Jon Peltier, who convinced me to start writing about pivot tables, and who is always willing to exchange ideas and humor, and to Tom Ogilvy, who generously shares his creative code. Thanks to all those who ask questions and provide answers in the Microsoft Excel newsgroups, and were the inspiration for many of the recipes in this book. Thanks to my clients, who remained patient as I juggled projects and writing, and who continue to challenge me with interesting assignments. Finally, thanks to my parents, Doug and Shirley McConnell, and my sister Nancy Nelson, for a lifetime of love and support.
xix
Introduction E
xcel’s pivot tables are a powerful tool for analyzing data. With only a few minutes of work, a new user can create an attractively formatted table that summarizes thousands of rows of data. This book assumes that you know the basics of Excel and pivot tables, and provides troubleshooting tips and techniques, as well as programming examples.
Who This Book Is For This book is for anyone who uses pivot tables, and who reads the manual only when all else fails. It’s designed to help you understand the advanced features and options that are available, as you need them. Experiment with pivot tables, and if you get stuck, search for the problem in this book. With luck, you’ll find a solution, a workaround, or, occasionally, confirmation that pivot tables can’t do what you want them to do.
How This Book Is Structured Chapters 1 to 12 contain manual solutions to common pivot table problems, and alert you to the situations where no known solution exists. Chapter 13 has sample code for those who prefer a programming solution to their pivot table problems. The following is a brief summary of the material contained in each chapter. • Chapter 1, “Creating a Pivot Table”: Topics include issues you should consider when planning a pivot table and preparing the source data; problems that occur when connecting to the source data; and understanding the pivot table options that are available. • Chapter 2, “Sorting and Grouping Pivot Table Data”: You’ll learn how data sorts in a pivot table, as well as how to create custom sort orders, show top items only, and group and ungroup numbers, dates, and text. • Chapter 3, “Calculations in a Pivot Table”: This chapter discusses using the summary functions and custom calculations, creating calculated items and calculated fields to expand the built-in capabilities, modifying formulas, and adjusting the solve order. xxi
xxii
■INTRODUCTION
• Chapter 4, “Formatting a Pivot Table”: You’ll learn about autoformatting a pivot table, applying and retaining formatting, creating custom number formats, and showing and hiding totals and subtotals. • Chapter 5, “Extracting Pivot Table Data”: Topics include using the Drill to Details feature to extract underlying records, using the GetPivotData worksheet function to return pivot table data, turning off the GetPivotData feature, and creating pivot table copies with the Show Pages feature. • Chapter 6, “Modifying a Pivot Table”: This chapter covers changing the pivot table layout, modifying field and item captions, clearing old items from the field dropdowns, adding comments to data cells, and customizing the PivotTable toolbar. • Chapter 7, “Updating a Pivot Table”: Topics include refreshing the pivot table, refreshing automatically, reconnecting to the source data, changing the source data, and creating a dynamic source data range. • Chapter 8, “Securing a Pivot Table”: This chapter discusses preventing users from changing the pivot table layout, connecting to a password-protected data source, using security features, and addressing privacy issues. • Chapter 9, “Pivot Table Limits and Performance”: This chapter covers understanding limits to pivot table field size, addressing memory issues, maximizing performance, and reducing file size. • Chapter 10, “Publishing a Pivot Table”: This chapter explains how to prepare a pivot table for publishing on a web page, with or without interactivity. • Chapter 11, “Printing a Pivot Table”: Topics include printing headings on every page, repeating row and column labels, adjusting the print area, and using the Report Manager to simplify printing. • Chapter 12, “Pivot Charts”: This chapter covers restoring lost formatting, creating normal charts from pivot tables, stepping through the Chart Wizard when creating a pivot chart, and modifying a pivot chart layout. • Chapter 13, “Programming a Pivot Table”: You’ll learn how to record and use macros, and edit recorded code. This chapter includes sample code for modifying and printing pivot tables and clearing old items from pivot field dropdown lists, and it discusses refreshing pivot tables on protected sheets, preventing layout changes, reformatting a pivot chart, and changing the pivot cache.
■CONTENTS
Prerequisites The solutions in this book are written for Microsoft Excel 2003. Most will work in Excel 2002, but may not adapt to earlier versions. A working knowledge of Excel is assumed, as well as familiarity with pivot table basics. Sample code is provided in Chapter 13, and some programming experience may be required to adjust the code to conform to your workbook setup. For an introduction to pivot tables, see A Complete Guide to PivotTables: A Visual Approach, by Paul Cornell (Apress, 2005).
Downloading the Code Sample workbooks and code are available for download from the Apress website.
Contacting the Author The author can be contacted at [email protected]. Visit her Contextures website at www.contextures.com.
xxiii
CHAPTER
1
■■■
Creating a Pivot Table E
ven though you’ve likely created many PivotTable reports in Microsoft Excel, you sometimes encounter problems while setting them up. Perhaps the layout isn’t as flexible as you’d like, or you have trouble connecting to the data source that you want to use. The PivotTable and PivotChart Wizard provides many options as you create the PivotTable report, and you want to understand the effect the different settings have. This chapter discusses the issues you can consider as you plan the PivotTable report, set up the source data, connect to the source, and select the PivotTable options.
1.1. Planning a Pivot Table: Getting Started Problem You’ve been asked to create a pivot table from your company’s sales data, and you aren’t sure what issues to consider before you create it.
Solution If you spend some time planning, you can create a pivot table that is easier to maintain and that clearly delivers the information your customers need. When planning a pivot table, you should consider several things: • What type of data is available as the source for the pivot table? • Where is the source data stored? • Will you need to share this information with others?
1
2
1.2 ■ PLANNING A PIVOT TABLE: ACCESSING THE SOURCE DATA
1.2. Planning a Pivot Table: Accessing the Source Data Problem You’ve been asked to create a pivot table from your company’s sales data, and you need to determine what type of source data you’ll use.
Solution In the first step of the PivotTable and PivotChart Wizard, you select a data source for your pivot table from the list of four source types: • Microsoft Excel list or database • External data source • Multiple consolidation ranges • Another PivotTable report or PivotChart report
Many pivot tables are created from a single Excel list, usually in the same workbook as the pivot table. Others are created from multiple Excel lists, or another source, such as a database query, online analytical processing (OLAP) cube, or an existing pivot table in the same workbook. To determine if the source data can be used in its current state, here are some things to consider.
Where Is the Raw Data Stored? To create a meaningful pivot table, you need current, accurate data. Is the raw data in your workbook updated by you on a regular basis? Or is the raw data stored elsewhere? If others are using the pivot table, and the data is not stored in the workbook, will they have access to the source data when they want to refresh the pivot table?
How Frequently Will the Raw Data Be Updated? If the raw data will be updated frequently, you may want a routine that automatically refreshes the pivot table when the workbook is opened. If the data is stored outside of the workbook, how will you be notified that the data has changed and that you need to refresh the pivot table?
1.4 ■ PLANNING A SHARED PIVOT TABLE
What Problems Might You, or Other Users, Have in Accessing the Raw Data? Does every user have access to the source data? Should there be a routine to refresh the pivot table on a regular basis? If the source data is password protected, will all users know the password?
1.3. Planning a Pivot Table: Source Data Fields Problem You’ve been asked to create a pivot table from your company’s sales data, and you need to know if the source data has all the fields you need to create your report.
Solution The source data may contain all the fields that you want in the pivot table. However, you may need to report on other fields. For example, if variance from plan to budget is required in the pivot table, is variance a field in the source data? If not, you’ll need some way to calculate that in the pivot table, or add it to the fields in the source data. If fields are missing from the source data, can they be calculated at the source, or will they be calculated in the pivot table? What is the effect of either option?
1.4. Planning a Shared Pivot Table Problem You’ve been asked to create a pivot table from your company’s sales data, and make the results available to other employees.
Solution If a pivot table will be shared with others, here are some things to consider.
Will All Users Need the Same Level of Detail? Some users may require a top-level summary of the data. For example, the senior executives may want to see a total per region for annual sales. Other users may require greater detail. The regional directors may want to see the data totaled by district, or by sales representative. Sales representatives may need the data totaled by customer, or by product number.
3
4
1.4 ■ PLANNING A SHARED PIVOT TABLE
If the requirements are varied, you may want to create multiple pivot tables, each one focused on the needs of a particular user group. If that’s not possible, you’ll want to create a pivot table that’s easy to navigate, and adaptable for each user group’s needs.
Is the Information Sensitive? Often, a pivot table is based on sensitive data. For example, the source data may contain sales results and commission figures for all the sales representatives. If you create a pivot table from the data, assume that anyone who can open the workbook will be able to view all the data. Even if you protect the worksheet, and the workbook, the data won’t be secure. Some passwords can be easily cracked, allowing the protection to be bypassed. This weakness is described in Excel’s Help files, under the heading, “About worksheet and workbook protection.” It includes the warning, “With enough time, users can obtain and modify all the data in a workbook, as long as they have access to it.” When requiring a password to open the workbook, use a strong password, as described in the Microsoft article “Implementing Guidelines for Strong Passwords,” at www.microsoft.com/ntserver/techresources/security/password.asp.
■Note A strong password contains a mixture of upper- and lowercase letters, numbers, and special characters (such as $ and %), and is at least six characters long.
For sensitive and confidential data, the pivot table should only be based on the data that each user is entitled to view. You can create multiple source lists, in separate workbooks, and create individual pivot tables from those. It will require more time to set these up, but it will be worthwhile to ensure that privacy concerns are addressed. You can use macros and naming conventions to standardize the source data and the pivot tables, and to minimize the work required to create the individual copies. Another option is to use secured network folders to store the workbook, where only authorized users can access the data. Also available in Excel 2003 is Information Rights Management, a file-protection technology that allows you to assign permissions to users or groups. For example, some users can have Read permission only, and won’t be able to edit, copy, or print the file contents. Other users, with Change permission, can edit and save changes. You can also set expiry dates for the permissions to limit access to a specific time period. To learn more about Information Rights Management, see Excel’s Help files, and check out “Information Rights Management in Office Professional Edition 2003” at www.microsoft.com/office/editions/prodinfo/technologies/irm.mspx. The Microsoft Office 2003 Security Whitepaper discusses the security technologies available in Excel, as well as other Office programs, at www.microsoft.com/technet/prodtechnol/office/office2003/ operate/o3secdet.mspx#EEAA.
33faf4ff068d72f2adcfa053cf4f7274
1.5 ■ PREPARING THE SOURCE DATA: USING AN EXCEL LIST
Will the Information Be Shared in Printed or Electronic Format? If the information will be shared in printed format only, the security issues are minimized. You can control what’s printed and issued to each recipient. If the information will be shared electronically, it’s crucial that confidential data not be included in any pivot table that’s being distributed to multiple users.
Will the Pivot Table Be in a Shared Workbook? Many features are unavailable in a shared workbook, including creating or changing a PivotTable report or chart. Users will be able to view your pivot table, but won’t be able to rearrange the fields or select different items from the dropdown lists. Also, protection can’t be changed in a shared workbook, so you can’t run macros that unprotect the worksheet, make changes, and then reprotect the worksheet.
Will Users Enable Macros in Your Workbook? If your pivot table requires macros for some functionality, will users have the ability to enable macros? In some environments, they may not be able to use macros. Will that have a serious impact on the value of your pivot table?
1.5. Preparing the Source Data: Using an Excel List Problem You want to create a pivot table from an Excel list. Before starting the PivotTable and PivotChart Wizard, you want to ensure that your database is in the required format.
Solution Probably the most common data source for a pivot table is an Excel list, in the same workbook as the pivot table. The list may contain only a few rows of records, or it may contain thousands of records. No matter what size the list is, there are common requirements when preparing to base a pivot table on the data.
How Should Your List Be Arranged in Order to Create a Pivot Table? • Your list should have a heading in each cell in the first row. • There should be no blank rows or columns within the list.
5
6
1.5 ■ PREPARING THE SOURCE DATA: USING AN EXCEL LIST
■Tip Select a cell in the list, then while holding down the Ctrl key, press the asterisk key (*) to select the current region. If the complete list isn’t selected, there are probably blank rows or columns within the list. Locate and delete them, or enter data in them.
• Each column should contain the same type of data. For example, one column may contain sales amounts in currency. Another column may contain region names in text. Another column may contain order dates. • If you plan to group the data in the pivot table, ensure that each row contains data in the fields you want to group. For example, enter an order date in each row.
■Tip If no date is available, you could create a dummy date for use in those rows. Make the dummy date obvious, so it won’t be confused with real data.
• Remove any total calculations at the top or bottom of the list, as well as any manually entered subtotals within the list. • Name the range so you can refer to that name when creating the pivot table. • If the list will grow or shrink, use a dynamic name for the range. • If the Subtotals feature is turned on in the list, remove the subtotals. Otherwise, the PivotTable and PivotChart Wizard will prevent you from creating the pivot table. • If the AutoFilter feature is turned on, you can leave it on. It won’t have any effect on creating the pivot table, and the pivot table will be based on all data, whether it’s hidden or visible. • If rows or columns have been manually hidden, you can leave them hidden. They won’t have any effect on creating the pivot table, and the pivot table will be based on all rows and columns, whether they’re hidden or visible.
■Tip If columns are hidden, check that they contain data in the heading cells, or you won’t be able to create a pivot table from the list.
1.8 ■ PREPARING THE SOURCE DATA: USING AN EXCEL LIST WITH MONTHLY COLUMNS
1.6. Preparing the Source Data: Excel List Invalid Field Names Problem You want to create a pivot table from an Excel list, but you get an error message about invalid field names.
Solution One or more of the heading cells in the Excel list may be blank, and to create a pivot table you need a heading for each column. In Step 2 of the PivotTable and PivotChart Wizard, check carefully to ensure that you haven’t selected extra columns. Also, check that there aren’t hidden columns within the source data range, as they may have blank heading cells.
1.7. Preparing the Source Data: Using a Filtered Excel List Problem You want to create a pivot table from a filtered Excel list, but all the records are included when you create the pivot table, instead of just the filtered items.
Solution A pivot table includes all the items from the source list, even if the list has an AutoFilter or Advanced Filter applied, hiding some of the rows. Instead of filtering the list in place, you could use an Advanced Filter to extract some records to another worksheet, and then base the pivot table on the extracted list.
1.8. Preparing the Source Data: Using an Excel List with Monthly Columns Problem Your Excel list has a column for each month, and you’re having trouble creating a flexible pivot table from this source data. Each month gets its own field button, and it’s difficult to get the layout you want in the pivot table, and to create annual totals.
7
8
1.8 ■ PREPARING THE SOURCE DATA: USING AN EXCEL LIST WITH MONTHLY COLUMNS
Solution In your Excel list, the column headings contain Pivot Field Item names, instead of Pivot Field names. For example, instead of a column heading Month, each month has its own heading, and the data for that month is entered there. This is a great way to create a summary report in your workbook but isn’t ideal for creating a pivot table. If you create a pivot table from the Excel list, you’ll need 12 data fields, one for each month, and will need another field to total the monthly data. You should rearrange the data, using actual dates, if available, or month names, in a single column, with the sales amounts all in one column. Instead of 13 columns (Product and one for each of the 12 months), the revised list will have three columns: Product, Month, and Amount. This will normalize the data and allow a more flexible pivot table to be created. For information on normalization, you can read the Microsoft Knowledge Base article “Description of the Database Normalization Basics” at http://support.microsoft.com/ kb/283878. The following technique will automate the normalization process for you. It creates a pivot table from the existing list, and combines all the Month columns into one field. Then, the Drill to Details feature is used to extract the source data in its one-column format.
■Tip If you have data for each item, for each month, you’ll increase the required rows 12-fold, so check that you won’t exceed Excel’s row limit before trying the following technique. If your normalized data will exceed Excel’s row limit, you could process the columns in two or more sessions, and store the data in an external database.
Assuming you have a simple list, with one column of product names, and 12 columns of monthly sales figures, follow these steps: 1. Select a cell in the list. 2. Choose Data ➤ PivotTable and PivotChart Report. 3. In Step 1, select Multiple Consolidation Ranges, select PivotTable as the type of report, and then click Next. 4. In Step 2a, select the I will create the page fields option, then click Next. 5. In Step 2b, select your list on the worksheet, then click the Add button. 6. Leave the other settings at their defaults, and click the Next button.
1.8 ■ PREPARING THE SOURCE DATA: USING AN EXCEL LIST WITH MONTHLY COLUMNS
■Tip The next step isn’t required, but will create a much smaller pivot table, in which only the Grand Total is visible in the data area.
7. In Step 3, click the Layout button, and in the Layout window, drag the Row and Column field buttons off the pivot table, leaving only the Sum of Value field in the data area. Then click OK. 8. Click Finish, and a pivot table appears in the workbook, with a PivotTable Field List that contains only three fields: Row, Column, and Value. 9. In the pivot table that was created, double-click on the Grand Total cell to drill down and create a list of underlying data.
■Tip You can use an AutoFilter to remove the rows with blank value cells. To filter the list, choose Data ➤ Filter ➤ AutoFilter. From the dropdown list in the Value column, choose Blanks. Delete the filtered rows, then choose Data ➤ Filter ➤ AutoFilter to remove the AutoFilter.
10. In the resulting list of data, rename the heading cells as Product, Month, and Amount.
■Tip This normalized list will be used as the source for your new pivot table. Make a backup copy of the file, then you can delete the original list and its pivot table. You can also delete the sheet that contains the pivot table used in Step 10.
11. Create a pivot table from the normalized list, with Product in the row area, Month in the column area, and Amount in the data area. Because there’s only one data field, the Row Grand Total will automatically sum the Months. In the old version of the pivot table, with 12 month fields, you had to create a calculated field to sum the months.
9
10
1.9 ■ PREPARING THE SOURCE DATA: USING AN EXCEL LIST WITH MONTHLY COLUMNS AND TEXT FIELDS
1.9. Preparing the Source Data: Using an Excel List with Monthly Columns and Text Fields Problem You’d like to use the normalization technique described in Section 1.8, but you have two columns with text, then your columns of monthly data. The normalization technique only works with one column of text data.
Solution If you have two or more text columns, you should concatenate them before using the normalization technique. For example, if you have columns for Name and Region, follow these steps: 1. Insert a blank column after Region, with the heading NameRegion. 2. In cell C2, enter the formula =A2 & "$" & B2 3. Copy the formula down to the last row of data. 4. Follow steps 1 to 10 in the normalization instructions from Section 1.8, using columns C:O as the source range for the pivot table. 5. In the resulting list of data, rename the heading cells as NameRegion, Month, and Amount. 6. Select column A (NameRegion), and move it to the right of the other columns. This will prevent it from overwriting the other columns when you separate Name and Region in the next step. 7. With the NameRegion column selected, choose Data ➤ Text to Columns. 8. Select Delimited, and click the Next button. 9. Select the Other option, and in the text box, type the $ sign. 10. Click Finish to split the text into two columns. 11. Add headings to the columns, and rearrange them, if desired. 12. Create a pivot table from the normalized list, with Name and Region in the row area, Month in the column area, and Amount in the data area.
1.11 ■ PREPARING THE SOURCE DATA: USING AN ACCESS QUERY WITH PARAMETERS
1.10. Preparing the Source Data: Using an Access Query Problem You want to create a pivot table from a Microsoft Access query, but you aren’t sure what fields to include in the query.
Solution In the Access query, include all the fields that you want in the pivot table. If you use lookup tables, include them in the query, and add the descriptive field names to the query output instead of using ID numbers or codes. For example, an OrderDetail table might include a product number. Another table (Products) in the database contains the information about each product number, such as the product name and color. In the query, add both tables, then join the Product number field in the two tables. In the query grid, include fields from the OrderDetail table, such as Quantity, and from the Products table, include descriptive fields, such as Product Name and Color. Also, in the Access query create calculated fields for any line calculations you want summarized in the pivot table, such as LineTotal:UnitsSold*UnitPrice.
■Caution Although they’re permissible within Microsoft Access, user-defined functions, and some built-in Access functions such as NZ, will create an error, for example, “Undefined function 'NZ' in expression,” when used outside of Access. For more information on the Jet SQL expressions that are used to return the data to Excel, see “Microsoft Jet SQL Reference” at http://office.microsoft.com/en-ca/assistance/ CH062526881033.aspx. For more information on Access queries, see “Queries” at http://office. microsoft.com/en-us/assistance/CH062526491033.aspx.
1.11. Preparing the Source Data: Using an Access Query with Parameters Problem You want to base your pivot table on a Microsoft Access query that contains parameters, but you get an error message saying you can’t use parameters.
Solution In Access, you can use parameters in the criteria row of a query, and you are prompted to enter the criteria when the query runs. However, you can’t create a pivot table that’s directly based on a parameter query. Instead, you can use page fields to filter the pivot table:
11
12
1.12 ■ PREPARING THE SOURCE DATA: USING A TEXT FILE
1. In Access, create a query without parameters. 2. In Excel, create a pivot table based on the query without parameters. 3. Place the field that you want to filter in the pivot table’s page area. 4. Select an item from the page field, and the pivot table will display the data for that item only.
1.12. Preparing the Source Data: Using a Text File Problem The accounting department can provide you with a text file of the year-to-date transactions, which you can use as a data source for your pivot table. They’ve asked how you want the file set up.
Solution You can use a delimited or fixed-width text file as the data source for a pivot file, but it’s usually easier to work with a delimited file. It requires only one setting to separate the fields. If using a fixed-width file, you have to specify the start position and length of each field. If possible, include field headings in the first row, and ensure that there is a line break character at the end of each record.
1.13. Preparing the Source Data: Using an OLAP Cube Problem You want to base your pivot table on an OLAP cube that you’ll create from data in your Access database. You aren’t sure what should be included in the Access query that will provide the source data for the cube.
Solution An online analytical processing (OLAP) database, also referred to as an OLAP cube, is a way of organizing a large amount of data. It organizes the database fields in levels and makes it more efficient for you to work with the data. For example, Date fields can be organized into levels such as Month, Quarter, and Year. A related set of levels is called a dimension. The data for each dimension is summarized, and can be used in your pivot table.
1.15 ■ PREPARING THE SOURCE DATA: USING MULTIPLE CONSOLIDATION RANGES
Each field can be used only once in the OLAP cube, so if you’ll need a field twice—for example, an Average and a Sum—you’ll need two copies of the field in the Access query. Include at least one field that is a measure (data to be summarized), and other fields that you can use in the row, column, or page area.
1.14. Preparing the Source Data: Creating an OLAP Cube Problem You want to base your pivot table on an OLAP cube, but you don’t know how to create one.
Solution In the PivotTable and PivotChart Wizard, if you select the External data source option in Step 1, you’ll have the opportunity to create an OLAP cube from your data source in the final step of the Query Wizard or in Microsoft Query. The OLAP Cube Wizard will guide you through the process.
1.15. Preparing the Source Data: Using Multiple Consolidation Ranges Problem You have an Excel list with each region’s sales on separate sheets in your workbook, and want to combine all the data into one pivot table. You created a pivot table and selected the Multiple consolidation ranges option in Step 1 of the PivotTable and PivotChart Wizard, but the pivot table doesn’t look like your other pivot tables, and you can’t get it to work correctly.
Solution Creating a pivot table from multiple consolidation ranges allows you to create a pivot table from data in two or more separate lists. However, the result is not the same as a pivot table created from a single Excel list. The first field is placed in the row area, the remaining field names are placed in the column area, and the values in those columns appear in the data area. You can hide or show the column items, and use the page fields to filter the data. However, there’s no setting you can change to create a pivot table from multiple consolidation ranges that’s a different structure than the standard result.
13
14
1 . 1 6 ■ P R E PA R I N G T H E S O U R C E D ATA : A LT E R N AT I V E S TO U S I N G M U LT I P L E C O N S O L I D AT I O N R A N G E S
1.16. Preparing the Source Data: Alternatives to Using Multiple Consolidation Ranges Problem The results aren’t satisfactory when you create a pivot table from multiple consolidation ranges. What are the alternatives?
Solution Instead of using multiple consolidation ranges, you may be able to cut and paste all the data onto one worksheet. If there are too many rows to fit on one Excel worksheet, consider storing the data in a database, and create a pivot table from the external source.
1.17. Preparing the Source Data: Setting Up Multiple Consolidation Ranges Problem You have too much data to store on one worksheet, you have no control over the source data, or you are unable to move the data to a database. You can’t use one of the alternatives, and have to create a pivot table from multiple consolidation ranges. What’s the best way to set up a pivot table from multiple consolidation ranges?
Solution First, ensure that all the ranges being used are identical in setup. Each list should have the same column headings, in the same order, and contain the same type of data. The ranges can contain different numbers of rows. The first column will be used as a row heading in the pivot table, so move the most important field to that position.
1.18. Preparing the Source Data: Benefits of Using Another PivotTable or PivotChart Report Problem You have to create a new pivot table from the same source as an existing pivot table. What are the benefits of basing a pivot table on another pivot table?
1.20 ■ PREPARING THE SOURCE DATA
Solution When you create a pivot table, Excel stores a copy of the source data in a memory area called a pivot cache. If you base a pivot table on another pivot table, it uses the same pivot cache as the first pivot table. This may result in a smaller file size, and less memory used in Excel. To view the memory used, you can check the list of processes in the Windows Task Manager, or, in Excel, enter the following formula on a worksheet: =INFO("MemUsed") This returns the number of bytes of memory being used for data.
1.19. Preparing the Source Data: Problems Caused by Using Another PivotTable or PivotChart Report Problem You have to create a new pivot table from the same source as an existing pivot table. What problems might you encounter if you base the new pivot table on the existing pivot table?
Solution If you group fields in one pivot table, those fields are also grouped in the other pivot table. This might cause unexpected layout changes in a pivot table. You won’t be able to refresh the pivot tables separately; if you refresh either pivot table, the other pivot table will automatically be refreshed.
1.20. Preparing the Source Data: Page Field Settings When Using Another PivotTable or PivotChart Report Problem You have to create a new pivot table from the same source as an existing pivot table in the same workbook. In Step 1 of the PivotTable and PivotChart Wizard, the option to use another PivotTable or PivotChart report is not available.
15
16
1.21 ■ CONNECTING TO THE SOURCE DATA: SELECTING A LARGE RANGE IN AN EXCEL LIST
Solution If you’ve set the page fields in a PivotTable to the Query external data source as you select each field option, you won’t be able to use the pivot table as a source for your new pivot table. If the pivot table is the only one in the active workbook, the Another PivotTable report or PivotChart report option will be disabled in the list of data sources (see Figure 1-1).
Figure 1-1. The PivotTable option is disabled in the PivotTable and PivotChart Wizard.
To change the setting, double-click each page field in the source pivot table, and click the Advanced button. Set the Page field options to Retrieve external data for all page field items.
1.21. Connecting to the Source Data: Selecting a Large Range in an Excel List Problem How can you select a large list easily when you’re setting the source range in the PivotTable and PivotChart Wizard?
1.22 ■ CONNECTING TO THE SOURCE DATA: USING ARROW KEYS IN AN EXCEL LIST
Solution While in the PivotTable and PivotChart Wizard, you can use the keyboard to select the range. For example, press Ctrl+Home to select the first cell on the worksheet.
■Note If Freeze Panes have been applied, the Ctrl+Home shortcut will activate the first cell in the unfrozen part of the window.
After you’ve selected the starting cell, press the Shift key. This will expand the selection as you use the navigation keys. To select cells to the right of the active cell, press the End key, then press the Right arrow key. To select cells below the active cell, press the End key, then press the Down arrow key. For example, to select a list that starts at the top left of the worksheet, follow these steps: 1. Press Ctrl+Home, to go to the top left of the worksheet. 2. Hold the Shift key, and press and release the End key, then the Right arrow key. 3. Hold the Shift key, and press and release the End key, then the Down arrow key.
1.22. Connecting to the Source Data: Using Arrow Keys in an Excel List Problem While selecting a worksheet range in the PivotTable and PivotChart Wizard, you aren’t able to use the arrow keys.
Solution Whether or not the arrow keys work depends on how you open the wizard. If you right-click on the PivotTable and choose PivotTable Wizard, the arrow keys won’t work. However, if you click the PivotTable button on the Pivot toolbar and choose PivotTable Wizard, or open the wizard from the Data menu, the shortcut keys will work when selecting the range.
17
18
1.23 ■ CONNECTING TO THE SOURCE DATA: INSTALLING DRIVERS FOR EXTERNAL DATA
1.23. Connecting to the Source Data: Installing Drivers for External Data Problem You want to base your pivot table on an external source, but you don’t know what drivers you need installed.
Solution To connect to external data, Microsoft Query must be installed on your machine, as well as the appropriate Open Database Connectivity (ODBC) drivers or data source drivers. Microsoft Office includes drivers for the following data sources: • Microsoft Excel • Microsoft Access • Microsoft SQL Server OLAP Services • SQL Server • Text file databases • dBASE • Microsoft FoxPro • Oracle • Paradox • Third-party providers
These drivers are installed when you install Excel. If your external data source isn’t supported by one of these ODBC drivers, you may be able to obtain a Microsoft Office– compatible ODBC driver, along with installation instructions, from the database vendor.
1.24 ■ CONNECTING TO THE SOURCE DATA: CREATING A NEW SOURCE FOR EXTERNAL DATA
1.24. Connecting to the Source Data: Creating a New Source for External Data Problem In Step 2 of the PivotTable and PivotChart Wizard, you clicked the Get Data button to open the Choose Data Source dialog box. You want to connect to an external data source that isn’t listed.
Solution In the Choose Data Source dialog box, both the OLAP cubes tab and the Database tab have a New Data Source option, which allows you to create additional data sources. You can save additional queries later in the dialog box, and they’ll be added to the Queries tab.
■Tip If you select an OLAP cube, then try to change it to a different data source, you’ll receive an error message. You can cancel out of the PivotTable and PivotChart Wizard, and restart.
To create a new data source in the Choose Data Source dialog box, follow these steps: 1. On the Database tab, select New Data Source, and click the OK button. 2. In the Create New Data Source dialog box, type a name for the data source. 3. In box 2, select a driver for the type of database that you will use as your data source. 4. Click the Connect button. 5. The next step will vary, depending on which driver you selected. For example, if you selected an Access driver, the ODBC Microsoft Access Setup dialog box appears, where you can click the Select button and select the database to which you want to connect. To change some of the settings, click the Advanced button, select an option in the list, and change its value.
19
20
1.25 ■ CONNECTING TO THE SOURCE DATA: EXCEL HANGS WHEN USING EXTERNAL DATA
■Note If using a text file to create the data source, you may see an error message, advising you that the INI file is corrupted. This can be resolved by obtaining a hotfix from Microsoft, as described in the following Microsoft Knowledge Base article: http://support.microsoft.com/kb/884038.
6. Click OK to return to the Create New Data Source dialog box. 7. In box 4, you can select a table as the default. 8. At the bottom of the dialog box, you can add a checkmark to store your user ID and password in the DSN file. This may compromise your database security, and you may prefer to manually enter this information when connecting to the database. 9. Click OK to return to the Choose Data Source dialog box.
1.25. Connecting to the Source Data: Excel Hangs When Using External Data Problem Excel hangs when you try to create a PivotTable from an external data source.
Solution Microsoft Query may be open in the background, waiting for you to respond. 1. Click the Microsoft Query button on the Windows Taskbar to activate the program. 2. In Microsoft Query, click the Return Data button.
This should close Microsoft Query, and allow you to continue working in Excel.
1.26 ■ CONNECTING TO THE SOURCE DATA: USING PAGES WITH MULTIPLE CONSOLIDATION RANGES
1.26. Connecting to the Source Data: Using Pages with Multiple Consolidation Ranges Problem You have each salesperson’s orders listed on a separate worksheet, and you’re creating a pivot table from all the data. When you create a pivot table from multiple consolidation ranges, you have the option to create page fields, but you aren’t sure how they work.
Solution In the PivotTable and PivotChart Wizard, after you select Multiple Consolidation Ranges as the data source, Step 2a asks, “How many page fields do you want?” You can let Excel create one page field, or you can create the page fields yourself.
Create a Single Page Field for Me If you select this option, one page field is created automatically. In Step 2b of the PivotTable and PivotChart Wizard, you aren’t presented with any options for creating the page fields. In the completed pivot table, there’s one page field, and each range in the multiple consolidation ranges is represented as a numbered item—for example, Item1, Item2, and Item3. This makes it difficult to determine which data you’re viewing when you select one of the items from the dropdown list. However, if you’re more interested in the total amounts than in the individual ranges, this is a quick way to create the page field.
I Will Create the Page Fields If you select this option, you can create the page fields in Step 2b of the PivotTable and PivotChart Wizard. To create the page fields, follow these steps: 1. In Step 2b, select each range, and add it to the All ranges list. 2. Select the number of page fields that you want to create (zero to four). In this example, there will be two page fields. 3. In the All ranges list, select the first range. 4. You’ll use the first page field to show the salesperson names. In the dropdown list for Field one, type the name of the person whose range you have highlighted in the list.
21
22
1.26 ■ CONNECTING TO THE SOURCE DATA: USING PAGES WITH MULTIPLE CONSOLIDATION RANGES
5. Each salesperson works in one of your sales regions, and you’ll use the second page field to show the Region names. In the dropdown list for Field two, type the region name for the person whose range you have highlighted in the list, as shown in Figure 1-2.
Figure 1-2. Create page fields for multiple consolidation ranges.
6. Select the next range in the All ranges list, and repeat Steps 4 and 5. Continue until all the ranges have page labels.
■Tip After you have created a label, you can select that label from the dropdown list for subsequent entries.
7. Click Next, then click Finish to close the PivotTable and PivotChart Wizard. 8. In the pivot table, the page fields that you created have numbered buttons, Page1 and Page2. You can change the button labels by typing over them. Select the cell that has the Page1 button, and type Salesperson, then select the cell that has the Page2 button, and type Region.
1.28 ■ UNDERSTANDING PIVOTTABLE OPTIONS: TABLE NAMING RULES
1.27. Understanding PivotTable Options: Table Name Problem Excel names your pivot tables with meaningless names like PivotTable1. You’d like to name the pivot table when you’re creating it.
Solution In the last step of the PivotTable and PivotChart Wizard, click the Options button to set general options for the pivot table that you’re creating. At the top of the PivotTable Options dialog box is the Name box. You can refer to this to see the name that was automatically assigned when the pivot table was created. You can change the name to something more meaningful—for example, Sales Pivot instead of PivotTable1.
1.28. Understanding PivotTable Options: Table Naming Rules Problem What are the rules for naming a pivot table?
Solution Although the naming rules for other items, such as macros or worksheets, are somewhat strict, the rules for naming pivot tables are quite flexible. For example, a worksheet name is limited to a maximum of 31 characters, and can’t include characters such as a question mark or colon. However, a pivot table name can include these characters, or the name could even be a single space character.
■Warning While you can include these characters in a pivot table name, I suggest you avoid them, as well as other characters such as slashes, square brackets, and asterisks, as they might cause problems when you’re programming.
23
24
1.29 ■ UNDERSTANDING PIVOTTABLE OPTIONS: ROW AND COLUMN GRAND TOTALS
1.29. Understanding PivotTable Options: Row and Column Grand Totals Problem Sometimes Row and Column Grand Totals appear and sometimes they don’t. What controls them?
Solution For pivot tables, Row and Column Grand Totals are turned on by default. If you have at least one field in the row area, the Column Grand Totals will appear. If you have at least one field in the column area, the Row Grand Totals will appear. In some pivot tables, these totals are meaningless and should be turned off. In other pivot tables, you can leave them on, or turn them off to simplify the layout. To turn them off, in the last step of the PivotTable and PivotChart Wizard, click the Options button and remove the checkmark from Row Grand Totals and/or Column Grand Totals.
1.30. Understanding PivotTable Options: AutoFormat Table Problem What does the AutoFormat Table option do? Even when it’s off, the pivot table seems to format itself.
Solution If the AutoFormat Table option is on, the default PivotTable format will be applied to your pivot table. You can turn this feature off, or apply other AutoFormats to the pivot table, by choosing Format ➤ AutoFormat. However, you may find that the formatting is very persistent. You can turn off the AutoFormat option, format the pivot table with no borders or fill color, but when you refresh the pivot table or rearrange the layout, some of the cell borders and fill color will return. If you want the column widths to remain a specific size, turn off the AutoFormat option. Otherwise, when you select a different item from the page field dropdown lists, the column widths will change to match the width of the selected item.
1.32 ■ UNDERSTANDING PIVOTTABLE OPTIONS: MERGE LABELS
1.31. Understanding PivotTable Options: Subtotal Hidden Page Items Problem What does the Subtotal hidden page items option do?
Solution You can hide page items by double-clicking on the page field button and selecting items in the Hide items list. Normally, these items are not included in the pivot table subtotals or grand totals. When you turn on the Subtotal hidden page items option, the items are included. Also, when page items are hidden, and you select the (All) item from the dropdown list, the cell displays (Multiple Items). If Subtotal hidden page items is on, the cell displays (All).
■Note This option is not available for OLAP-based pivot tables.
1.32. Understanding PivotTable Options: Merge Labels Problem Is there any benefit to using the Merge Labels option?
Solution If this option is turned on, cells for the outer level row and column heading labels are merged. Normally, if you select a cell in the label area, only that cell is selected, indicating that the cells are not merged. If you turn on the Merge labels option, and select a cell in the label area, the entire range of cells is selected, indicating that the cells are merged. Changing this option is the only way to merge the cells in a pivot table. You can’t manually select and merge, or unmerge, them.
25
26
1.33 ■ UNDERSTANDING PIVOTTABLE OPTIONS: MERGE LABELS FORMATTING
■Note All outer fields are affected—you can’t apply this setting to specific fields.
The most common reason for using this feature is to have the outer field labels centered vertically. However, a side effect of turning this option on is that the merged labels, along with the related subtotals, are vertically and horizontally centered instead of left and bottom aligned.
1.33. Understanding PivotTable Options: Merge Labels Formatting Problem If you try to change the cell alignment of an outer field or its subtotal label when the Merge labels option is on, you’ll receive an error message (see Figure 1-3).
Figure 1-3. Error message that appears when you try to format a merged label
Solution To overcome this limitation, you can select a cell, click the Increase Indent button on the Formatting toolbar, then click the Decrease Indent button on the Formatting toolbar. This will change the cell’s alignment to left aligned.
■Note Other alignment options, such as right aligned or rotated text, are not available for outer field labels and their subtotals when the Merge labels option is on.
1.35 ■ UNDERSTANDING PIVOTTABLE OPTIONS: REPEAT ITEM LABELS ON EACH PRINTED PAGE
1.34. Understanding PivotTable Options: Preserve Formatting Problem When you refresh the pivot table, formatting that you’ve applied is removed.
Solution If the Preserve Formatting option is on, you can apply formatting to cells in the pivot table, and it should be retained when the pivot table is refreshed or the layout changed.
1.35. Understanding PivotTable Options: Repeat Item Labels on Each Printed Page Problem You have created a large pivot table, and when you print it, the column and row headings don’t print on each page when a field is continued on subsequent pages.
Solution You can turn on the Repeat item labels on each printed page option if you’re printing a large pivot table, and the outer headings are continued on multiple pages. For example, with the row fields Region, SalesRep, and Product, the rows for a large region may fill more than one page. If this option is not on, the Region name would print with its first SalesRep name. On the second, and subsequent pages, the Region name wouldn’t appear. However, if the option is on, the relevant Region name would appear at the top of each page of the printed pivot table.
■Note If the Merge labels option is on, the Repeat item labels on each printed page option has no effect. Row item labels are printed only once, near the center of the merged field label.
27
28
1.36 ■ UNDERSTANDING PIVOTTABLE OPTIONS: PAGE LAYOUT
1.36. Understanding PivotTable Options: Page Layout Problem You have several page fields, and they use a great deal of space at the top of the worksheet, which makes it harder to work with your pivot table.
Solution The Page layout option, in conjunction with the Fields per Column/Row option, allows you to make better use of this space. Instead of one tall column of page fields, you can have multiple, smaller stacks. The Page layout option controls how the page fields are placed in the multiple stacks, and provides two choices: Down, then Over, or Over, then Down.
■Tip With either option, the new arrangement will have the same ending row as the original page area. You can delete rows above the new page area to reduce the amount of space at the top of the worksheet.
How It Works Let’s look at the two options for Page layout.
Down, then Over When Down, then Over is selected, the option that follows is Fields per column. In the Fields per column box, type a number greater than zero, and the page fields are rearranged into columns, with the specified number of page fields in each column. With this page layout option, page fields fill down for the number of fields specified in the Fields per column option, then start filling the next column of fields.
■Note If the Fields per column option is set at zero, the page fields are arranged in a single column.
For example, with five page fields, if you opt to have two fields per column, page fields one and two will be in the first column of page fields, three and four will be in the next column, and five will be in the third column.
1.38 ■ UNDERSTANDING PIVOTTABLE OPTIONS: ERROR VALUES
Over, then Down When Over, then Down is selected, the option that follows is Fields per row. In the Fields per row box, type a number greater than zero, and the page fields are rearranged into rows, with the specified number of page fields in each row. With this page layout option, page fields fill across for the number of fields specified in the Fields per row option, then start filling the next row of fields.
■Note If the Fields per row option is set at zero, the page fields are arranged in a single row.
For example, with five page fields, if you opt to have two fields per row, page fields one and two will be in the first row of page fields, three and four will be in the next row, and five will be in the third row.
1.37. Understanding PivotTable Options: Fields per Column/Fields per Row Problem Your PivotTable has several page fields, and they use a great deal of space at the top of the worksheet, which makes it harder to work with your pivot table. You’d like to arrange the page fields horizontally instead of vertically so that they use fewer rows.
Solution The name of this option depends on the selection made in the Page layout option above it. Type or select a number, between 0 and 255, in the Fields per column/Fields per row box to control the page field layout. If the Fields per column option is set at zero, the page fields are arranged in a single column. If the Fields per row option is set at zero, the page fields are arranged in a single row.
1.38. Understanding PivotTable Options: Error Values Problem Your pivot table is showing error values, and you’d rather have the cells show as blank.
29
30
1.39 ■ UNDERSTANDING PIVOTTABLE OPTIONS: EMPTY CELLS
Solution By default, error values are displayed in a pivot table. For example, if you create a calculated item, the pivot table may show a #DIV/0! error if it has to divide by zero. You can use the For error values, show option to display something other than the error value.
■Note This setting only affects cells in the data area of the pivot table. If error values in the source data appear in the row, column, or page area, they will not be replaced.
To turn the option on, add a checkmark in the check box. Then, in the text box, type the value you want in the error cells. You can use text, or characters such as dashes, to replace the errors. For example, you could replace errors with two hyphen characters (--) or with the text N/A.
■Note If you use a single hyphen in the text box, the error values will be replaced with zeros.
Up to 255 characters are allowed, but a short message is best in most cases.
■Tip If you’d like the error cells to appear blank, leave the For error values, show text box empty.
1.39. Understanding PivotTable Options: Empty Cells Problem Your pivot table has empty cells in the data area, and you’d like to use dashes instead.
Solution By default, empty cells in the data area are left blank. If your pivot table’s row area has a Regions field, and your pivot table’s column area has a Products field, there may be blank cells in the data area if some products were not sold in some regions. You can use the For empty cells, show option to display something other than the blank cells.
1.40 ■ UNDERSTANDING PIVOTTABLE OPTIONS: SET PRINT TITLES
■Note This setting only affects cells in the data area of the pivot table. If empty cells in the source data appear in the row, column, or page area as (blank), they will not be replaced.
To turn the option on, add a checkmark in the check box. Then, in the text box, type the value you want in the empty cells. You can use text, or characters such as dashes, to replace the errors. For example, you could replace empty cells with two hyphen characters (--) or with the text N/A. Up to 255 characters are allowed, but a short message is best in most cases.
■Note If you turn off the For empty cells, show option, or use a single hyphen in the text box, the empty cells will be filled with zeros.
1.40. Understanding PivotTable Options: Set Print Titles Problem You want to print the pivot table with the row and heading columns on every page.
Solution Turn on the Set print titles option, and the page, row, and column headings will appear on every sheet when you print the pivot table. You can rearrange the pivot table in any layout without manually resetting the print titles, and the headings will always appear on the printed sheets.
■Note For best results when using the Set print titles option, the worksheet shouldn’t contain other data.
1. Before you turn on this option, choose File ➤ Page Setup. 2. On the Sheet tab, clear the Rows to repeat at top and Columns to repeat at left boxes.
31
32
1.41 ■ UNDERSTANDING PIVOTTABLE OPTIONS: MARK TOTALS WITH *
■Caution If there is an entry in either of these boxes, the Set print titles option won’t be applied. If you clear the Print titles settings, on the Sheet tab in the Page Setup dialog box, after turning on the Set print titles option, you’ll have to turn the Set print titles option off, then turn it back on, for it to take effect.
3. Click OK to close the Page Setup dialog box. 4. Right-click on a cell in the PivotTable, choose Table Options, and add a checkmark to Set print titles.
■Tip If you clear the Print titles settings after turning on the Set print titles option, you’ll have to turn the Set print titles option off, then turn it back on, for it to take effect.
1.41. Understanding PivotTable Options: Mark Totals with * Problem The Mark totals with * option isn’t available for my pivot table.
Solution If your pivot table is based on a non-OLAP data source, the Mark Totals with * option isn’t available. If your pivot table is based on an OLAP source, each total has an asterisk to indicate that the amount includes both visible and hidden items. To remove the asterisks, clear the check box for this option. If the option is disabled for a pivot table based on an OLAP source, click the Include Hidden Items in Totals button on the PivotTable toolbar.
1.42. Understanding PivotTable Options: Save Data with Table Layout Problem What are the benefits of using the Save data with table layout option?
1.43 ■ UNDERSTANDING PIVOTTABLE OPTIONS: ENABLE DRILL TO DETAILS
Solution When using an external source, you can specify whether or not to save the data with the table layout. If you turn this option off, the file will be smaller, and may open, close, and save faster. However, you’ll have to refresh the pivot table before you can use it. If you turn this option on, the file size will be larger, and it may open, close and save more slowly. However, you don’t have to refresh the pivot table in order to use it. If storage space isn’t a concern, I’d recommend turning this option on. For example, a sample file saved with data was 1125KB, and the same file saved without data was 343KB. The difference in file opening and closing times was indiscernible, but the pivot table with the Save data with table layout option turned off had to be refreshed before it was used.
1.43. Understanding PivotTable Options: Enable Drill to Details Problem What are the benefits of using the Enable drill to details option?
Solution This option allows users to double-click on a pivot table cell, and show or hide detail. If you double-click a data cell, a new sheet is inserted in the workbook, containing the data from the source table, in the same field layout as the source. The formatting for AutoFormat List 3 is applied to the detail data.
■Note This option isn’t available for OLAP-based pivot tables.
How It Works When the Enable drill to details option is turned on, if you double-click a row or column cell, details for inner fields are shown or hidden. If the field has no inner fields, a dialog box appears, asking you to select from a list of the pivot table’s fields. The selected field is added to the pivot table as an inner field of the double-clicked field, and the details for the double-clicked cell are shown. If you double-click a row or column heading cell for which the inner field items are visible, the inner field items are hidden.
33
34
1.44 ■ UNDERSTANDING PIVOTTABLE OPTIONS: REFRESH ON OPEN
If you turn off the Enable drill to details option, and users double-click on a cell in the pivot table’s data area, they’ll see an error message that warns them the pivot table cannot be changed (see Figure 1-4).
Figure 1-4. The error message warning users the pivot table cannot be changed
1.44. Understanding PivotTable Options: Refresh on Open Problem What are the benefits of using the Refresh on open option?
Solution Turn this option on if you want the pivot table to be automatically refreshed when the workbook is opened. This should ensure that users don’t mistakenly view outdated data, but may slow the opening of the workbook. For pivot tables based on an external source, if this option is on, when you open the workbook you may see an alert message advising that queries may be harmful (see Figure 1-5). Whether you see the message depends on your Excel security level.
Figure 1-5. The Refresh on Open message
1.46 ■ UNDERSTANDING PIVOTTABLE OPTIONS: SAVE PASSWORD
If others will be using your pivot table workbook, you can warn them that this message will appear, and explain why, so they’ll be comfortable enabling the query refresh. Otherwise, they may see stale data, and could make incorrect decisions based on that data.
1.45. Understanding PivotTable Options: Refresh Every n Minutes Problem What are the benefits of using the Refresh every n minutes option?
Solution Turn this option on if you want the pivot table to be automatically refreshed at a specific interval while the workbook is open. You can use this option if the source data changes frequently, and it’s important that the pivot table reflect the latest available data.
■Note This option is only available for pivot tables with an external data source.
1.46. Understanding PivotTable Options: Save Password Problem What are the benefits of using the Save password option?
Solution If the pivot table’s external data source requires a password, you can turn on this option to store the password with the pivot table. However, security concerns may deter you from using this option. If you turn this option off, you’ll be prompted for the password during the first refresh in a session with the pivot table workbook (see Figure 1-6).
35
36
1.47 ■ UNDERSTANDING PIVOTTABLE OPTIONS: BACKGROUND QUERY
Figure 1-6. Password prompt for an external data source
1.47. Understanding PivotTable Options: Background Query Problem What are the benefits of using the Background query option?
Solution If you turn this option on, you can continue to work while Excel runs the query for the external data source. However, if you are refreshing the pivot table as part of running a series of macros, you may need to wait until the pivot table is refreshed before continuing with the remainder of the code. In that case, leave the option off to ensure that the pivot table is refreshed before other operations occur.
■Note This option isn’t available for OLAP-based pivot tables.
1.48. Understanding PivotTable Options: Optimize Memory Problem Your pivot table is based on an external data source, and you wonder if you should use the Optimize memory option.
Solution
■Note This option isn’t available for OLAP-based pivot tables.
1.49 ■ UNDERSTANDING PIVOT TABLE LAYOUT
You can leave this option turned off, unless you receive an error that warns of insufficient memory when working with the PivotTable report. When the option is turned on, refreshing the pivot table is much slower. For example, with a pivot table based on an Access query with 30,000 records, the pivot table refreshed in under 3 seconds with the Optimize memory option turned off. When the option was turned on, the refresh took over 10 seconds.
How It Works This option was introduced in Excel 97 to address memory problems that occurred when creating pivot tables in earlier versions of Excel. When the option is on, Excel queries the external data if the pivot table is refreshed, or the layout is changed, to determine how many unique items are in each field. If there are 256 or fewer items, it can optimize the storage for that field. However, running the queries has a negative impact on the pivot table performance, and the Optimize memory option should only be turned on if memory errors occur.
1.49. Understanding Pivot Table Layout Problem In Step 3 of the PivotTable and PivotChart Wizard, should you click the Layout button, and create the layout there, or should you use the worksheet to create the pivot table layout?
Solution Create the layout on the worksheet if field names are long, or there are several fields—it’s easier to see the fields in the field list than in the wizard. Use the Layout dialog box in the PivotTable and PivotChart Wizard if field names are short and there aren’t too many fields; it may be quicker because the pivot table isn’t created until you leave the PivotTable and PivotChart Wizard.
■Tip Place fields with more items in the row area, where they can fill down, rather than in the column area, where they will fill across. Even if the number of items doesn’t exceed the number of columns on the worksheet, it’s usually easier to absorb information in a narrower layout.
37
CHAPTER
2
■■■
Sorting and Grouping Pivot Table Data A
s you analyze data in a pivot table, you may want to rearrange the items in the Row and Column fields, or sort the summarized data, to focus on products that are selling the best, or regions that are doing poorly. Sort the fields or the data to move the important information to the top.
2.1. Sorting a Pivot Field: One Row Field Problem Your pivot table contains the field Product in the row area, and the field Sum of Sold in the data area, as Figure 2-1 shows. You want to sort the data in descending order, so the products with the highest revenue appear at the top of the pivot table.
Figure 2-1. The Product row field and Sum of Sold data field
39
40
2.1 ■ SORTING A PIVOT FIELD: ONE ROW FIELD
Solution You can quickly sort the data by using a toolbar button, or you can change the Product field settings. Quickly Sorting by the Sum of Sold Data 1. Select a cell in the data area in the Sum of Sold column. 2. On the Standard toolbar, click the Sort Descending button (Z-A).
■Note This technique sets the Product field to Manual sort. If you refresh the pivot table, the Sum of Sold column will not automatically re-sort, so the data may no longer be in descending order. You might have to click the Sort Descending button again to see data in the correct order.
Setting the Sort Order for the Product Field 1. Right-click on the Product field button, and choose Field Settings. 2. Click the Advanced button to open the PivotTable Field Advanced Options dialog box. 3. Under AutoSort options, select Descending. 4. From the Using field dropdown list, select the Sum of Sold field (see Figure 2-2).
Figure 2-2. The Using field dropdown list
2.2 ■ SORTING A PIVOT FIELD: OUTER ROW FIELD
5. Click OK to close the PivotTable Field Advanced Options dialog box, and click OK to close the PivotTable Field dialog box.
■Note With this AutoSort option selected, the Product field will automatically re-sort in descending order when the pivot table is refreshed.
2.2. Sorting a Pivot Field: Outer Row Field Problem There are two fields in the row area of your pivot table: Salesperson and Category, as shown in Figure 2-3. Salesperson, the outer row field, is sorted alphabetically, and you want to sort the salespeople in descending order by their total sales.
■Note If a pivot table has more than one field in the row area, the field that’s closest to the data area is the inner field. All the remaining row fields are outer fields. In Figure 2-3, Salesperson is the outer row field, and Category is the inner row field.
Figure 2-3. Salesperson is the outer row field and Category is the inner row field.
41
42
2.2 ■ SORTING A PIVOT FIELD: OUTER ROW FIELD
Solution To sort the outer field in descending order by subtotal, follow these steps: 1. Double-click the field button for the outer field in the row area—the Salesperson field in this example. 2. In the PivotTable Field dialog box that opens, click the Advanced button. 3. In the PivotTable Field Advanced Options dialog box, under the AutoSort options, select Descending. 4. From the Using field dropdown list, select the field on which you want to base the sort—Sum of Sold in this example. 5. Click OK to close the PivotTable Field Advanced Options dialog box, and click OK to close the PivotTable Field dialog box.
This will sort the Salesperson subtotals in descending order.
■Tip The subtotals do not have to be visible in order to sort by them.
How It Works When you do a descending sort, values are sorted in the following order: 1. Error values such as #N/A and #NAME? 2. Logical values (TRUE comes before FALSE) 3. Text, in the following order: Z Y X W V U T S R Q P O N M L K J I H G F E D C B A > = < + ~ } | { ` _ ^ ] \ [ @ ? ; : / . , * ) ( & % $ # " ! (space) 9 8 7 6 5 4 3 2 1 0
■Tip Hyphens and apostrophes are ignored, except where two items are the same except for a hyphen. In that case, in a descending sort the item with the hyphen is sorted first.
4. Numbers (including dates, which Excel stores as numbers) 5. Blank cells
2.3 ■ SORTING A PIVOT FIELD: INNER ROW FIELD
■Note Unlike a worksheet sort, where error values are treated equally, error values in a pivot table are sorted alphabetically.
2.3. Sorting a Pivot Field: Inner Row Field Problem There are two fields, Salesperson and Customer, in the row area of your pivot table. You want to sort the inner row field, Customer, in descending order, to highlight the best customers for each salesperson, based on Sales Dollars.
Solution When you sort the inner row field (Customer), the results will be sorted within the related outer field (Salesperson). The order of the outer fields won’t be affected by sorting the inner field. 1. Double-click the field button for the inner field—the Product field in this example. 2. In the PivotTable Field dialog box that opens, click the Advanced button. 3. In the PivotTable Field Advanced Options dialog box, under the AutoSort options, select Descending. 4. From the Using field dropdown list, select the field on which you want to base the sort—Sum of Sales Dollars in this example. 5. Click OK to close the PivotTable Field Advanced Options dialog box, and click OK to close the PivotTable Field dialog box.
The order of the items in the Salesperson field doesn’t change, but the Customers for each Salesperson are sorted in descending order.
How It Works In a pivot table, when you do an ascending sort, values are sorted in the following order: 1. Numbers (including dates, which Excel stores as numbers) 2. Text, in the following order: 0 1 2 3 4 5 6 7 8 9 (space) ! " # $ % & ( ) * , . / : ; ? @ [ \ ] ^ _ ` { | } ~ + < = > A B C D E F G H I J K L M N O P Q R ST UVW XY Z
43
44
2.4 ■ SORTING A PIVOT FIELD: RENAMED NUMERIC ITEMS
■Tip Hyphens and apostrophes are ignored, except where two items are the same except for a hyphen. In that case, in an ascending sort the item with the hyphen is sorted last.
3. Logical values (FALSE comes before TRUE) 4. Error values such as #N/A and #NAME? 5. Blank cells
2.4. Sorting a Pivot Field: Renamed Numeric Items Problem You accidentally renamed an item, changing it from 3.00 to 3.01, and now it’s not sorting correctly. When you refresh the pivot table, the item doesn’t change back to its original value.
Solution If you rename a numeric item in the pivot table, it becomes text, and is sorted after the numbers (or before the numbers in a descending sort). To change the entry back to a number, follow these steps: 1. Drag the numeric field out of the pivot table. 2. Refresh the pivot table. 3. Drag the numeric field back to the pivot table.
2.5. Sorting a Pivot Field: New Items Out of Order Problem You added several new products to your pivot table source data, and when you refreshed the pivot table, the new products appear at the end of the dropdown list.
2.6 ■ SORTING A PIVOT FIELD: SORTING ITEMS GEOGRAPHICALLY
Solution If the Product field is set to Manual sort, new items will appear at the end of the dropdown list. If you sort the field, the dropdown list will also be sorted. Follow these steps to sort the field: 1. Select a cell in the Product field. 2. Click the Sort Ascending (A-Z) button on the Excel Standard toolbar.
If you want new products to automatically appear in alphabetical order, you can set the Products field to AutoSort: 1. Double-click the Product field button. 2. In the PivotTable Field dialog box that opens, click the Advanced button. 3. In the PivotTable Field Advanced Options dialog box, under the AutoSort options, select Ascending. 4. From the Using field dropdown list, select Product. 5. Click OK to close the PivotTable Field Advanced Options dialog box, and click OK to close the PivotTable Field dialog box.
2.6. Sorting a Pivot Field: Sorting Items Geographically Problem You have a Regions field in the row area, and you would like the items sorted geographically (east to west) instead of alphabetically.
Solution In Excel, you can create custom lists, similar to the built-in lists of weekdays and months. The custom list can be based on a worksheet list, or typed in the Options dialog box.
45
46
2.6 ■ SORTING A PIVOT FIELD: SORTING ITEMS GEOGRAPHICALLY
Creating a Custom List 1. Choose Tools ➤ Options. 2. On the Custom Lists tab, select the NEW LIST item. 3. Click in the List entries box and type your list, pressing the Enter key to separate the items (see Figure 2-4).
Figure 2-4. Create a custom list by typing the entries.
■Tip Instead of typing a list in the List entries box, you can import the list from the worksheet by selecting it and clicking the Import button.
4. Click OK to close the Options dialog box.
Applying the Custom Sort Order If the Region field is already in the pivot table, it won’t automatically be sorted in the new custom order. If the field is set for AutoSort, with Region selected in the Using field dropdown list, refresh the pivot table to see the Regions in the custom sort order.
2.6 ■ SORTING A PIVOT FIELD: SORTING ITEMS GEOGRAPHICALLY
If the Region field is set for manual sort, you can remove it from the pivot table, refresh the pivot table, then drag the Region field back. Or use the custom sort order to re-sort it: 1. Select the Region field button. 2. Choose Data ➤ Sort. If the field is set for AutoSort, you’ll see a message asking if you want to sort it manually. Click the No button, then refresh the pivot table to apply the custom sort order (see Figure 2-5).
Figure 2-5. Warning message that appears if the field is set for AutoSort
3. The Sort dialog box appears, with fewer options than you’d have when choosing Data ➤ Sort in a list on the worksheet (see Figure 2.6). Under the Sort option, Labels should be selected, because you had selected a label cell in the pivot table.
Figure 2-6. The Sort dialog box when accessed from a pivot table
47
48
2.7 ■ SORTING A PIVOT FIELD: DATA SOURCE ORDER
4. Click the Options button to open the Sort Options dialog box. 5. From the First key sort order dropdown list, select your custom list, then click OK. 6. Click OK to close the Sort dialog box.
2.7. Sorting a Pivot Field: Data Source Order Problem When you created a new PivotTable report, the field items were listed in ascending order. You want the items in the same order they appear in the source data, but when you rightclick a field and click the Advanced button, the Data Source Order option is disabled, and you can’t select it.
Solution In the PivotTable Field Advanced Options dialog box, the Data Source Order option is available only for pivot tables based on OLAP source data. For pivot tables built from non-OLAP sources, the items are listed in ascending order when the pivot table is created. To achieve a different sort order, you could create a custom list, as described in Section 2.6, and sort on that.
2.8. Sorting a Pivot Field When Some Items Won’t Sort Correctly Problem One of your salespeople is named Jan, and her name always appears at the top of the SalesRep items, ahead of the names that precede it alphabetically.
Solution If the field is set to Manual sort, Jan should move to the correct position alphabetically. Follow these steps to set the field for Manual sort: 1. Right-click the SalesRep field button, and choose Field Settings. 2. Click the Advanced button. 3. Under AutoSort options, select Manual.
2.10 ■ USING TOP 10 AUTOSHOW: SPECIFYING ITEMS OVER A SET AMOUNT
4. Click OK to close the PivotTable Field Advanced Options dialog box, and click OK to close the PivotTable Field dialog box. 5. Select a cell in the SalesRep field, and click the Sort Ascending (A-Z) button on the Excel Standard toolbar.
How It Works Jan goes to the top of the list because Excel assumes it means January, which appears in one of the built-in custom lists. Other names, such as May or June, would also go to the top of the list, because they’re also in the custom list for months. When you create a pivot table and Excel detects an entry from a custom list, it uses that list as the first sort order. So, in your table, Jan sorts to the top, and the other SalesRep names appear below, because they’re not in a custom list.
2.9. Using Top 10 AutoShow: Specifying Top Items Overall Problem In the row area, you have Region and Product fields, and you’d like to show the top ten products overall.
Solution For non-OLAP based pivot tables, if you have multiple fields in the row area, the top ten products per region will be displayed. For OLAP-based pivot tables, the results for the top ten products overall will be displayed, with the sales per region showing. In the non-OLAP pivot table, you could remove the Region field from the row area, and the top ten overall products would show. If you move Region to the column area, the pivot table would show the top ten overall products, with the sales results for each region in its column.
2.10. Using Top 10 AutoShow: Specifying Items Over a Set Amount Problem You’d like to use the Top 10 AutoShow feature to show the products that have sales over a set amount.
49
50
2.11 ■ USING TOP 10 AUTOSHOW: REFERRING TO A CELL VALUE
Solution The Top 10 AutoShow feature can show only a set number of items, not items that meet a specific criterion. You can manually or programmatically hide the items that are below your limit.
2.11. Using Top 10 AutoShow: Referring to a Cell Value Problem You’d like to use a cell value to set the number of items in the Top 10 AutoShow feature.
Solution You could use programming to change the AutoShow settings, based on a change to the worksheet. However, you can’t manually refer to a cell while in the Top 10 AutoShow dialog box.
2.12. Grouping: Error Message When Grouping Items in a Date Field Problem You get an error message when you try to group the items in your date field (see Figure 2-7).
Figure 2-7. The error message you see when items in a field can’t be grouped
Solution This error occurs if there are blank cells, or cells with text, in the source data field. 1. If there are blank cells in the date column of the source data, add a date in each record. If necessary, add dummy dates where no date is available. 2. If any of the rows contain text, such as N/A, replace the text with a date.
2.13 ■ GROUPING: ERROR MESSAGE WHEN GROUPING ITEMS IN A NUMERIC FIELD
3. Check the source range in the PivotTable and PivotChart Wizard to ensure that no blank rows have been included at the end of the source data table. 4. Refresh the pivot table, and try to group the items in the date field.
■Tip If you’re still unable to group the items in the date field, remove it from the pivot table layout, refresh the pivot table, then add the date field to the pivot table again.
2.13. Grouping: Error Message When Grouping Items in a Numeric Field Problem You get an error message when you try to group the items in your numeric field.
Solution The numbers may have been copied or imported from another program and are text values instead of real numbers. In the source data, convert the text to numbers, and you’ll be able to group the items in the pivot table.
How It Works There are several ways to convert text “numbers” to real numbers. For example, you can use the Paste Special command: 1. Select a blank cell on the worksheet, and copy it. 2. In the source data, select the cells that contain the numbers. 3. Choose Edit ➤ Paste Special. 4. Select Add, and click OK.
Another method is to use the Text to Columns feature: 1. Select the cells that contain the numbers. 2. Choose Data ➤ Text to Columns. 3. Click the Finish button.
51
52
2.14 ■ GROUPING: ERROR MESSAGE WHEN GROUPING ITEMS IN A DATE FIELD WITH NO BLANKS OR TEXT
2.14. Grouping: Error Message When Grouping Items in a Date Field with No Blanks or Text Problem You’ve checked your source data, and it doesn’t contain any blank cells or text values in the date column. However, you still get an error message when you try to group the items in your date field.
Solution There may be a field with grouped items left over from the previous time that you grouped the data. Check the field list to see if there’s a second copy of the date field, for example, Date2. If there is, add it to the row area and ungroup the items. Then, you should be able to group the date field items again.
2.15. Grouping the Items in a Page Field Problem Your date field is in the page area, and you aren’t able to group the items in the field.
Solution Move the date field to the row or column area and group the items, then drag the date field back to the page area.
2.16. Grouping the Items in a Page Field: Using an External Source Problem Your date field is in the page area, and you aren’t able to move it to the row area.
Solution If you’re using an external source, check that the page field can be moved:
2.19 ■ GROUPING DATES BY WEEK
1. Double-click the Page field button. 2. In the PivotTable Field dialog box, click the Advanced button. 3. Set the Page field option to Retrieve external data for all page field items. 4. Click OK to close the PivotTable Field Advanced Options dialog box, and click OK to close the PivotTable Field dialog box.
2.17. Grouping: Incorrect Error Message About Calculated Items Problem You’re trying to group the items in a date field, and you’re getting an error message about calculated items. The date field doesn’t have any calculated items.
Solution There may be another field in the pivot table that has a calculated item, and that’s preventing the grouping. The other field may not be visible in the pivot table, but it will still prevent the grouping. You can delete the calculated item, or use formulas in the source data to create date groups. Then, add that field to the pivot table.
2.18. Grouping Text Items Problem You’d like to group states by region, but you don’t want to do it manually.
Solution Instead of grouping the items in the pivot table, you could add a Region field to the source data. Then, add that field as the first row field, and the states will appear in the correct region.
2.19. Grouping Dates by Week Problem You want to group dates by week, but the option isn’t available in the Group By dialog box.
53
54
2.20 ■ GROUPING DATES BY FISCAL QUARTER
Solution In the Group By dialog box, select Days, and set the number of days to seven.
2.20. Grouping Dates by Fiscal Quarter Problem You want to group dates by fiscal quarter instead of using the calendar quarter that’s available in the Group By dialog box.
Solution There’s no built-in option to group by fiscal quarter. In the source data, you can add a column that calculates the fiscal quarter, then add that field to the pivot table.
2.21. Grouping Renamed Numeric Items Problem You renamed an item, changing it from 3.00 to 3.01, and now you get an unusual error message when you try to group the items (see Figure 2-8).
Figure 2-8. The error message that appears when you try to group items in the field with the renamed item
Solution If you rename a numeric item in the pivot table, it becomes text, and will prevent you from grouping items in the numeric field. Follow these steps to change the entry back to a number:
2.23 ■ GROUPING DATES USING THE STARTING DATE
1. Drag the numeric field out of the pivot table. 2. Refresh the pivot table. 3. Drag the numeric field back to the pivot table.
2.22. Grouping Months Problem When you group by month, January data from different years is lumped together. You’d like to keep the data from different years separated.
Solution When you group, select both Year and Month in the Group By list. The data from each year will be grouped, and each month in that year will be grouped.
2.23. Grouping Dates Using the Starting Date Problem You want to group dates into four-week periods that coincide with your sales calendar. The grouped dates are a couple of days off, starting midweek instead of on a Monday.
Solution When you group the dates, select to group by days, and set the number of days to 28. Then, select a starting date for the first period.
How It Works Excel automatically selects the first date in your source data, but you can select a date outside this range to get the starting date that you need.
55
56
2.24 ■ GROUPING DATES BY MONTHS AND WEEKS
2.24. Grouping Dates by Months and Weeks Problem You grouped days by week, and the date range was displayed in the pivot table. When you added month as a grouping level, the days became ungrouped, but you’d like to use months and weeks together.
Solution There’s no way to group by both weeks and months. You could create a column in the source data, and calculate one of the grouping levels there. Then, add that field to the pivot table.
2.25. Grouping the Items in a Pivot Table Based on an Existing Pivot Table Problem You based one pivot table on another. You’d like to group items in one pivot table, and leave items in that field ungrouped in the other pivot table.
Solution Because the pivot tables use the same pivot cache, the grouped items will be the same in both tables. If you need different groups in the pivot tables, you can base each pivot table on the source range instead of using another pivot table as the source.
2.26. Grouping Dates Outside the Range Problem When you group by date, and set a start or end date, Excel automatically creates categories for all items outside this range. You’d like to prevent this from happening.
Solution There’s no setting you can change to prevent this from happening. You can uncheck these groups in the date field dropdown list to prevent them from appearing in the pivot table.
2.27 ■ GROUPING NONADJACENT ITEMS
2.27. Grouping Nonadjacent Items Problem You want to group several customers in the Customer row field, but they aren’t listed consecutively. You don’t want to rearrange the pivot items before you group them.
Solution 1. In the Customer field, select one of the cells that contains a customer name that you want in the group. 2. Ctrl-click on each of the remaining customer names for the group (see Figure 2-9).
Figure 2-9. Hold the Ctrl key and click on customer names.
3. Right-click on one of the selected Customer names, and choose Group and Show Detail ➤ Group.
57
CHAPTER
3
■■■
Calculations in a Pivot Table I
n a pivot table, you can use summary functions or custom calculations to summarize the data, and you can write your own formulas to create calculated fields and calculated items.
3.1. Using Summary Functions Problem You can select from a list of summary functions when subtotaling fields in a pivot table or adding fields to the data area, and you wonder if they’re the same as the worksheet functions.
Solution When you add a field to the pivot table, a list of 11 functions is available to summarize the data in the data area or subtotal the data for row and column fields. The summary functions in a pivot table are similar to the worksheet functions with the same names, with a few differences as noted in the “How It Works” section that follows. A summary function is automatically applied to each field when it’s added to the pivot table, and in most cases you can select a different summary function.
■Note The list of summary functions is not available when you’re working with OLAP-based pivot tables.
Follow these steps to change the summary function for a field in the data area:
59
60
3.1 ■ USING SUMMARY FUNCTIONS
1. Right-click a cell in the field that you want to change, and choose Field Settings. 2. In the Summarize by list, select one of the functions (see Figure 3-1).
Figure 3-1. The PivotTable Field dialog box with a list of summary functions
3. Click OK to close the PivotTable Field dialog box.
■Tip Add two copies of a field to the pivot table data area, and use a different summary function for each copy. For example, show the Sum of Orders and the Count of Orders.
How It Works Only the 11 functions listed here are available when summarizing or subtotaling pivot table data; other functions, such as Median, Mode, and Percentile, can be calculated from the source data, outside the pivot table.
Sum Sum is the default summary function for numerical fields that are added to the pivot table’s data area, and it totals all the underlying values in the data area. The result is the same as using the SUM function on the worksheet to total the values.
Count Count is the default summary function when fields with text or blank cells are added to the data area. Like the COUNTA worksheet function, the pivot table Count function counts text, numbers, and errors. Blank cells are not counted.
3.1 ■ USING SUMMARY FUNCTIONS
Average This summary function totals all the underlying values in the data area, and divides by the number of values. The result is the same as using the AVERAGE function on the worksheet to calculate the average (mean) of the values.
Max This summary function shows the maximum value from the underlying values in the data area. The result is the same as using the MAX function on the worksheet to calculate the maximum of the values.
Min This summary function shows the minimum value from the underlying values in the data area. The result is the same as using the MIN function on the worksheet to calculate the minimum of the values.
Product This summary function shows the result of multiplying all the underlying values in the data area. The result is the same as using the PRODUCT function on the worksheet to calculate the product of the values. The results of this function may be very large numbers and default to a Scientific number format. Excel only stores and calculates with 15 significant digits of precision, so after the 15th character you’ll only see zeros.
Count Nums This summary function counts all the underlying numbers in the data area. The result is the same as using the COUNT function on the worksheet. Blank cells, errors, and text are not counted.
StDev Like the STDEV worksheet function, this summary function calculates the standard deviation for the underlying data in the data area. If the count of items is one, a #DIV/0! error will be displayed, because one is subtracted from the count when calculating the standard deviation. In Excel 2003, there may be differences between the worksheet results and the pivot table results, because the worksheet function has been improved. For more information, see the Microsoft Knowledge Base article “Description of Improvements in the Statistical
61
62
3.1 ■ USING SUMMARY FUNCTIONS
Functions in Excel 2003 and in Excel 2004 for Mac,” at http://support.microsoft.com/ default.aspx?kbid=828888.
StDevp Like the STDEVP worksheet function, the StDevp summary function calculates the standard deviation for the entire population for the underlying data in the data area. In Excel 2003, improvements were made to several statistical functions, including STDEV and STDEVP. See the previous “StDev” section for more information.
Var Like the VAR worksheet function, this summary function calculates the variance for the underlying data in the data area. If the count of items is one, a #DIV/0! error will be displayed, because one is subtracted from the count when calculating the standard deviation. In Excel 2003, improvements were made to several statistical functions, including VAR and VARP. The Var and Varp summary functions have been improved when used in the interior of the PivotTable report, but not for grand totals for rows or columns. For more information, see the Microsoft Knowledge Base article “Excel Statistical Functions: VAR and VARP Improvements and Pivot Tables,” at http://support.microsoft.com/ default.aspx?kbid=829250.
Varp This summary function calculates the variance for the entire population for the underlying data in the data area. In Excel 2003, improvements were made to several statistical functions, including VAR and VARP. See the previous “Var” section for more information.
Errors in the Source Data The Count and Count Nums fields handle errors as we outlined earlier. For other summary functions, if there are errors in the source data field, the first error encountered will be displayed in the pivot table, and the total will not be calculated. If subtotals, or row and column totals, are displayed, affected totals and subtotals will display the error. For example, the source data shown in Figure 3-2 has two errors for West Binder data—a #REF! error and an #N/A error.
3.2 ■ USING SUMMARY FUNCTIONS: DEFAULT FUNCTIONS
Figure 3-2. Errors in the sample source data
The first error, #REF!, appears in the pivot table, when West Binder Totals are summed (see Figure 3-3). If the dates were sorted in descending order, the #N/A error would be listed first, and would appear in the PivotTable report.
Figure 3-3. The first error in the source data appears in the pivot table.
3.2. Using Summary Functions: Default Functions Problem When you added the Amount_Owing field to the data area, it automatically used the Sum function, but when you added the Amount_Paid field to the data area, it automatically used the Count function. You’re not sure why different functions were used for two data fields that are similar.
Solution Perhaps there were blank cells in the source data for the Amount_Paid field. If you add a number field to the data area, the default summary function is Sum. If there are blank cells, or non-numeric data in the field, the Count function is used as a default.
63
64
3.3 ■ USING SUMMARY FUNCTIONS: COUNTING BLANK CELLS
■Note You can’t set a default summary function in a PivotTable.
After the field has been added to the data area, you can change its summary function: 1. Right-click a cell in the field that you want to change, and choose Field Settings. 2. In the Summarize by list, select one of the functions. 3. Click OK to close the PivotTable Field dialog box.
3.3. Using Summary Functions: Counting Blank Cells Problem There are blank cells in the Region field of your source data, and you want to show a count of the blank Region cells in the pivot table. You added the Region field to the pivot table’s row area, and another copy of the Region field in the data area, as Count of Region. However, there’s no count showing for the blank regions.
Solution A pivot table can’t count the blank cells when you add a field to the data area and use the Count or CountNum summary function. Add a different field to the data area, and use it for the count. For example, if the Units field will always contain data, add Region to the row area, and add Count of Units to the data area (see Figure 3-4). The count of blank Regions will be calculated.
Figure 3-4. Blank cells are not counted in the data area.
3.4 ■ USING CUSTOM CALCULATIONS: DIFFERENCE FROM
3.4. Using Custom Calculations: Difference From Problem Every morning, you download the current month’s sales data from your sales system. You’d like to use the pivot table to calculate each day’s change from the previous day, to obtain the daily sales figures for each region. In your pivot table, Date is in the row area, and Region is in the column area. Units sold (Sum of Units) is in the data area, as shown in Figure 3-5.
Figure 3-5. Pivot table shows units sold to date for each region
Solution To supplement the summary functions, custom calculations are available when summarizing data in a pivot table. In addition to the default normal calculation, custom calculations provide eight different ways of viewing the summary results. For this problem, use the Difference From custom calculation to analyze the data that accumulates over the month by comparing each day’s total to the previous day’s total.
■Tip Custom calculations are only available for pivot tables based on non-OLAP sources.
1. Right-click on the Sum of Units button, and choose Field Settings. 2. Click the Options button. 3. From the dropdown list for Show data as, select Difference From. 4. For the base field, select Date.
■Caution If you select a base field that isn’t in the row or column area, all the results will show an #N/A error. 5. For the Base item, select (previous), and click the OK button.
65
66
3.5 ■ USING CUSTOM CALCULATIONS: % OF
The row for the first date is empty, because there’s no previous date with which to compare it. The remaining rows show the change from the previous day in units sold for each region (see Figure 3-6).
Figure 3-6. The custom calculation Difference From compares daily sales totals.
How It Works The Difference From custom calculation compares each item in the field to another item in the same field. In this example, (previous) was selected, so each day’s total is compared to the previous day’s total. If days are missing or hidden, the comparison is made to the previous visible day. Instead of (previous), you could select (next) to compare each day to the next visible day’s totals, or select a specific date to compare all days to the sales for the selected date. Using the same sample data, you could select Region as the base field, and compare each region’s sales to sales in the Central Region (see Figure 3-7).
Figure 3-7. The custom calculation Difference From, with Region as a base field
3.5. Using Custom Calculations: % Of Problem You have monthly sales figures for each region, and would like to compare average sales for each region to the average sales in your strongest region. In your pivot table, Date is in the row area, grouped by Month; Region is in the column area; and Units sold (Average of Units) is in the data area (see Figure 3-8).
3.5 ■ USING CUSTOM CALCULATIONS: % OF
Figure 3-8. Pivot table with average Units per Sale per Region
Solution While using the custom calculations, you can use different summary functions for the data. For this problem, you can use the Average summary function and the % Of custom calculation. 1. Right-click on the Sum of Units button, and choose Field Settings. 2. Click the Options button. 3. From the dropdown list for Show data as, select % Of. 4. For the Base field, select Region. 5. For the Base item, select Central (see Figure 3-9), then click the OK button.
Figure 3-9. The custom calculation % Of, with Region as a base field
67
68
3.6 ■ USING CUSTOM CALCULATIONS: % DIFFERENCE FROM
The column for the Central shows 100%, because it’s being compared to itself. The remaining columns show the percent of their average units per sale, compared to the Central region’s average units per sale.
3.6. Using Custom Calculations: % Difference From Problem You have monthly sales figures and forecasts for each product, and would like to compare actual sales to forecast sales for each product per month. In your pivot table, Date (grouped by Month) and Product are in the row area, Status (Actual or Forecast) is in the column area, and Units sold (Sum of Units) is in the data area (see Figure 3-10).
Solution Use % Difference From to compare the Actual to Forecast items in the Status field.
■Tip Leave the original copy of the Sum of Units field, so you can see the Units quantity, as well as the % Difference From calculation.
1. Add another copy of the Units field to the data area. 2. Right-click on the Sum of Units2 heading, and choose Field Settings. 3. Click the Options button. 4. From the dropdown list for Show data as, select % Difference From. 5. For the Base field, select Status. 6. For the Base item, select Forecast, and then click the OK button.
3.7 ■ USING CUSTOM CALCULATIONS: RUNNING TOTAL
Figure 3-10. The % Difference calculation from the forecast in units sold per product per month
The Forecast column will be empty, because it won’t be compared to itself. The Actual column shows the % Difference From calculation from the forecast in units sold.
■Tip Select the cell that contains the Sum of Units2 label, and type a more descriptive name, for example, % Diff.
3.7. Using Custom Calculations: Running Total Problem You have monthly sales figures for each product, and would like to see a running total by month. In your pivot table, Date (grouped by Month) is in the row area, Product is in the column area, and Units sold (Sum of Units) is in the data area (see the table at the left in Figure 3-11).
Solution Use a running total in a custom calculation to accumulate the sales amounts per product, down through the list of months. 1. Right-click on the Sum of Total button, and choose Field Settings. 2. Click the Options button. 3. From the dropdown list for Show data as, select Running Total In. 4. For the Base field, select Date, then click the OK button.
69
70
3.7 ■ USING CUSTOM CALCULATIONS: RUNNING TOTAL
■Caution If you select a base field that isn’t in the row or column area, all the results will show an #N/A error. Also, if there’s an error in any month’s results, it will carry down through the remaining months.
Figure 3-11. Running total in a custom calculation with different base fields
How It Works With Date as the base field, each Product column shows a running total for the year, by month (see the center table in Figure 3-11). If you select Product as the base field, the running total accumulates across the pivot table, in each month row (see the right table in Figure 3-11). For pivot tables with multiple fields in the row area, the running totals work the same way, but may be harder to follow as the layout becomes more complex. Using the previous example, with Product moved to the row area, the running total amounts are the same but are arranged vertically (see Figure 3-12).
Figure 3-12. Running total in a custom calculation with different base fields and two row fields
3.8 ■ USING CUSTOM CALCULATIONS: % OF ROW
3.8. Using Custom Calculations: % of Row Problem You have monthly sales figures for each sales manager, and would like to see what percent of their total sales came from each product category. In your pivot table, Sales Manager is in the row area, Category is in the column area, and Units is in the data area (see the table at the left in Figure 3-13).
Solution Use the % of Row custom calculation to show the percent per category for each Sales Manager’s sales. 1. Right-click on the Sum of Sales button, and choose Field Settings. 2. Click the Options button. 3. From the dropdown list for Show data as, select % of row, then click OK.
■Note There’s no option to select a base field or base item with this custom calculation.
In the row for each Sales Manager, you can see the percent of the total that was sold in each category (see the table at the right in Figure 3-13).
Figure 3-13. The % of Row custom calculation shows category sales percentages.
■Tip You can hide the Grand Total for Rows, because it will always equal 100%.
71
72
3.9 ■ USING CUSTOM CALCULATIONS: % OF COLUMN
3.9. Using Custom Calculations: % of Column Problem You have monthly sales figures for each product category, and would like to see what percent of their total sales were attributed to each sales manager. In your pivot table, Sales Manager is in the row area, Category is in the column area, and Units is in the data area (see the table at the left in Figure 3-14).
Solution 1. Right-click on the Sum of Units Count button, and choose Field Settings. 2. Click the Options button. 3. From the dropdown list for Show data as, select % of column, then click OK.
In the column for each Category, you can see the percent of the total for each Sales Manager (see the table at the right in Figure 3-14).
Figure 3-14. The % of Column custom calculation shows Sales Manager percentages.
■Tip There's no % of Subtotal custom calculation available in a pivot table, but you can arrange fields in the row and column areas, then use % of Row or % or Column to show the percentages.
3.10. Using Custom Calculations: % of Total Problem You have monthly insurance policy counts for your regional offices. You’d like to see what percentage of the existing policies are in each region, for auto policies and property policies.
3.11 ■ USING CUSTOM CALCULATIONS: INDEX
In your pivot table, Region is in the row area, Policy Type is in the column area, and Sum of Policies is in the data area. Policy Status is in the page area, and Existing has been selected (see the table at the left in Figure 3-15).
Solution 1. Right-click on the Sum of Policies button, and choose Field Settings. 2. Click the Options button. 3. From the dropdown list for Show data as, select % of total, then click OK.
In each cell, you can see the percent of the total that was sold in each region for each policy type (see the table at the right in Figure 3-15).
Figure 3-15. The % of Total custom calculation shows percentage policy type by Region.
3.11. Using Custom Calculations: Index Problem You have monthly insurance policy counts for your regional offices. You’d like to compare canceled policies in each region, for both auto policies and property policies. In your pivot table, Region is in the row area, Policy Type is in the column area, and Sum of Policies is in the data area. Policy Status is in the page area, and Cancel has been selected (see the table at the left in Figure 3-16).
Solution Use the Index option to show the relative weight of each cell when compared to its row total, its column total, and the grand total.
73
74
3.12 ■ USING FORMULAS: CALCULATED FIELD VS. CALCULATED ITEM
1. Right-click on the Sum of Total button, and choose Field Settings. 2. Click the Options button. 3. From the dropdown list for Show data as, select Index, then click OK.
In each cell, you can see its index (see the table at the right in Figure 3-16).
Figure 3-16. The Index custom calculation shows an index of the canceled policy type by Region.
How It Works The index formula is ((value in cell) x (Grand Total of Grand Totals)) / ((Grand Row Total) x (Grand Column Total)) Two cells with the same number of canceled policies may have a different index. A value of 1000 will have a higher index if it’s the highest value in its row and column, but the same value would have a lower index if it’s the lowest value in its row and column.
3.12. Using Formulas: Calculated Field vs. Calculated Item In addition to the built-in summary functions and custom calculations, you can write your own formulas in a pivot table to create calculated fields and calculated items.
■Note Formulas are available only in non-OLAP based pivot tables.
Problem You’re not sure when to use a calculated field and when to use a calculated item.
3.13 ■ USING FORMULAS: ADDING ITEMS WITH A CALCULATED ITEM
Solution You can create calculated items in a field to perform calculations on other items in that field. For example, if your pivot table contains a Status field that stores the Order status, you could create a Sold item that sums the items with a status of Shipped, Pending, and Backorder but doesn’t include Canceled. You can create calculated fields to perform calculations on other fields in the pivot table. For example, you may have agreed to pay sales representatives a 3 percent bonus on any products for which they sold more than 100 units. The calculated field would display the bonus amount using values in the Units and Total fields.
■Caution If you create a calculated item or a calculated field, you may not be able to move the pivot field to the page area.
How It Works A calculated item becomes an item in a pivot field. Its calculation can use the contents of other items in the same field. A calculated field becomes a new field in the pivot table, and its calculation can use the contents of other fields. Calculated items and calculated fields are calculated differently. For calculated items, the individual records in the source data are calculated, then the results are summed. For example, if you create a Tax field that multiplies the items by 7 percent, the value in each record is multiplied by 7 percent, then the individual tax amounts are summed for the Grand Total. For calculated fields, the individual amounts are summed, then the calculation is performed on the total amount.
■Note You can’t create formulas that refer to the pivot table totals or subtotals. Also, the calculated item and calculated field formulas can’t refer to worksheet cells by address or by name.
3.13. Using Formulas: Adding Items with a Calculated Item Problem Your pivot table contains an Order Status field, and you’d like to create a Sold item that sums the items with a status of Shipped, Pending, and Backorder but doesn’t include Canceled.
75
76
3.14 ■ USING FORMULAS: MODIFYING A CALCULATED ITEM
Solution Create a calculated item that adds the Shipped, Pending, and Backorder items. 1. In the pivot table, select the Status field button. 2. On the Pivot toolbar, choose PivotTable ➤ Formulas ➤ Calculated Item. 3. Type a name for the Calculated Item, for example, Sold. 4. Press the Tab key to move to the Formula box. 5. In the Fields list, ensure that Status is selected. 6. In the Items list, double-click on Shipped, then type a plus sign (+). 7. Double-click on Pending, and type a plus sign, then double-click on Backorder. 8. The complete formula is =Shipped+ Pending+ Backorder 9. Click the OK button to save the calculated item, and close the dialog box. 10. In the pivot table, hide the Shipped, Pending, and Backorder items to see the correct Grand Totals.
3.14. Using Formulas: Modifying a Calculated Item Problem After you create a calculated item, you want to change it.
Solution You can go back into the Formula dialog box, and modify the calculated item. 1. In the pivot table, select the field button for the field that contains the calculated item. 2. On the Pivot toolbar, choose PivotTable ➤ Formulas ➤ Calculated Item. 3. From the Name dropdown, select the name of the calculated item that you want to modify.
3.16 ■ USING FORMULAS: PERMANENTLY REMOVING A CALCULATED ITEM
4. In the Formula box, change the formula. 5. Click the Modify button. 6. Click OK to close the dialog box.
3.15. Using Formulas: Temporarily Removing a Calculated Item Problem After you create a calculated item, you want to temporarily remove it from the pivot table.
Solution You can hide a calculated item, then show it again later. 1. Click the dropdown arrow in the calculated item’s field list. 2. Remove the checkmark from the calculated item. 3. Click OK.
3.16. Using Formulas: Permanently Removing a Calculated Item Problem After you create a calculated item, you want to permanently remove it from the pivot table.
Solution 1. In the pivot table, select the field button for the field that contains the calculated item. 2. On the Pivot toolbar, choose PivotTable ➤ Formulas ➤ Calculated Item. 3. From the Name dropdown, select the name of the calculated item that you want to delete. 4. Click the Delete button. 5. Click OK to close the dialog box.
77
78
3.17 ■ USING FORMULAS: USING INDEX NUMBERS IN A CALCULATED ITEM
3.17. Using Formulas: Using Index Numbers in a Calculated Item Problem The items in your field change frequently, and you’d like to refer to the items by number in the formulas instead of by name.
Solution You can use an index number to refer to field items. For example, to multiply the data for the first item in the Date field by 5 percent, use the following formula: =Date[1]*.05
■Caution If you create a calculated item that refers to items by position, the Top 10 AutoShow and AutoSort options become unavailable, and are reset to Manual or Off.
3.18. Using Formulas: Using Relative Position Numbers in a Calculated Item Problem You’d like to refer to items by their position relative to the calculated item instead of by name.
Solution You can refer to items by their position relative to the calculated item. For example, to calculate the difference between the data for the first visible Date item and the last visible Date item, use the following formula: =Date[1]-Date[-1] This formula assumes that the calculated item will be last in its field and the last date is directly above it (see Figure 3-17).
3.19 ■ USING FORMULAS: MODIFYING A CALCULATED ITEM FORMULA IN A CELL
Figure 3-17. The DiffFirstLast calculated item refers to the dates by index number.
■Caution If you create a calculated item that refers to items by relative position, the Top 10 AutoShow and AutoSort options become unavailable, and are reset to Manual or Off.
3.19. Using Formulas: Modifying a Calculated Item Formula in a Cell Problem You’d like a calculated item to be slightly different for one column.
Solution You can select a cell that shows the result of a calculated item, and modify its formula in the formula bar. For example, in the previous calculated item example, the index number was used to calculate the difference between the first visible item and the last: =Date[1]-Date[-1] If one of the columns has a blank cell in the first position, you can modify the formula to refer to the second item: =Date[2]-Date[-1]
■Caution The Undo feature is not available after making this change.
To make the change, select the cell, and modify the formula in the formula bar.
79
80
3.20 ■ USING FORMULAS: CREATING A CALCULATED FIELD
■Tip Unlike worksheet formulas, where you can select several cells, make a change, then press Ctrl + Enter to apply the change to all selected cells, the pivot table cells with calculated item formulas must be changed individually.
3.20. Using Formulas: Creating a Calculated Field Problem Your pivot table contains a Total field that stores the total amount of each sale, and a Units field, that stores the number of units sold. You’d like a Bonus field that calculates the bonus payable if a sales representative has sold more than 100 units of any product.
Solution 1. In the pivot table, select any cell. 2. On the Pivot toolbar, choose PivotTable ➤ Formulas ➤ Calculated Field. 3. Type a name for the calculated field, for example, Bonus. 4. Press the Tab key to move to the Formula box. 5. Type the formula =IF(Units>100,Total*0.03,0) 6. Click the OK button to save the calculated field and close the dialog box.
■Note You can’t change the summary function for a calculated field. Sum is the only function available for these fields.
3.21. Using Formulas: Modifying a Calculated Field Problem After you create a calculated field, you’d like to change it.
3.23 ■ USING FORMULAS: PERMANENTLY REMOVING A CALCULATED FIELD
Solution You can go back into the Formula dialog box, and modify the settings. 1. In the pivot table, select any cell. 2. On the Pivot toolbar, choose PivotTable ➤ Formulas ➤ Calculated Field. 3. From the Name dropdown, select the name of the calculated field that you want to modify. 4. In the Formula box, change the formula. 5. Click the Modify button, then click OK to close the dialog box.
3.22. Using Formulas: Temporarily Removing a Calculated Field Problem After you create a calculated field, you’d like to remove it from the pivot table temporarily.
Solution You can hide a calculated field, then show it again later: 1. Click the dropdown arrow in the Data field list. 2. Remove the checkmark from the calculated field, then click OK.
3.23. Using Formulas: Permanently Removing a Calculated Field Problem After you create a calculated field, you’d like to remove it from the pivot table permanently.
Solution 1. In the pivot table, select any cell. 2. On the Pivot toolbar, choose PivotTable ➤ Formulas ➤ Calculated Field.
81
82
3.24 ■ USING FORMULAS: DETERMINING THE TYPE OF FORMULA
3. From the Name dropdown, select the name of the calculated field that you want to delete. 4. Click the Delete button, then click OK to close the dialog box.
3.24. Using Formulas: Determining the Type of Formula Problem You can’t tell if a formula is a calculated item or calculated field.
Solution If it’s a calculated field, you’ll see its name in the PivotTable Field List. If the name doesn’t appear there, it’s a calculated item.
3.25. Using Formulas: Adding a Calculated Item to a Field With Grouped Items Problem You want to add a calculated item in a field, but some items are grouped, and you get an error message.
Solution Ungroup the items and create the calculated item. Then, regroup the items in the field.
3.26. Using Formulas: Calculating the Difference Between Plan and Actual Problem In your pivot table you have a Plan amount and an Actual amount. You’d like to calculate the percent difference between Actual and Plan.
3.28 ■ USING FORMULAS: COUNTING UNIQUE ITEMS IN A CALCULATED FIELD
Solution If Plan and Actual are separate fields in the source data, you can create a calculated field to calculate the percent difference. If they’re items in the same field, use the % Difference From custom calculation.
3.27. Using Formulas: Correcting the Grand Total for a Calculated Field Problem You have a calculated field that calculates a customer discount based on each month’s orders: =IF(Units >1000,Dollars *-0.02,0) You assumed the grand total would be a sum of the discount amounts, but it performs the same discount calculation in the grand total row, creating the wrong total (see Figure 3-18).
Solution The grand total for a calculated field will perform the same calculation that’s defined in the calculated field. As a workaround, you could use a GetPivotData formula outside the pivot table to extract the line totals, and then sum those amounts (see Figure 3-18).
Figure 3-18. The Grand Total is incorrect for the calculated field.
3.28. Using Formulas: Counting Unique Items in a Calculated Field Problem In your pivot table, you want a count of unique customers who placed orders, but the data area will only show a count of orders. Some customers placed several orders, so the count of orders doesn’t tell you how many customers you have.
83
84
3.29 ■ USING FORMULAS: CORRECTING RESULTS IN A CALCULATED FIELD
Solution A pivot table won’t calculate a unique count. You could add a column to the source data, then add that field to the pivot table. For example, to count unique customers in column A, use the following formula: =IF(COUNTIF($A$1:A2,A2)=1,1,0) Copy this formula down to all rows in the list, then add the field to the pivot table data area using the Sum function.
How It Works The COUNTIF formula checks the range A1:A2, and counts the instances of the customer name that’s entered in cell A2. For the first occurrence of the name, the count is one. The IF formula checks the result of the COUNTIF formula, and if the count is one, the IF formula returns a one. For the next occurrence of the customer name, the COUNTIF formula would return a 2, and the IF formula would return a zero. When you copy the formula down the column on the worksheet, only the first occurrence of each customer name returns a one, so the sum of this column is the number of unique customers.
3.29. Using Formulas: Correcting Results in a Calculated Field Problem In your source data, you have columns for price and quantity, but the results are incorrect when you use a calculated field to multiply price by quantity.
Solution If you use a calculated field to create the totals in the pivot table, you may get incorrect results. For example, the pivot table may show the Sum of Price multiplied by Sum of Quantity for results that are much too high. Instead, calculate the total sales (price × quantity) for each row in the source data table. Then, add that field to the pivot table’s data area to get the correct totals.
3.31 ■ USING FORMULAS: ACCIDENTALLY CREATING A CALCULATED ITEM
3.30. Using Formulas: Listing All Formulas Problem You’re documenting your workbook, and you would like to create a list of all the formulas that you created in the pivot table.
Solution 1. Select any cell in the pivot table. 2. From the Pivot toolbar, choose PivotTable ➤ Formulas ➤ List Formulas.
A new sheet is inserted in the workbook, with a list of calculated items and a list of calculated fields.
3.31. Using Formulas: Accidentally Creating a Calculated Item Problem Your pivot table contains a calculated item, but you didn’t create one and aren’t sure how it got there.
Solution It’s possible to accidentally create a calculated formula if you drag the fill handle on one of the labels. Follow these steps to remove the formula: 1. Select the field button for the field that contains the calculated item. 2. From the Pivot toolbar, choose PivotTable ➤ Formulas ➤ Calculated Item. 3. From the dropdown list of formulas, select the formula you want to delete. 4. Click the Delete button, then click the Close button.
85
86
3.32 ■ USING FORMULAS: SOLVE ORDER
3.32. Using Formulas: Solve Order Problem In your pivot table, there are two calculated items: CancelRate: = Cancel/( Cancel+ Existing) All: East + West In the pivot table, grand totals are hidden and the calculated items are shown (see the table at the left in Figure 3-19).
Figure 3-19. The pivot table, with two calculated items
However, the CancelRate for All is incorrect. It’s a sum of the CancelRate for the individual regions, instead of a calculation of the CancelRate, based on the All totals. You’d prefer to have the CancelRate for All be calculated like the CancelRate for the individual regions.
Solution You can change the Solve Order for the calculated items to get the result that you want: 1. Select a cell in the pivot table. 2. From the Pivot toolbar, choose PivotTable ➤ Formulas ➤ Solve Order. 3. Select the All formula, and click the Move Up button.
The All CancelRate is adjusted, and shows the All cancel ratio, instead of the sum of region ratios (see the table at the right in Figure 3-19).
■Note When you change the Solve Order, it affects all calculated items in the pivot table.
3.32 ■ USING FORMULAS: SOLVE ORDER
How It Works Calculated items are added to the Solve Order list in the order in which they’re created. For any result that is affected by two or more calculated items, the last calculation listed is the one that determines its value. In this example, the CancelRate calculated item was created first, then the All calculated item. The bottom-right cell in the pivot table is affected by both calculated items. In the original Solve Order, the All calculated item is last in the Solve Order, so the bottom-right cell shows the result of that formula: East + West. In the revised Solve Order, the CancelRate calculated item is last in the Solve Order, so the bottom-right cell shows the result of that formula: Cancel/(Cancel+ Existing).
87
CHAPTER
4
■■■
Formatting a Pivot Table C
ommon problems with pivot table formatting include loss of formatting when the pivot table is changed or refreshed, showing or hiding subtotals and grand totals, and retaining formats applied in the source data. The solutions in this chapter are those that can be applied manually. Some formatting issues should be addressed by using macros, and are included in Chapter 13.
4.1. Using AutoFormat: Applying a Predefined Format Problem You’d like a quick way to format your pivot table.
Solution You can use the AutoFormat command to apply a predefined format. 1. Select a cell in the pivot table. 2. Choose Format ➤ AutoFormat. 3. Select one of the AutoFormats, and then click OK.
How It Works When you select one of the PivotTable AutoFormats, it will apply specific cell formatting to different parts of the pivot table. For example, all of the row subtotals may be changed to bold Arial font, with yellow fill color in the cell. When you pivot the fields, the formatting is retained.
89
90
4.2 ■ USING AUTOFORMAT: REMOVING AN AUTOFORMAT
Many of the AutoFormats change the layout of the pivot table, moving row or column fields to a different area. Some add blank rows between items. Number formatting and date formatting may be changed.
■Note You can’t create your own AutoFormats, or modify the existing AutoFormats.
4.2. Using AutoFormat: Removing an AutoFormat Problem You applied an AutoFormat to a pivot table, and would like to remove it.
Solution To remove an AutoFormat immediately after applying it, you can click the Undo button, or choose Edit ➤ Undo AutoFormat, or press Ctrl+Z on the keyboard. To remove an AutoFormat later, choose Format ➤ AutoFormat. From the list of AutoFormats, select PivotTable Classic or None, and then click OK.
How It Works Applying the PivotTable Classic AutoFormat or selecting None will remove the cell fill colors and other formatting options that were applied by previous AutoFormats or manually applied to the cells. Number formats that were applied as a field setting will not be removed.
4.3. Using AutoFormat: Applying a Standard Table AutoFormat You’d like to apply one of the standard table AutoFormats, but it isn’t available when you’re formatting a pivot table.
Solution Some AutoFormats aren’t listed when you apply AutoFormats to a pivot table. A different list of AutoFormats is available if you’re formatting a regular table. Follow these steps to use one of the standard AutoFormats: 1. Select a cell in a normal worksheet table (for example, select a cell in your source data table). 2. Choose Format ➤ AutoFormat.
4.4 ■ USING THE ENABLE SELECTION OPTION
3. Select one of the AutoFormats, and then click OK. 4. Select a cell in the pivot table. 5. Press the F4 key to repeat the previous formatting.
4.4. Using the Enable Selection Option Problem You want to select and format all the row subtotals for the Region field at the same time, instead of formatting each one separately.
Solution You can select them all, and then format them together. To select them, you may have to activate the Enable Selection option: 1. Select a cell in the pivot table, and from the PivotTable toolbar, choose PivotTable ➤ Select. 2. If Enable Selection is not activated, click it to activate the feature (see Figure 4-1).
Figure 4-1. The Enable Selection option turned on 3. Point to the left side of a cell that contains a subtotal label, and when the pointer changes to a black arrow shape (see Figure 4-2), click to select all the subtotals for that field.
Figure 4-2. The black arrow pointer at the left of the subtotal in Row 10
4. Format the selected subtotals.
91
92
4.5 ■ LOSING FORMATTING WHEN REFRESHING THE PIVOT TABLE
4.5. Losing Formatting When Refreshing the Pivot Table Problem Your pivot table formatting is lost when you refresh or change the pivot table.
Solution Some formatting loss can be prevented if you change the Formatting options in the PivotTable Options dialog box: 1. Right-click a cell in the pivot table. 2. Choose Table Options. 3. Under Format options, remove the checkmark from AutoFormat table. 4. Add a checkmark to Preserve formatting, and then click OK.
Then, when you apply formatting, do the following: 1. Ensure that Enable Selection is turned on (see Figure 4-1). 2. Use the pivot table selection feature to select the elements that you want to format (point to the top or left edge of the element, and click when the black arrow appears) (see Figure 4-2).
To prevent loss of number formatting, apply formatting to the field instead of selected cells: 1. Right-click the field button or Field heading cell, and choose Field Settings. 2. Click the Number button, and select the number formatting option you want. 3. Click OK to close the Format Cells dialog box, and click OK to close the PivotTable Field dialog box.
4.6 ■ RETAINING THE SOURCE DATA FORMATTING
4.6. Retaining the Source Data Formatting Problem The numbers in your source data are formatted as currency, with no decimals, and a dollar sign. When you add the field to the pivot table’s data area, the formatting is lost.
Solution Although source data number formatting is generally maintained if you add a field to the row, column, or page area, it’s lost if you add the field to the data area. To make it easier to reapply the formatting in the pivot table, you can create a style with all the format settings.
■Tip Styles are available in regular worksheet cells too, so you can apply them within a PivotTable report or in other cells.
1. Select a cell in the source data that has the format you want to save as a style. 2. Choose Format ➤ Style. 3. Type a name for your new style, for example, CURR NO DEC.
■Tip It will be easier to identify your styles if you type them in all capitals. 4. Remove the checkmark from any features you don’t want included in the style. In this style, you may want only the number formatting and thus you should remove all the other checkmarks. 5. If you want to change any of the format settings, click the Modify button, adjust the settings, and click OK in the Format Cells dialog box. 6. Click OK to close the Style dialog box and save the style. Follow these steps to apply a style: 1. Select the cells where you want to apply the style. 2. Choose Format ➤ Style. 3. Select a style from the dropdown list, and click the OK button.
93
94
4.7 ■ HIDING DATA ERRORS ON WORKSHEET
If you use styles frequently, you can add the Style dropdown to one of your toolbars: 1. Choose Tools ➤ Customize. 2. On the Commands tab, select the Format Category. 3. Drag the Style dropdown to one of your existing toolbars, and click the Close button.
4.7. Hiding Data Errors on Worksheet Problem There are errors in the pivot table data, and you’d like to hide them on the worksheet.
Solution By default, error values are displayed in a pivot table. You can hide the errors by changing the pivot table options: 1. Right-click a cell in the pivot table. 2. Choose Table Options. 3. To turn on the For error values, show option, add a checkmark in the check box. 4. Leave the text box blank, and the errors will be replaced with blank cells.
■Note This setting only affects cells in the data area of the pivot table. If there are error values in the source data that appear in the row, column, or page area, they will not be replaced.
4.8. Hiding Errors When Printing Problem There are errors in the pivot table data, and you’d like to hide them when printing the worksheet.
4.10 ■ USING CONDITIONAL FORMATTING IN A PIVOT TABLE
Solution You can hide the errors in the printed copy by changing the page setup options: 1. Choose File ➤ Page Setup. 2. On the Sheet tab in the Print section, select from the Cell errors as dropdown.
4.9. Showing Zero in Empty Data Cells Problem Some cells in the data area are empty, and you’d prefer that they contain a zero.
Solution Change the pivot table options: 1. Right-click a cell in the pivot table, and choose Table Options. 2. Add a checkmark to For empty cells, show, and in the text box, type a zero (see Figure 4-3). 3. Click OK.
Figure 4-3. Set empty data cells to display a zero.
4.10. Using Conditional Formatting in a Pivot Table Problem You want to use conditional formatting to highlight the high and low values in your pivot table data. Values below 25 should have a red background, and values above 75 should have a green background.
95
96
4.11 ■ CREATING CUSTOM NUMBER FORMATS IN THE SOURCE DATA
Solution You can apply conditional formatting in a pivot table, just as you would on normal worksheet cells (Format ➤ Conditional Formatting). However, if the pivot fields are rearranged, the conditional formatting doesn’t move with them but stays on the cells that were originally formatted. If your pivot table is fairly static in size and shape, you may be able to use conditional formatting without too many problems. If your pivot table layout changes frequently, you’ll need to remove and reapply the conditional formatting each time. Another option is to use a custom number format to apply font color to a cell that meets specific criteria, and the format will move with the cell if the pivot table is rearranged. For example, to change the font color to red if the cell value is less than 25, follow these steps: 1. Right-click a cell in the field that you want to format, and choose Field Settings. 2. In the PivotTable Field dialog box, click the Number button. 3. In the Category list, select Custom. 4. In the Type box, type the format [Red][ 0 Then Sh.Name = Left("XShow_" & Sh.Name, 31) End If End If End Sub ' Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim ws As Worksheet Dim Resp As Long Dim ShowCount As Long
261
262
13.9 ■ AUTOMATICALLY DELETING WORKSHEETS WHEN CLOSING A WORKBOOK
ShowCount = 0 For Each ws In ThisWorkbook.Worksheets If UCase(Left(ws.Name, 5)) = "XSHOW" Then ShowCount = ShowCount + 1 End If Next ws If ShowCount > 0 Then Resp = MsgBox("Delete Show Pages sheets?", _ vbYesNo, "Delete Sheets?") If Resp = vbYes Then Application.DisplayAlerts = False For Each ws In ThisWorkbook.Worksheets If UCase(Left(ws.Name, 5)) = "XSHOW" Then ws.Delete End If Next ws End If End If End Sub 2. To run the event code, select a cell in the pivot table and choose PivotTable ➤ Show Pages on the PivotTable toolbar.
How It Works If a new sheet is created, the workbook’s NewSheet event is triggered. If the new sheet contains a pivot table, it’s assumed to be generated by the Show Pages command, and is renamed by the NewSheet code, with XShow_ at the start of the name. When the workbook closes, the workbook’s BeforeClose event is triggered. The event code asks the user if Show Pages sheets should be deleted. If the user clicks the Yes button, the sheets are deleted, and the workbook is saved without the sheets.
■Note To use this code to delete the Drill to Detail sheets created with the sample code in Section 13.8, change the code to check for XDRILL instead of XSHOW.
13.10 ■ CHANGING THE PAGE FIELD SELECTION IN RELATED TABLES
13.10. Changing the Page Field Selection in Related Tables Problem In your workbook, two pivot tables are based on the same Excel list. You want to change the Salesperson page field items in both pivot tables at the same time, instead of going to each sheet and changing them individually.
Solution You can use an event procedure to change the second pivot table’s page field when the Salesperson page field is changed in the first pivot table. 1. Enter the following module level variable in the declarations area at the top of the first pivot table sheet’s module: Dim mstrPage As Variant 2. Below the declarations area, add the following code, changing the sheet name to your second pivot table’s sheet name. Also, change the page field name from Salesperson to the name of your page field: Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable) Dim wsOther As Worksheet Dim pt As PivotTable Dim strPage As String On Error GoTo err_Handler Set wsOther = Worksheets("PivotOther") 'second PT sheet name strPage = "Salesperson" 'page field name Application.EnableEvents = False If UCase(Target.PivotFields(strPage).CurrentPage) _ UCase(mstrPage) Then mstrPage = Target.PivotFields(strPage).CurrentPage For Each pt In wsOther.PivotTables pt.PageFields(strPage).CurrentPage = mstrPage Next pt End If
263
264
13.11 ■ CLEARING OLD ITEMS FROM FIELD DROPDOWN LISTS
err_Handler: Application.EnableEvents = True End Sub To run the event code, change the selection in the first pivot table’s page field.
How It Works A variable within a procedure is stored only as long as the procedure that declared it is running. A module level variable is used in this example, because it can store the current page field selection while the workbook is open. When the pivot table is updated, the variable named mstrPage is compared to the current page in the Salesperson page field. If they’re different, the new page field value is stored in the variable, and the page fields in related pivot tables are changed to match the page field in the main pivot table.
13.11. Clearing Old Items from Field Dropdown Lists Problem You deleted some records from the Excel list on which your pivot table is based, but the old items remain in the pivot field dropdown lists, and you’d like to remove them.
Solution For non-OLAP based pivot tables, you can programmatically change the pivot cache’s MissingItemsLimit property to prevent missing items from appearing. The following code sets a missing items limit of zero for each pivot cache in the active workbook. Store the procedure in a regular code module.
■Note If the MissingItemsLimit property is used in code for an OLAP-based pivot table, a runtime error can occur.
Sub SetMissingItemsLimit() Dim pc As PivotCache For Each pc In ActiveWorkbook.PivotCaches pc.MissingItemsLimit = xlMissingItemsNone pc.Refresh Next pc End Sub
13.12 ■ HIDING ALL ITEMS IN A PIVOT FIELD
To run the code, use a method described in Section 13.1. The code refers to ActiveWorkbook, so you can run the code on any workbook that contains a pivot table.
How It Works Setting the MissingItemsLimit property to 0 (xlMissingItemsNone) prevents items with no data in the pivot cache from appearing in the pivot field dropdown lists. The pivot cache is refreshed to clear any existing old items.
13.12. Hiding All Items in a Pivot Field Problem You can manually check the Show All box to show or hide all items in a pivot table field. When you record this step, the code shows a list of all the items in the pivot field, instead of using a ShowAll command. If you add new items to the field and run the macro, the new items aren’t hidden.
Solution There’s no HideAll or ShowAll command available for programming a pivot field. In some cases, the quickest way to show all items is to remove the field from the pivot table, refresh the pivot table, then add the field back to the pivot table. You can also loop through the items in the field, and show or hide them. For example, the following code hides all items, except the last item, in all row fields, in all tables on the active sheet. Store the code on a regular module. Sub HideVisiblePivotItems() Dim pt As PivotTable Dim pf As PivotField Dim pi As PivotItem Dim SortOrder As Long Dim SortField As String Dim ws As Worksheet On Error Resume Next Set ws = ActiveSheet Application.ScreenUpdating = False Application.DisplayAlerts = False
265
266
13.13 ■ CHANGING CONTENT IN THE DATA AREA
For Each pt In ws.PivotTables pt.ManualUpdate = True For Each pf In pt.RowFields SortOrder = pf.AutoSortOrder SortField = pf.AutoSortField pf.AutoSort SortOrder, SortField For Each pi In pf.PivotItems pi.Visible = False Next pi pf.AutoSort SortOrder, SortField Next pf Next pt exit_Handler: Application.ScreenUpdating = True Exit Sub err_Handler: MsgBox Err.Number & ": " & Err.Description GoTo exit_Handler End Sub To run the code, use a method described in Section 13.1. The code refers to ActiveSheet, so you can run the code on any sheet that contains a pivot table.
How It Works The HideVisiblePivotItems code uses For Each...Next loops to set the Visible property for all pivot items in all row fields.
■Tip To hide column fields, change pt.RowFields to pt.ColumnFields.
The On Error Resume Next line prevents the code from displaying an error message if it can’t change the Visible property for a pivot item. Instead, it moves to the next line of code.
13.13. Changing Content in the Data Area Problem You’d like to change some of the content in the data area of the pivot table, but when you try, you see the error message “Cannot change this part of a PivotTable report.”
13.14 ■ IDENTIFYING A PIVOT TABLE’S PIVOT CACHE
Solution If you change a PivotTable setting programmatically, you can make temporary changes to the PivotTable data. Store the code in a regular code module. Sub ChangePTData() Dim pt As PivotTable Set pt = ActiveSheet.PivotTables(1) pt.EnableDataValueEditing = True End Sub
■Note When the pivot table is refreshed, the manually entered data will be overwritten.
To run the code, use a method described in Section 13.1. The code refers to ActiveSheet, so you can run the code on any sheet that contains a pivot table.
How It Works The EnableDataValueEditing property can only be set programmatically, and it allows temporary changes to the pivot table data area cells.
13.14. Identifying a Pivot Table’s Pivot Cache Problem Several pivot tables in your workbook are based on the same Excel list, and you want to identify which pivot cache each pivot table uses.
Solution To determine which pivot cache a pivot table uses, you can run the following code to test the pivot table’s CacheIndex property and view the result in a message box. Store the code on a regular code module. Sub ViewCacheIndex() Dim pcIndex As Long On Error GoTo err_Handler pcIndex = ActiveCell.PivotTable.CacheIndex
267
268
13.14 ■ IDENTIFYING A PIVOT TABLE’S PIVOT CACHE
MsgBox "PivotCache: " & pcIndex Exit Sub err_Handler: MsgBox "Active cell is not in a pivot table" End Sub Select a cell in a pivot table, then run the code, using a method described in Section 13.1. A message box will display the CacheIndex property for the active cell’s pivot table. If the active cell is not in a pivot table, an error message is displayed.
How It Works When pivot caches are created, they are added to the workbook’s PivotCaches collection and given an index number. This number is displayed in the macro’s message box. Another option is to create a user-defined function (UDF) that returns the PivotCache index number for a range so you can see the number on a worksheet. To create a UDF, enter the following code on a regular code module: Function PTCacheIndex(rng As Range) PTCacheIndex = rng.PivotTable.PivotCache.Index End Function To use the UDF, follow these steps: 1. On a worksheet, select the cell in which you want to see the PivotCache index number. 2. From the Excel Worksheet menu, choose Insert ➤ Function. 3. In the Insert Function dialog box, from the select a category dropdown, select User Defined. 4. In the list of functions, select PTCacheIndex, and click OK. 5. In the Function Arguments dialog box, click in the Rng box. 6. Select a cell in the pivot table for which you want to see the pivot cache index number. In this example, the pivot table starts in cell A3 (see Figure 13-6).
13.15 ■ CHANGING A PIVOT TABLE’S PIVOT CACHE
Figure 13-6. Select a cell in the pivot table as the Rng argument in the UDF.
7. Click OK to close the Function Arguments dialog box and to see the function results.
13.15. Changing a Pivot Table’s Pivot Cache Problem You used the code from the previous problem to identify the pivot cache used by each pivot table in your workbook. You want to change the pivot cache of one pivot table so it uses the same cache as another pivot table.
Solution To change the pivot cache, you can set the pivot table’s CacheIndex property. The following code will set the pivot table for the active cell to use pivot cache number 2. Store the code on a regular code module. Sub SetCacheIndex() On Error GoTo err_Handler ActiveCell.PivotTable.CacheIndex = 2 Exit Sub err_Handler: MsgBox "Cache index could not be changed" End Sub To run the code, use a method described in Section 13.1. The code refers to ActiveCell, so you can run the code on any sheet that contains a pivot table.
269
270
13.16 ■ IDENTIFYING THE QUERY USED AS THE DATA SOURCE
How It Works The code sets the CacheIndex property for the active cell’s pivot table to 2. If a pivot table cell is not selected, an error message is displayed.
Notes If you have created several pivot tables in a workbook, based on the same source data, you may find it more efficient to use the same pivot cache for all the pivot tables. This uses less memory than having a separate pivot cache for each pivot table. The following code will change the pivot cache for each pivot table in the workbook, so all pivot tables use the pivot cache of the active cell’s pivot table. Store the code in a regular code module. Sub ChangeAllPivotCaches() Dim pt As PivotTable Dim ws As Worksheet On Error GoTo err_Handler For Each ws In ActiveWorkbook.Worksheets For Each pt In ws.PivotTables pt.CacheIndex = ActiveCell.PivotTable.CacheIndex Next pt Next ws Exit Sub err_Handler: MsgBox "Cache index could not be changed" End Sub Select a cell in the pivot table that uses the pivot cache you want all pivot tables to use. Then run the code, using a method from Section 13.1. Because the code refers to ActiveCell instead of a specific range, you can run the code on any worksheet that contains a pivot table.
13.16. Identifying the Query Used as the Data Source Problem Someone else built the pivot table from a Microsoft Access query, and you need to know which query they used as the source and find the path and name of the Access file.
13.16 ■ IDENTIFYING THE QUERY USED AS THE DATA SOURCE
Solution The following code enters the connection string for the active cell’s pivot table on a worksheet named Connection, where it can be edited. Store the code on a regular code module. Sub ListConnection() Dim pt As PivotTable Dim ws As Worksheet On Error GoTo err_Handler Set pt = ActiveCell.PivotTable Set ws = Worksheets("Connection") ws.Cells.ClearContents ws.Cells(2, 1).Value = pt.PivotCache.Connection ws.Cells(3, 1).Value = pt.PivotCache.CommandText Exit Sub err_Handler: MsgBox "Connection could not be shown" End Sub Then, follow these steps: 1. Before running the code, create a worksheet named Connection. 2. Select a cell in the pivot table for which you want to view the connection string, and run the ListConnection macro, using a method described in Section 13.1.
How It Works The ListConnection macro clears the cells on the Connection worksheet, and enters the connection string for the active cell’s pivot table in cell A2. In cell A3, it enters the command text string, that shows the query name and the fields that were used.
■Tip To view the strings in the cells, you can widen column A, and make rows 2 and 3 taller. Format cells A2 and A3 with Wrap Text (Format ➤ Cells, Alignment tab).
271
272
13.17 ■ REFRESHING A PIVOT TABLE ON A PROTECTED SHEET
If the database on which the pivot table is based has been moved, you may need to connect the pivot table to the database in its new location. You can modify the connection string to enter the new location for the database. The edited connection string can be used to programmatically update the pivot cache connection. 1. Add the following code to a regular code module: Sub UpdateConnection() Dim pt As PivotTable Dim ws As Worksheet On Error Resume Next Set ws = Worksheets("Connection") Set pt = ActiveCell.PivotTable pt.PivotCache.Connection = ws.Cells(2, 1).Value pt.RefreshTable End Sub 2. Run the ListConnection macro to enter the current connection string on the Connection worksheet. 3. On the Connection worksheet, edit the connection string in cell A2 to include the new path for the database. 4. Select a cell in the pivot table for which you want to change the connection. 5. Run the UpdateConnection macro to update the connection string.
13.17. Refreshing a Pivot Table on a Protected Sheet Problem You want to refresh a pivot table that’s on a protected worksheet, but the Refresh Data button on the PivotTable toolbar is disabled.
Solution You can record a macro, modify it slightly, and then run that macro when you need to refresh the pivot table.
13.17 ■ REFRESHING A PIVOT TABLE ON A PROTECTED SHEET
1. Record a macro as you unprotect the sheet, refresh the pivot table, then protect the worksheet. 2. View the recorded code in the VBE. For example, your code may look similar to the following: Sub RefreshPivot() ActiveSheet.Unprotect Range("B1").Select ActiveSheet.PivotTables("BillingPivot").PivotCache.Refresh ActiveSheet.Protect DrawingObjects:=True, _ Contents:=True, Scenarios:=True End Sub 3. In the recorded code, you can add a password to the Unprotect and Protect lines. For example, if your password is pwd, the revised code would be as follows: Sub RefreshPivot() ActiveSheet.Unprotect Password:="pwd" Range("B1").Select ActiveSheet.PivotTables("BillingPivot").PivotCache.Refresh ActiveSheet.Protect Password:="pwd", _ DrawingObjects:=True, Contents:=True, Scenarios:=True End Sub 4. Run the macro using one of the methods shown in Section 13.1.
How It Works The RefreshPivot macro stores your password, and uses it when unprotecting and protecting the worksheet. While the worksheet is unprotected, it refreshes the pivot table.
Notes If you add your password to the macro, it will be visible to anyone who can open your workbook project in the VBE. For information on protecting your code, see the article “Locking Your Solution’s VBA Project,” at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odeopg/html/ deovrlockingyoursolutionsvbaproject.asp
273
274
13.18 ■ REFRESHING AUTOMATICALLY WHEN SOURCE DATA CHANGES
13.18. Refreshing Automatically When Source Data Changes Problem You want the pivot table to automatically refresh when changes are made in the Excel list on which the pivot table is based.
Solution You can use an event procedure to automatically update the pivot table if the source data changes: 1. Add the following code to the Excel list’s worksheet module: Private Sub Worksheet_Change(ByVal Target As Range) Worksheets("Pivot").PivotTables(1).PivotCache.Refresh End Sub 2. Modify the code to include the worksheet name for your pivot table. 3. To run the event code, make a change to the data in the Excel list.
How It Works When there is a change on the Excel list’s worksheet, that sheet’s Change event is triggered. In the Event code, the specified pivot table’s pivot cache is refreshed.
13.19. Preventing Selection of (All) in a Page Field Problem You want to prevent users from choosing (All) in a page field.
Solution You can’t remove the (All) option from the page field list, but you can use programming to prevent users from selecting that option. The following code, stored on the pivot table sheet’s module, will select the first visible item in the list if (All) is selected, and displays a message warning that the (All) option is not available:
13.20 ■ DISABLING PIVOT FIELD DROPDOWNS
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable) Dim pf As PivotField Dim i As Long Application.EnableEvents = False Application.ScreenUpdating = False For Each pf In Target.PageFields If pf.CurrentPage = "(All)" Then i = 1 For i = 1 To pf.PivotItems.Count + 1 On Error Resume Next pf.CurrentPage = pf.PivotItems(i).Name If Err.Number = 0 Then Exit For End If Next i MsgBox "The (All) option is not available" End If Next pf err_Handler: Application.EnableEvents = True Application.ScreenUpdating = True End Sub To run the code, select an item from a page field dropdown list.
How It Works When an item is selected from a page field’s dropdown list, the PivotTableUpdate event is triggered. If the (All) item was selected, the code loops through the page field items, and displays the first available item.
13.20. Disabling Pivot Field Dropdowns Problem You want to prevent users from selecting a different item in the field dropdown lists.
275
276
13.21 ■ PREVENTING LAYOUT CHANGES IN A PIVOT TABLE
Solution You can use programming to disable many pivot table features. For example, the following macro will disable selection in each field in the first pivot table of the active sheet. The dropdown arrows disappear, and users can’t change the displayed items. Store the code in a regular code module.
■Note Instead of PivotFields, you can use VisibleFields, RowFields, ColumnFields, or PageFields.
Sub DisableSelection() Dim pt As PivotTable Dim pf As PivotField Set pt = ActiveSheet.PivotTables(1) For Each pf In pt.PivotFields pf.EnableItemSelection = False Next pf End Sub To run the code, use a method described in Section 13.1. The code refers to ActiveSheet, so you can run the code on any sheet that contains a pivot table.
How It Works The code sets the EnableItemSelection property to False for each field in the pivot table, even if they aren’t visible in the layout. To reenable selection, create and run a similar macro that sets the EnableItemSelection property to True.
13.21. Preventing Layout Changes in a Pivot Table Problem You want to prevent users from rearranging the pivot table layout.
Solution The Pivot Table has DragTo settings that you can change programmatically. For example, the following macro prevents dragging fields to any pivot table area or off the pivot table. Store the code in a regular code module.
13.22 ■ PREVENTING CHANGES TO THE PIVOT TABLE
■Caution After using this code, Excel may hang if you attempt to drag the Data button.
Sub RestrictPTDrag() Dim pt As PivotTable Dim pf As PivotField Set pt = ActiveSheet.PivotTables(1) For Each pf In pt.PivotFields With pf If .Name "Data" Then .DragToPage = False .DragToRow = False .DragToColumn = False .DragToData = False .DragToHide = False End If End With Next pf End Sub To run the code, use a method described in Section 13.1. The code refers to ActiveSheet, so you can run the code on any sheet that contains a pivot table.
How It Works The code stops users from dragging the pivot table fields to a different location. To allow dragging again, create and run a similar macro that sets the DragTo properties to True.
13.22. Preventing Changes to the Pivot Table Problem You want to prevent users from making changes to the pivot table, but you can’t use worksheet protection, because users need unrestricted access to other items on the worksheet.
Solution In addition to the security features discussed in the previous sections, you can programmatically control access to the following features:
277
278
13.22 ■ PREVENTING CHANGES TO THE PIVOT TABLE
• Field List • Field Settings • Refresh • Wizard • Drill to Details
The following macro turns off each of these features. Store the macro on a regular code module. Sub RestrictPTChanges() Dim pt As PivotTable Dim pf As PivotField Set pt = ActiveSheet.PivotTables(1) With pt .EnableWizard = False .EnableDrilldown = False .EnableFieldList = False .EnableFieldDialog = False .PivotCache.EnableRefresh = False End With End Sub To run the code, use a method described in Section 13.1. The code refers to ActiveSheet, so you can run the code on any sheet that contains a pivot table.
How It Works The RestrictPTChanges macro turns off many features in the first pivot table in the active sheet. To allow use of the features, run another macro that changes the settings to True.
13.23 ■ VIEWING INFORMATION ON PIVOT CACHES
13.23. Viewing Information on Pivot Caches Problem Your workbook contains several pivot tables, and you aren’t sure how many pivot caches there are, or how much memory is being used. You’d like a summary of the pivot cache information so you can make changes, if necessary, to help the workbook’s performance.
Solution The following procedure creates a new worksheet, with a list of pivot caches in the active workbook and details on each pivot cache. If multiple pivot caches use the same source data, you can set the pivot tables to use one of these caches, and the duplicate will be eliminated. Store the code in a regular code module. Sub ListPivotCacheInfo() Dim pc As PivotCache Dim wb As Workbook Dim ws As Worksheet Dim CountRow As Long CountRow = 1 Set wb = ActiveWorkbook On Error Resume Next Application.DisplayAlerts = False Worksheets("Cache_Info").Delete Application.DisplayAlerts = True On Error GoTo 0 Set ws = Worksheets.Add ws.Name = "Cache_Info" With ws .Cells(CountRow, .Cells(CountRow, .Cells(CountRow, .Cells(CountRow, .Cells(CountRow, .Cells(CountRow, .Cells(CountRow, .Cells(CountRow,
1).Value 2).Value 3).Value 4).Value 5).Value 6).Value 7).Value 8).Value
= = = = = = = =
"Cache Index" "Command Text" "Command Type" "Connection" "Memory Used (kb)" "Missing Items Limit" "Record Count" "Refresh Date"
279
280
13.23 ■ VIEWING INFORMATION ON PIVOT CACHES
.Cells(CountRow, 9).Value = "Refresh Period" .Cells(CountRow, 10).Value = "Source Data File" .Cells(CountRow, 11).Value = "Source Type" .Rows(1).EntireRow.Font.Bold = True End With CountRow = CountRow + 1 For Each pc In wb.PivotCaches With ws On Error Resume Next .Cells(CountRow, 1).Value = pc.Index .Cells(CountRow, 2).Value = pc.CommandText .Cells(CountRow, 3).Value = pc.CommandType .Cells(CountRow, 4).Value = pc.Connection .Cells(CountRow, 5).Value = pc.MemoryUsed / 1000 .Cells(CountRow, 6).Value = pc.MissingItemsLimit .Cells(CountRow, 7).Value = pc.RecordCount .Cells(CountRow, 8).Value = pc.RefreshDate .Cells(CountRow, 9).Value = pc.RefreshPeriod .Cells(CountRow, 10).Value = pc.SourceDataFile .Cells(CountRow, 11).Value = pc.SourceType End With CountRow = CountRow + 1 Next pc End Sub To run the code, use a method described in Section 13.1. The code refers to ActiveWorkbook, so you can run the code on any workbook that contains a pivot table.
How It Works The ListPivotCacheInfo macro deletes the Cache_Info sheet, if one exists. Then, it adds a new sheet, and names it Cache_Info. Headings are entered in the first row of the new sheet, and details on the workbook caches are listed in the following rows.
13.24 ■ RESETTING THE PRINT AREA TO INCLUDE THE ENTIRE PIVOT TABLE
13.24. Resetting the Print Area to Include the Entire Pivot Table Problem Your pivot table is on a worksheet that contains other data. The pivot table frequently changes size, and you have to reset the print area every time you want to print it.
Solution Use programming to reset the print area automatically before printing. Add the following code to a regular code module: Sub SetPivotPrintArea() Dim ws As Worksheet Dim pt As PivotTable Set ws = ActiveSheet Set pt = ws.PivotTables(1) With ws.PageSetup .PrintTitleRows = "" .PrintTitleColumns = "" .PrintArea = pt.TableRange2.Address End With pt.PrintTitles = True ws.PrintOut Preview:=True End Sub To run the code, use a method described in Section 13.1. The code refers to ActiveSheet, so you can run the code on any sheet that contains a pivot table.
How It Works The SetPivotPrintArea macro clears the print titles for the active sheet, sets the print area based on the current layout of the pivot table, turns on the pivot table’s print titles options, and prints the worksheet. In this example, the ws.PrintOut line has Preview set to True, so the worksheet will preview instead of printing. When you’ve finished testing the code, you can change the setting to False, and the sheet will print when the code runs.
281
282
13.25 ■ PRINTING THE PIVOT TABLE FOR EACH PAGE FIELD
13.25. Printing the Pivot Table for Each Page Field Problem You want to print a copy of the pivot table for each page in your Pivot table.
Solution The following code will print the pivot table once for each item in the first page field. Store the code in a regular code module. Sub PrintPivotPages() Dim pt As PivotTable Dim pf As PivotField Dim pi As PivotItem Dim ws As Worksheet On Error Resume Next Set ws = ActiveSheet Set pt = ws.PivotTables(1) Set pf = pt.PageFields(1) For Each pi In pf.PivotItems pt.PivotFields(pf.Name).CurrentPage = pi.Name ws.PrintOut Preview:=True Next pi End Sub To run the code, use a method described in Section 13.1. The code refers to ActiveSheet, so you can run the code on any sheet that contains a pivot table.
How It Works The PrintPivotPages macro selects each item in the pivot table’s first page field, then prints the worksheet. In this example, the ws.PrintOut line has Preview set to True, so the worksheet will preview instead of printing. When you’ve finished testing the code, you can change the setting to False, and the sheet will print when the code runs.
Notes The procedure can be modified slightly to print a copy of a pivot chart on a chart sheet for each item in the first page field. Store the code on a regular code module.
13.26 ■ REFORMATTING PIVOT CHARTS AFTER CHANGING THE PIVOT TABLE
Sub PrintPivotChartPages() Dim pt As PivotTable Dim pf As PivotField Dim pi As PivotItem Dim ch As Chart On Error Resume Next Set ch = ActiveChart Set pt = ch.PivotLayout.PivotTable Set pf = pt.PageFields(1) For Each pi In pf.PivotItems pt.PivotFields(pf.Name).CurrentPage = pi.Name ch.PrintOut Preview:=True Next pi End Sub
13.26. Reformatting Pivot Charts After Changing the Pivot Table Problem When you refresh the pivot table on which the pivot chart is based, the series formatting in the chart is lost.
Solution You can record a macro as you format the pivot chart series. Then, run the macro after changing the pivot table. To run the macro automatically, you can use the PivotTableUpdate event to trigger it. For example, to run a recorded procedure named ReformatChart when the pivot table is changed, store the following code on the pivot table’s worksheet module: Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable) ReformatChart End Sub To run the code, refresh or change the pivot table.
How It Works The PivotTableUpdate event procedure runs the ReformatChart macro that contains your recorded code to reformat the pivot chart.
283
284
13.27 ■ SCROLLING THROUGH PAGE FIELD ITEMS ON A PIVOT CHART
13.27. Scrolling Through Page Field Items on a Pivot Chart Problem You have a long list of customers in the Pivot Chart page field. Instead of selecting the next customer from the dropdown list to view its chart, you’d like scrolling buttons on the chart so you can quickly view each customer’s data.
Solution You can add buttons from the Forms toolbar, and assign a macro to each button, then click the buttons to select the next or previous page field 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(1) 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
13.27 ■ SCROLLING THROUGH PAGE FIELD ITEMS ON A PIVOT CHART
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 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(1) 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 End Sub
285
286
13.27 ■ SCROLLING THROUGH PAGE FIELD ITEMS ON A PIVOT CHART
2. On the Excel worksheet menu, choose View ➤ Toolbars ➤ Forms. 3. On the Forms toolbar, use the Button tool to add two buttons at the top of the pivot chart sheet. For the first button, when the Assign Macro dialog box appears, select PivotPagePrev. For the second button, select PivotPageNext. 4. Right-click each button, and select Edit Text. On the first button, type (see Figure 13-7). 5. To scroll through the page field items, click the button.
Figure 13-7. Scrolling buttons at the top of a pivot chart
How It Works The two macros check the current page field item and calculate its position in the list of items. If the button is clicked, the PivotPageNext macro runs, and the visible item with the next higher number is shown.
Index ■Symbols
& (ampersand), specifying connection properties, 121 ? (question mark), specifying connection properties, 121
■A
absolute(int) method, scrollable ResultSets, 215 Access URL formats, 94 ACID (atomicity, consistency, isolation, and durability), 47 addBatch(String sql) method (Statement object), 403, 408 afterLast( ) method, scrollable ResultSets, 210, 215 ALTER command, 14 ampersand (&), specifying connection properties, 121 ANSI (American National Standards Institute), 13 Apache DbUtils package, 598–604 core classes/interfaces, 598 design goals, 598 example usage, 598–604 package components, 598 Web sites, 598, 602–603 Avalon/Excalibur, 51 connection pool, 55 datasource, 55 Commons Pool project, 55 DBCP component, 51 API, 26 debugging, 75–76 environment settings, 73 application component (ODBC), 7 application server tier (three-tier model), 84 application performance improvement, 51–63 avoiding generic search patterns, 57 avoiding memory leaks, 61–63 caching PreparedStatement objects, 61 driver selection, 52 managing database Connection objects, 53–57 minimizing database metadata methods usage, 52 retrieving only required data, 58–61 simplifying SQL queries, 52
architecture BCM (Basic Connection Management), 108 JDBC API, 26 detailed architecture, 3–5 high-level architecture, 3 JDBC-ODBC bridge, 8 ODBC architecture, 6–7 ArrayDescriptor, 497 arrays materializing BLOB data, 235 passing to PreparedStatement, 495–498 byte arrays, 526–530 Oracle, 496–498 AsciiStream, 498. See also InputStream getAsciiStream( ) method (Java.sql.Clob interface), 258 setAsciiStream(long pos) method (Java.sql.Clob interface), 258 atomic, 46 atomicity, consistency, isolation, and durability (ACID), 47 attributes, user-defined types (UDTs), 134 autocommit mode, 48, 88 autocommit rules, 134 choosing optimal transaction isolation levels, 57 Connection object, 132–134 disabling autocommits, 118–119, 133 enabling autocommits, 133 Oracle transaction support, 78 starting/ending transactions, 48–49 turning on/off, 48 autoReconnect parameter (Connection object), 130–131 AUTO_INCREMENT (Statement), 388–391 Avalon, 51 connection pool, 55 datasource, 55
■B
Basic Connection Management (BCM), 108–113 accessing method 1, 109–110 method 2, 110–112 architecture, 108 connection factories, 113 287
288
■INDEX
ConnectionManager, 112 package features, 112 using with your own values, 109 BasicConnectionManager class MySQL, 101–102 Oracle, 98–100 batch statements, executing with Statement, 403–408 mapping trees into SQL, 415–430 deleting existing nodes, 420–427 deleting existing subtrees, 427–430 inserting new nodes, 416–420 MySQL, 407–408 Oracle, 405–407 batch updates, 59–61. See also BatchUpdateException; batch statements JDBC batch update support, 414–415 BatchUpdateException, 43, 341, 364–373 constructors, 365 method, 365 MySQL, 368, 372 Oracle, 369, 373 BCM (Basic Connection Management), 108–113 accessing method 1, 109–110 method 2, 110–112 architecture, 108 connection factories, 113 ConnectionManager, 112 package features, 112 using with your own values, 109 BDB tables, 77 BEA WebLogic Server, 84 beforeFirst( ) method, scrollable ResultSets, 215 BigDecimal formatting, 593–594 passing to PreparedStatement, 502–505 MySQL, 504–505 Oracle, 502–504 binary data Binary Large OBjects. See BLOBs getting, using Statement, 402 inserting, using Statement, 401–402 Binary Large OBjects. See BLOBs binary streams, passing to PreparedStatement, 506–512 MySQL, 511–512 Oracle, 506–511 BLOBs (Binary Large OBjects), 194, 231–233 byte[ ] vs. java.sql.Blob, 256 creating objects, 234–235 deleting MySQL, 245–246 Oracle, 243–245 java.sql.Blob interface, 231–233
materializing data, 235–236 MySQL BLOBs, 233 Oracle BLOBs, 233 passing to PreparedStatement, 513–518 MySQL, 517–518 Oracle, 514–517 restrictions, 234 retrieving BLOB data types, 240–243 serializing objects MySQL, 252–256 Oracle, 246–252 tables creating, 233–234 defining BLOB data types, 233 getting BLOB data, 194–198 inserting records, 236–237, 240 BlobSelect( ), 243 books about JDBC topics, 74 boolean values, passing to PreparedStatement, 519–522 MySQL, 521–522 Oracle, 520–521 bootstrap process, 18 borrowing Connection objects from connection pool managers, 131–132 bridges, 6 JDBC-ODBC bridge, 8–13 architecture, 8 debugging, 13 JDBC 2.0 API features supported, 13 packages, 8, 10–13 using, 9–13 byte arrays converting input streams to byte arrays, 586–587 materializing BLOB data, 235 passing to PreparedStatement, 526–530 MySQL, 529–530 Oracle, 528–529 byte data type, passing to PreparedStatement, 522–526 MySQL, 525–526 Oracle, 524–525 byte[ ] versus java.sql.Blob, 256
■C
caching PreparedStatement objects, 61 Calendar. See also Date conversion of Java Date classes to Calendar, 327 creating java.util.Date objects, 315 getting dates for specific time zones, 340 making java.sql.Timestamp objects, 339–340 CallableStatement object, 27 batch updates, 60–61 cancelRowUpdates( ) method, updatable ResultSets, 221–222
■INDEX
MysqlConnectionFactory (BCM), 109, 112–113 OdbcConnectionFactory (BCM), 109, 112 OracleConnectionFactory (BCM), 109, 112–113 singleton classes, 109 SQLException, 341, 344–345 chaining, 350–351 checked vs. unchecked exceptions, 346 constructors, 345 example usage, 348–349 getting all instances using printSQLExceptions( ), 351–352 getting details, 349–350 methods, 345 relationship to other classes, 346–347 utilities. See utilities CLASSPATH environmental variable, 73 client tier (three-tier model), 84 CLOBs (Character Large OBjects), 199, 257–259 ClobSelectMySQL( ), 273 creating objects, 261–262 deleting, using servlets MySQL, 303–305 Oracle, 300–303 inserting, using servlets MySQL, 290–292 Oracle, 285–290 java.lang.String vs. java.sql.Clob, 305 materializing data, 262–265 MySQL, 259 defining, 260 Oracle, 259 defining, 259–260 passing to PreparedStatement, 534–538 MySQL, 537–538 Oracle, 535–537 restrictions for using, 261 selecting/displaying CLOBs JFrames, 270–274 servlets, MySQL, 277–279, 284–285 servlets, Oracle, 274–277, 280–283 tables creating, 260 defining CLOB data types, 259–260 getting CLOB data, 199 inserting records, 265–270 updating, using servlets MySQL, 297–300 Oracle, 293–297 closing Connection objects, 25, 131–132, 569–575 closing ResultSet, Statement, and Connection together, 580–581 closing Statement and Connection together, 579–580 committing and closing, 571–573
Find it faster at http://superindex.apress.com/
catch blocks, 43 catch clauses, 343–344 certification program for JDBC drivers, 82–83 chaining (SQLException), 350–351 Character Large OBject. See CLOBs character streams, passing to PreparedStatement, 530–534 MySQL, 533–534 Oracle, 532–533 checked vs. unchecked exceptions, 346 CheckJDBCInstallation, 148–151 Class.forName( ), loading JDBC drivers, 29 classes BasicConnectionManager MySQL, 101–102 Oracle, 98–100 ConnectionManager, 108–109 core JDBC classes/interfaces, 26–27 DbUtils core classes/interfaces, 598 DriverManager creating connections, 91–95 function of, 90 getConnection( ) method, 91 getDrivers( ) method, 95–97 DriverPropertyInfo, 103–106 java.lang.Error, 347 java.lang.Exception, 341–344, 347 java.lang.Throwable, 347 java.sql.BatchUpdateException, 341, 364–373 constructors, 365 method, 365 MySQL, 368, 372 Oracle, 369, 373 java.sql.DataTruncation, 341, 373–375 constructor, 375 creating/using DataTruncation objects, 375–379 determining whether DataTruncation has occurred, 374 methods, 375 using with ResultSet, 379 java.sql.Date. See Date java.sql.SQLWarning, 341, 352–356. See also warnings constructors, 352 creating/traversing warnings, 355–356 determining whether warnings have occurred, 353–355 getting all instances, 353 methods, 353 java.sql.Time. See Time java.sql.Timestamp. See Timestamp java.text.SimpleDateFormat, 323 java.util.Date. See Date JDBC detailed architecture, 5 MapListHandler, 602–603
289
290
■INDEX
logging exceptions, 570–574 not reporting exceptions, 570–574 reporting exceptions, 571–575 rolling back and closing, 573–575 soft closing using pool managers, 570 PreparedStatement objects, 24, 578–579 resources, avoiding memory leaks, 61–63 ResultSet objects, 23, 575–576 closing ResultSet, Statement, and Connection together, 580–581 Statement objects, 24, 576–578 closing ResultSet, Statement, and Connection together, 580–581 closing Statement and Connection together, 579–580 columns, 6. See also tables CLOBs, restrictions for using, 261 returning long columns, using utilities, 582–583 command lines, loading JDBC drivers using System.properties( ), 32 commands, SQL commands, 13–14 commit. See also Autocommit COMMIT command, 14 commit method (Connection object), 48 controlling commit updates, 118–119 handling commit( ) when exceptions occur, 44 committing and closing Connection objects, 571–573 concurrency, (ResultSets), 180 getting/setting, 181 connect( ) method (Driver interface), 106–108 connecting connecting to databases MySQL, 100–102 Oracle, 97–100 SQL Server, 94 creating connections using DataSource, 153–156 obtaining connections with JNDI, 166–167 obtaining connections without JNDI, 165–166 drivers MySQL JDBC drivers, 66–69 Oracle JDBC drivers, 64–65 existing databases, 17–18 maintaining connections (autoReconnect parameter, Connection object), 130–131 maximum connections limits, 145 restricting max connections in MySQL databases, 146–147 restricting max connections in Oracle databases, 145–146
multiple database connections, 124–125 SYSDBA and SYSOPER connections (Oracle), 147 URL syntax, 38–39 Connection object, 88 application optimization choosing optimal transaction isolation levels, 56–57 controlling transactions, 55–56 setting optimal Connection properties, 53 using connection pools, 54 autocommit mode, 48, 88, 132–134 autocommit rules, 134 disabling autocommits, 118–119, 133 enabling autocommits, 133 autoReconnect parameter, 130–131 Basic Connection Management (BCM), 108–113 accessing, method 1, 109–110 accessing, method 2, 110–112 architecture, 108 ConnectionManager, 112 MysqlConnectionFactory, 113 OracleConnectionFactory, 113 package features, 112 using with your own values, 109 borrowing Connection objects from connection pool managers, 131–132 checking for SQL warnings, 119–120, 354 closing, 25, 131–132, 569–575 closing ResultSet, Statement, and Connection together, 580–581 closing Statement and Connection together, 579–580 committing and closing, 571–573 logging exceptions, 570–574 not reporting exceptions, 570–574 reporting exceptions, 571–575 rolling back and closing, 573–575 soft closing using pool managers, 570 commit method, 48 connecting to databases MySQL, 100–102 Oracle, 97–100 SQL Server, 94 connection pooling. See connection pooling controlling commit and rollback updates, 118–119 creating connections using DataSource, 153–156 obtaining connections with JNDI, 166–167 obtaining connections without JNDI, 165–166 creating, using Driver interface, 106–108 creating, using DriverManager class, 91–95
■INDEX
converting input streams to byte arrays, 586–587 counting rows, using Statement, 401 CREATE command, 14 CREATE DATABASE statement, 15 CREATE TABLE statement, 19 creating databases, 15–16 creating DataSource objects, 156 MySQL, 156–157 without JNDI, 165–166 Oracle, 156 without JNDI, 165 using data source factory (DSF) objects, 160 using relational databases, 157–159 without JNDI, 165–166 creating simple JDBC programs, 11–13 cursors, 175 choosing, 58–59 REF CURSOR objects, passing to PreparedStatement, 553–557 Oracle, 556–557 ResultSets getting cursor position, 216–217 getting number of rows, 217–218 moving (scrolling methods), 214–216 troubleshooting running out of cursors, 61–63 custom type mapping, 136–144 implementing SQLData, 137–138 Oracle database preparation, 136–137 using a Connection’s type map, 138 writing new records, 138–144
■D
Data Definition Language (DDL), 13–14 Data Manipulation Language (DML), 13–14 Data Source Administrator, 11 data source factory (DSF) objects, 160 data sources, ODBC, 7 data types BLOBs. See BLOBs CLOBs. See CLOBs Date. See Date DATETIME (MySQL), 310 Double data type formatting, 591–594 passing to PreparedStatement, 542–545 int, formatting, 590–591 OBJECT deleting OBJECTs from Oracle tables, 413–414 inserting OBJECT values into Oracle tables, 410–411 retrieving OBJECT values from Oracle tables, 411–413
Find it faster at http://superindex.apress.com/
loading drivers, 91–92 making connections, method 1, 92–94 making connections, method 2, 94–95 making connections, method 3, 95 determining the driver that created a connection, 117–118 determining whether databases accept transactions, 113–115 DriverManager class creating connections, 91–95 function of, 90 getting list of loaded drivers, 95–97 five ways to create Connection objects, 89–90 function of DriverManager class, 90 limiting rows returned from SQL servers, 115–117 listing available parameters for creating connections, 103–106 managing connection objects for application optimization, 53–57 optimal connection properties, 53 using connection pools, 54–55 MySQL connection properties passing using database URLs, 122 passing using java.util.Properties, 121–122 passing using java.util.Properties and database URLs, 122–123 preventing timeouts using autoReconnect, 130–131 Oracle connection properties connecting as SYSDBA and SYSOPER, 147 passing using DriverManager getConnection( ) method, 123–124 relationship of Connection to other objects, 88–89 ResultSet holdability getting, 183 setting, 182 rollback method, 48 testing connection validity, 125–126, 129 type mapping. See type mapping connection pool managers, 1 connection pooling, 1, 49–51 application optimization, 54–55 pool managers, 49 borrowing Connection objects from, 131–132 soft closing Connection, 570 Connection.createStatement( ), 383 ConnectionManager class, 108–109 Connector/J, JDBC URL format, 594–597 constructors BatchUpdateException, 365 java.sql.DataTruncation, 375 java.sql.SQLWarning, 352 SQLException, 345
291
292
■INDEX
passing input parameters to PreparedStatement. See passing input parameters to PreparedStatement Time. See Time Timestamp. See Timestamp user-defined types (UDTs), 134. See also type mapping data-server tier (three-tier model), 84 database management system (DBMS), 6 database metadata, 45–46 DatabaseMetaData.getDriverVersion( ), 597 DatabaseMetaDataTool, 45 determining database support for scrollable ResultSets, 212 generic search patterns, 57 maximum connections limits, 145 MySQL, 146–147 Oracle, 145–146 minimizing methods usage, 52 database transactions, 47 databases, 6 connecting to. See connections; Connection object controlling commit and rollback updates, 118–119 creating, 15–16 creating DataSource objects, 157–160 determining whether databases accept transactions, 113–115 disconnecting, 131–132 dropping, 16 key JDBC concepts MySQL databases, 72–73 Oracle databases, 69–71 Northwind, 10 populating, 22 registering, 11 relational databases, 6 relationship with JDBC drivers, 37 DataSource object, 153 creating connections, 153–156 with JNDI, 166–167 without JNDI, 165–166 creating DataSource objects, 156 MySQL, 156–157, 165–166 Oracle, 156, 165 using data source factory (DSF) objects, 160 using relational databases, 157–159 without JNDI, 165–166 deploying/registering, 161 registering objects using file-based systems, 162–164 retrieving deployed/registered DataSource objects, 164–165 interface implementations, 154
invoking getDataSource( ) with specific users/passwords, 157–158 invoking getDataSource( ) without specific users/passwords, 158–159 life cycle of DataSource objects, 155 obtaining connections with JNDI, 166–167 obtaining connections without JNDI, 165–166 properties, 154, 160–161 DataTruncation, 43, 341, 373–375 constructor, 375 creating/using DataTruncation objects, 375–379 determining whether DataTruncation has occurred, 374 methods, 375 using with ResultSet, 379 Date, 307–308. See also Time; Timestamp adding/subtracting days for given dates, 327–328 checking for leap years, 325–326 converting conversion of Java Date classes, 326–327 current time to java.sql.Date objects, 322 Java Date classes to Calendar, 327 java.sql.Date to java.util.Date, 322–323 java.sql.Timestamp objects to java.util.Date objects, 335 java.util.Date objects to java.sql.Date objects, 316–317 java.util.Date to HH:MM:SS strings, 325 String dates to java.sql.Date objects, 319–320 String dates to java.util.Date objects, 317 Timestamp to Month-Day-Year strings, 331–332 creating java.util.Date objects, 314–315 from Year, Month, and Day format, 318–319 using java.sql.Date.valueOf(), 315 using java.util.Calendar, 315 using java.util.Date, 314 using java.util.GregorianCalendar, 315 creating yesterday’s date from String dates, 317–318 determining day of week from java.util.Date objects, 322 determining validity of format patterns for SimpleDateFormat, 332 finding difference between two given dates, 328–331 getting current date as a java.util.Date object, 314 using utilities, 587 getting date labels from java.sql.Timestamp objects, 333–335 getting dates for specific time zones, 340
■INDEX
deserializing objects, 246–248, 252 detailed architecture of JDBC, 3–5 disabling autocommits, 118–119, 133 disconnecting databases, 131–132 displaying BLOBs, 240–243 CLOBs JFrames, 270–274 servlets, MySQL, 277–279, 284–285 servlets, Oracle, 274–277, 280–283 ResultSet objects, 606–608 SQLException objects, 605 SQLWarning objects, 605–606 distributed transactions MySQL support, 77 Oracle support, 78 DML (Data Manipulation Language), 13–14 doGet( ), CLOBs, 283, 288, 296, 302 doPost( ), CLOBs, 303 Double data type formatting, 591–594 passing to PreparedStatement, 542–545 MySQL, 544–545 Oracle, 543–544 downloading JDBC drivers, 34 Driver interface creating connections, 106–108 loading JDBC drivers, 30 DriverManager, 7, 18, 27 creating connections, 91–95 loading drivers, 29, 91–92 making connections, method 1, 92–94 making connections, method 2, 94–95 making connections, method 3, 95 function of, 90 getConnection( ) method, 91 passing additional Oracle connection properties to drivers, 123–124 getDrivers( ) method, 95–97 tracing JDBC operations, 75–76 DriverManager.registerDriver( ), loading JDBC drivers, 29 DriverPropertyInfo class, 103–106 drivers. See also DriverManager building URLs in JDBC driver format, 594, 597 connecting to existing databases, 17–18 core functionality, 79 MySQL, 79–81 Oracle, 81–82 criteria for selection, 37–38 determining the driver that created a connection, 117–118 driver names, 38 driver relationship with databases, 37 driver types, 34–37
Find it faster at http://superindex.apress.com/
java.sql.Date.valueOf( ) method, 315 leap years, checking for, 325–326 mapping, 308 MySQL, 310 normalization, 335–339 Oracle, 310–314 passing to PreparedStatement, 538–541 MySQL, 540–541 Oracle, 539–540 retrieving, 309–310 SimpleDateFormat, 323 DATETIME (MySQL), 310 DBMS (database management system), 6 DbUtils package, 598–604 core classes/interfaces, 598 design goals, 598 example usage, 598–604 package components, 598 Web sites, 598, 602–603 DDL (Data Definition Language), 13–14 debugging debugging supported by JDBC-ODBC bridge, 13 exceptions getting all SQLException instancesusing printSQLExceptions( ), 351–352 getting details of SQLException, 349–350 JDBC programs, 75–76 ResultSet objects, 606–608 SQLException objects, 605 SQLWarning objects, 605–606 defining tables BLOBs, 233 CLOBs, 259–260 Date/Time/Timestamp (Oracle), 311 deleteRow( ) method, updatable ResultSets, 226–229 deleting batch updates, 59–61 BLOBs, 243–245 MySQL databases, 245–246 Oracle databases, 243–245 CLOBs, using servlets MySQL, 303–305 Oracle, 300–303 DELETE, 14 nodes, 420–423, 426–427 OBJECTs from Oracle tables, 413–414 rows, using Statement, 400 subtrees, 427–430 deploying DataSource objects, 161 problems with file-based DataSource objects, 163–164 registering objects using file-based systems, 162–163 retrieving deployed/registered DataSource objects, 164–165
293
294
■INDEX
getting JDBC driver version number, 597 JDBC, 13, 27–38 driver certification program, 82–83 driver database, 83 loading, 29–32, 91–92 getting list of loaded drivers, 95–97 using utilities, 588 MySQL, 65–69 connection properties, 121–123 core functionality, 79–81 obtaining, 34 ODBC, 6–7 ODBC bridges, 6 Oracle, 63–65 connection properties, 123–124, 147 core functionality, 81–82 selecting the right JDBC driver, 52 testing installations, 32–34 types of drivers, 52 Web site listing, 32 writing, 82 DROP command, 14 dropping databases, 16 tables, using Statement, 388 DSF (data source factory) objects, creating DataSource objects, 160
■E
empty_clob( ) function, 270 enabling autocommits, 133 ending transactions, 48–49 environment settings for JDBC APIs, 73 Error class, 347 errors. See also exceptions; warnings handling, 42–44 java.lang.Error, 347 Excalibur, 51, 55 Excel reading/extracting data from, 200–203 writing data to, 203–205 exceptions, 341–342 closing Connection logging exceptions, 570–574 not reporting exceptions, 570–574 reporting exceptions, 571–575 closing PreparedStatement logging exceptions, 578–579 not reporting exceptions, 576–578 reporting exceptions, 579 closing ResultSet logging exceptions, 576 not reporting exceptions, 575–576 reporting exceptions, 576
closing ResultSet, Statement, and Connection together not reporting exceptions, 580–581 reporting exceptions, 581 closing Statement logging exceptions, 577 not reporting exceptions, 577 reporting exceptions, 577–578 closing Statement and Connection together not reporting exceptions, 579–580 reporting exceptions, 580 common exceptions, 342 debugging/displaying SQLException objects, 605 exception object components, 344 handling, 42–44, 343–344 java.lang.Exception, 341–344, 347 java.sql.BatchUpdateException, 341, 364–373 constructors, 365 method, 365 MySQL, 368, 372 Oracle, 369, 373 java.sql.DataTruncation, 341, 373–375 constructor, 375 creating/using DataTruncation objects, 375–379 determining whether DataTruncation has occurred, 374 methods, 375 using with ResultSet, 379 java.sql.SQLWarning, 341, 352–353 constructors, 352 creating/traversing warnings, 355–356 determining whether warnings have occurred, 353–355 getting all instances, 353 methods, 353 SQLException, 341, 344–345 chaining, 350–351 checked vs. unchecked exceptions, 346 constructors, 345 example usage, 348–349 getting all instances using printSQLExceptions( ), 351–352 getting details, 349–350 methods, 345 relationship to other classes, 346–347 SQLState codes returned by SQLException.getSQLState( ), 358–364 code formats, 358 MySQL codes, 364 Oracle codes, 359–363 stack traces, 344 wrapping, 357–358
■INDEX
■F
fat client, 84 fetch size, 115 getting, 116–117 setting, 116 using Statement, 395–396 fields returning long columns/fields using utilities, 582–583 storing long text fields using utilities, 583–584 file-based systems, registering DataSource objects, 162–163 problems with file-based DataSource objects, 163–164 retrieving deployed/registered DataSource objects, 164–165 first( ) method, scrollable ResultSets, 215 float data type, passing to PreparedStatement, 542–545 MySQL, 544–545 Oracle, 543–544 formatting Double data types, 591–593 objects, 594 integers int data type, 590–591 Integer objects, 591 java.math.BigDecimal objects, 593–594 strings, 588–590 FreeTDS, 82 functional problems, 51
■G
generating random GUIDs, 608, 613 getAsciiStream( ), Java.sql.Clob, 258
getBinaryStream( ), Java.sql.Blob, 232 getBLOB( ), 242 getBlob(int columnPosition), 234–235 getBlob(String columnName), 235 getBytes(long pos, int length), Java.sql.Blob interface, 232 getCharacterStream( ), Java.sql.Clob interface, 258 getCLOB( ), 199, 261–262, 273 getClobAsURL( ), 282 getClobsContentAsString( ), 297 getConcurrency( ), 219–220 getConnection( ) CLOBs, 272, 282, 288, 292, 296, 300, 302 DriverManager, 91 passing additional Oracle connection properties to drivers, 123–124 getDataSize( ), java.sql.DataTruncation, 375 getDataSource( ). See also DataSource object invoking with specific users/passwords, 157–158 invoking without specific users/passwords, 158–159 getDate( ), 309 getDrivers( ), DriverManager class, 95–97 getIndex( ), java.sql.DataTruncation, 375 getNextWarning( ), 353 getParameter( ), java.sql.DataTruncation, 375 getRead( ), java.sql.DataTruncation, 375 getSqlDriver method, 18 getSubString(long pos, int length), Java.sql.Clob interface, 258 getTime( ), 309 getTimestamp( ), 310 getting. See retrieving getTransferSize( ), java.sql.DataTruncation, 375 getType( ), 214 getUpdateCounts( ), BatchUpdateException, 365 global transactions MySQL support, 77 Oracle support, 78 globally unique identifier (GUID) primary keys, generating, 608, 613 GRANT command, 14 GregorianCalendar checking for leap years, 326 creating java.util.Date objects, 315 making java.sql.Timestamp objects for a given Year, Month, Day, Hour, 339–340 grouping restrictions BLOBs, 234 CLOBs, 261
Find it faster at http://superindex.apress.com/
executeBatch( ) method (Statement object), 403, 414–415 executing statements batch statements, using Statement, 403–405 JDBC batch update support, 414–415 mapping trees into SQL. See mapping trees into SQL using batch updates MySQL, 407–408 Oracle, 405–407 executing SQL statements, using Statement, 386–387 explicit transactions, 49 extracting BLOBs, 240–243 CLOBs JFrames, 270–274 servlets, MySQL, 277–279, 284–285 servlets, Oracle, 274–277, 280–283
295
296
■INDEX
■H
JDBC detailed architecture, 5 utilities. See utilities International Organization for Standardization (ISO), 13 Internet architecture, 85–86 ISAM tables, 77 ISO (International Organization for Standardization), 13
■I
Jakarta DbUtils package, 598–604 core classes/interfaces, 598 design goals, 598 example usage, 598–604 package components, 598 Web sites, 598, 602–603 java.lang.Error, 347 java.lang.Exception, 341–344, 347 java.lang.String vs. java.sql.Clob, 305 java.lang.Throwable, 347 java.math.BigDecimal formatting, 593–594 passing to PreparedStatement, 502–505 MySQL, 504–505 Oracle, 502–504 java.sql, 1 java.sql.Array. See arrays java.sql.BatchUpdateException, 341, 364–373 constructors, 365 method, 365 MySQL, 368, 372 Oracle, 369, 373 java.sql.Blob interface, 231–233. See also BLOBs methods, 232 MySQL BLOBs, 233 Oracle BLOBs, 233 versus byte[ ], 256 java.sql.Connection object. See Connection object; connections java.sql.DatabaseMetaData, 45 java.sql.DataTruncation, 341, 373–375 constructor, 375 creating/using DataTruncation objects, 375–379 determining whether DataTruncation has occurred, 374 methods, 375 using with ResultSet, 379 java.sql.Date. See Data java.sql.Driver. See Driver interface java.sql.ParameterMetaData, 45 java.sql.ResultSetMetaData, 45 java.sql.SQLException. See SQLException
handling errors/exceptions, 42–44, 343–344. See also exceptions HEAP tables, 77 high-level architecture of JDBC, 3 holdability (ResultSets), 181 checking, 182–183 getting, 183 setting, 182 IBM URL formats, 94 IBM WebSphere, 84 InnoDB tables, 77 input streams converting to byte arrays, 586–587 materializing BLOB data, 235 CLOB data, 263 passing InputStream to PreparedStatement, 498–502 MySQL, 501–502 Oracle, 500–501 insensitive scrollable ResultSets, 213 creating, 383–384 inserting batch updates, 59–61 binary data, using Statement, 401–402 CLOBs, using servlets MySQL, 290–292 Oracle, 285–290 INSERT command, 14 nodes, 416–420 OBJECT values into Oracle tables using JDBC, 410–411 using SQL*Plus, 410 records BLOBs, 236–237, 240 CLOBs, 265–270 Oracle Date/Time types, 312 rows, using Statement, 399 SQL table data, 15 insertRow( ) method, updatable ResultSets, 222–226 installations, checking, 148–151 integers formatting int data type, 590–591 Integer objects, 591 passing int data type to PreparedStatement, 522–526 MySQL, 525–526 Oracle, 524–525 interfaces core JDBC classes/interfaces, 26–27 DbUtils core classes/interfaces, 598
■J
■INDEX
driver relationship with databases, 37 driver types, 34–37 getting driver version number, 597 loading, 29–32 creating an instance of a Driver class, 30 using a Thread class, 32 using Class.forName( ), 29 using DriverManager.registerDriver( ), 29 using System.properties( ) from command lines, 32 using System.properties( ) inside programs, 30–32 using utilities, 588 MySQL, 65–66 connecting with, 66–69 core functionality, 79–81 registering, 66 MySQL connection properties passing additional properties using java.util.Properties, 121–122 passing additional properties using java.util.Properties and database URLs, 122–123 obtaining, 34 Oracle, 63–65 core functionality, 81–82 KPRB driver, 65 OCI driver, 64–65 Thin driver, 63–65 Oracle connection properties connecting as SYSDBA and SYSOPER, 147 passing additional properties using DriverManager’s getConnection( ) method, 123–124 selecting the right driver, 52 testing installations MySQL, 32–33 Oracle, 33–34 types of drivers, 52 Web site listing, 32 writing, 82 JDBC installations, checking, 148, 150–151 JDBC Internet architecture, 85–86 JDBC programming, 15–25 closing objects, 23 Connection, 25 PreparedStatement, 24 result sets, 23 Statement, 24 connecting to existing databases, 17–18 creating databases, 15–16 tables, 19–21 dropping databases, 16 populating databases, 22
Find it faster at http://superindex.apress.com/
java.sql.SQLWarning, 341, 352–353 constructors, 352 creating/traversing warnings, 355–356 debugging/displaying SQLWarning objects, 605–606 determining whether warnings have occurred, 353–355 getting all instances, 353 methods, 353 java.sql.Time. See Time java.sql.Timestamp. See Timestamp java.util.Calendar, 315 java.util.Date. See Data java.util.GregorianCalendar, 315 java.util.Map object. See type mapping java.util.Properties, passing additional properties to JDBC drivers (MySQL), 121–123 JavaBeans, DbUtils package, 599–601 javax.sql, 2 javax.sql.RowSetMetaData, 45 jcb.bcm. See BCM (Basic Connection Management) JDBC, 1–3 architecture detailed, 3–5 high-level, 3 core JDBC classes/interfaces, 26–27 defined, 1–2 environment settings for APIs, 73 error/exception handling, 42–44 mapping between Java to JDBC SQL types, 39–42 OBJECT creating Oracle OBJECTs, 409–410 inserting OBJECT values into Oracle tables, 410–411 retrieving OBJECT values from Oracle tables, 411–413 utilities. See utilities versions, 78–79 Web site, 2 JDBC 1.0, 79 JDBC 2.0, 79 JDBC 3.0, 79 JDBC 4.0, 79 JDBC API, 26 JDBC Driver Certification Program, 82–83 JDBC driver database, 83 JDBC drivers, 13, 27–38 building URLs in JDBC driver format, 594, 597 connecting to existing databases, 17–18 core functionality, 79 MySQL, 79–81 Oracle, 81–82 criteria for selection, 37–38 driver names, 38
297
298
■INDEX
processing result sets, 23 retrieving table records, 22–23 JDBC resources, 74 JDBC Thin driver, 39 JDBC Utility Component (DbUtils), 598–604 core classes/interfaces, 598 design goals, 598 example usage, 598–604 package components, 598 Web sites, 598, 602–603 JDBC-ODBC bridge, 8–13 architecture, 8 debugging, 13 JDBC 2.0 API features supported, 13 packages, 8 using sun.jdbc.odbc, 10–13 using, 9–13 JFrames displaying BLOBs, 240–243 displaying CLOBs, 270–274 JNDI obtaining connections with JNDI, 166–167 without JNDI, 165–166 obtaining DataSource objects, 153 registering DataSource objects, 162–163 problems with file-based DataSource objects, 163–164 retrieving deployed/registered DataSource objects, 164–165 jxDBCon open-source JDBC driver framework, 82
■K-L
keys primary keys, generating random GUIDs, 608, 613 retrieving automatically generated keys using Statement MySQL, 388–391 Oracle, 392–394 KPRB driver (Oracle), 65 last( ) method, scrollable ResultSets, 215 leap years, 325–326 length( ) method Java.sql.Blob interface, 232 Java.sql.Clob interface, 258 limiting maximum connections, 145 MySQL, 146–147 Oracle, 145–146 Linux CLASSPATH environmental variable settings, 73 listing available parameters for creating connections, 103–106
loading drivers, 29–32, 91–92 creating an instance of a Driver class, 30 getting a list of loaded drivers, 95–97 using Class.forName( ), 29 a Driver class, 32 DriverManager.registerDriver( ), 29 System.properties( ) from command lines, 32 System.properties( ) inside programs, 30–32 utilities, 588 logging exceptions closing Connection, 570–574 closing PreparedStatement, 578–579 closing ResultSet, 576 closing Statement, 577 logons, SYS logon (Oracle), 123–124 long columns/fields, returning using utilities, 582–583 long data type, passing to PreparedStatement, 522–526 MySQL, 525–526 Oracle, 524–525 long text fields, storing using utilities, 583–584
■M
maintaining connections (autoReconnect parameter, Connection object), 130–131 Map object. See type mapping MapListHandler class, 602–603 mapping between Java to JDBC SQL types, 39–42 mapping trees into SQL using batch updates, 415–423, 427–430 deleting existing nodes, 420–423, 426–427 deleting existing subtrees, 427–430 inserting new nodes, 416–420 matching wildcards in SQL statements, 200 materializing data BLOBs, 235–236 CLOBs, 262–265 math, BigDecimal formatting, 593–594 passing to PreparedStatement, 502–505 maximum connections limits, 145 restricting max connections in MySQL databases, 146–147 restricting max connections in Oracle databases, 145–146 memory avoiding memory leaks, 61–63 MEMORY tables, 77
■INDEX
ResultSetMetaData, generic search patterns, 57 SQLException, 345 printSQLExceptions( ), 351–352 Statement object addBatch(String sql), 403, 408 execute( ), 387 executeBatch( ), 403, 414–415 user-defined types (UDTs), 134 utilities. See utilities Microsoft Access URL formats, 94 Microsoft Excel reading/extracting data from, 200–203 writing data to, 203–205 Microsoft ODBC Data Source Administrator, 11 Microsoft SQL Server making connections, 94–95 URL formats, 94 middle tier (three-tier model), 84 months. See Date MyISAM tabls, 77 MySQL BatchUpdateException, 368, 372 BLOBs, 233. See also BLOBs creating tables, 234 extracting, 240–241 inserting records, 236–237 serializing objects, 252–256 checking JDBC installations, 148–150 CLOBs, 259 creating tables, 260 defining, 260 deleting, using servlets, 303–305 extracting, 270–271 inserting, using servlets, 290–292 selecting/displaying CLOBs using servlets, 277–279, 284–285 updating, using servlets, 297–300 connecting to MySQL databases, 100–102 connection properties passing using database URLs, 122 passing using java.util.Properties, 121–122 passing using java.util.Properties and database URLs, 122–123 preventing timeouts using autoReconnect, 130–131 core functionality of JDBC drivers, 79–81 creating DataSource objects, 156–159 without JNDI, 165–166 DataTruncation, 377–378. See also DataTruncation Date, 310 Date/Time normalization, 336–339 DATETIME, 310
Find it faster at http://superindex.apress.com/
metadata, 45–46 DatabaseMetaData.getDriverVersion( ), 5 97 DatabaseMetaDataTool, 45 determining database support for scrollable ResultSets, 212 generic search patterns, 57 maximum connections limits, 145 MySQL, 146–147 Oracle, 145–146 minimizing methods usage, 52 methods BatchUpdateException, 365 Connection object commit, 48 controlling commit and rollback updates, 118–119 rollback, 48 database metadata generic search patterns, 57 minimizing usage, 52 Date, java.sql.Date.valueOf( ), 315 Driver interface, connect( ), 106–108 DriverManager class getConnection( ), 91, 124 getDrivers( ), 95–97 java.sql.Blob interface, 232 creating BLOBs, 234–235 materializing BLOB data, 235–236 java.sql.Clob interface creating CLOBs, 261–262 materializing CLOB data, 262–265 java.sql.DataTruncation, 375 java.sql.SQLWarning, 353 PreparedStatement, setXXX( ), 493–495. See also passing input parameters to PreparedStatement ResultSet absolute(int), 215 afterLast( ), 210, 215 beforeFirst( ), 215 cancelRowUpdates( ), 221–222 deleteRow( ), 226–229 first( ), 215 getConcurrency( ), 219–220 getType( ), 214 insertRow( ), 222–226 last( ), 215 next( ), 210, 215 previous( ), 210, 215 refreshRow( ), 229 relative(int), 215 retrieving Date, Time, and Timestamp, 309 scrolling methods, 214–216 setting ResultSet type, 176–178 updateRow( ), 220–221
299
300
■INDEX
determining whether databases accept transactions, 115 executing batch statements, using Statement, 407–408. See also batch statements JDBC drivers, 65–66 connecting with, 66–69 registering, 66 JDBC URL format, 594–597 key JDBC concepts for MySQL databases, 72–73 limiting maximum connections, 146–147 obtaining JDBC drivers, 34 passing input parameters to PreparedStatement BigDecimal objects, 504–505 BinaryStream objects, 511–512 BLOB objects, 517–518 boolean values, 521–522 byte arrays, 529–530 byte data type, 525–526 character streams, 533–534 CLOBs, 537–538 Date, 540–541 double data type, 544–545 float data type, 544–545 InputStream objects, 501–502 NULL, 548 Object, 550, 553 String objects, 560–561 Time objects, 564 Timestamp objects, 564 URL objects, 567 ResultSet. See ResultSets retrieving automatically generated keys using Statement, 388–391 serializing objects, 252–256 SQLException, 348–349 SQLState codes, 364 Statement, creating tables to store Java data types, 396–397 table types, 77 testing JDBC driver installation, 32–33 Time, 310 Timestamp, 310 transaction support, 76–77 URL formats, 94 viewing system variables, 146 MySQL Connector/J, 65–66 URL format, 66 MysqlConnectionFactory class (BCM), 109, 112–113
■N
nested sets, 415 next( ) method, scrollable ResultSets, 210, 215
nodes deleting, 420–423, 426–427 inserting, 416–420 normalization (Date and Time), 335–339 Northwind database, 10 NULL, passing to PreparedStatement, 545–548 MySQL, 548 Oracle, 546–547
■O
OBJECT (Oracle) creating, 408 using JDBC, 409–410 using SQL*Plus, 408–409 deleting from tables, 413–414 inserting values into tables using JDBC, 410–411 using SQL*Plus, 410 retrieving values from tables, 411–413 objects BLOBs. See BLOBs CallableStatement, batch updates, 60–61 CLOBs. See CLOBs closing, 23–25 Connection. See Connection object; connections DataSource. See DataSource object formatting Double, 594 Integer, 591 BigDecimal, 593–594 Map. See type mapping PreparedStatement batch updates, 59–61 caching, 61 passing to, 548–553 versus Statement, 58 Properties, passing to drivers (Oracle) using database URLs and Properties object, 123 ResultSet, 175–176, 183. See also ResultSets checking for SQL warnings, 120–121 choosing the right cursor, 58–59 serializing, 246 MySQL, 252–256 Oracle, 246–252 Statement. See also Statement batch updates, 59–61 checking for SQL warnings, 120 versus PreparedStatement, 58 unserializing, 246–248, 252 OCI driver (Oracle), 64 connecting with, 65 ODBC (Open Database Connectivity), 6–7 ODBC bridges, 2, 6 ODBC Data Source Administrator, 11
■INDEX
inserting, using SQL*Plus, 410 retrieving OBJECT values, 411–413 passing input parameters to PreparedStatement Array objects, 496–498 BigDecimal objects, 502–504 BinaryStream objects, 506–511 BLOB objects, 514–517 boolean values, 520–521 byte arrays, 528–529 byte data type, 524–525 character streams, 532–533 CLOBs, 535–537 Date, 539–540 double data type, 543–544 float data type, 543–544 InputStream objects, 500–501 NULL, 546–547 Object, 550, 553 REF CURSOR objects, 556–557 String objects, 559–560 Time objects, 563 Timestamp objects, 563 URL objects, 566 ResultSets. See ResultSets retrieving automatically generated keys using Statement, 392–394 SEQUENCE object, 392 serializing objects, 246–252 SQLException, 349 SQLState codes, 359–363 Statement, creating tables to store Java data types, 397–398 SYSDBA and SYSOPER connections, 147 testing JDBC driver installation, 33–34 Time, 310–314 Timestamp, 310–314 transaction support, 77–78 URLs formats, 94, 594–597 syntax for connecting to databases, 39 Oracle Application Server, 84 OracleConnectionFactory class (BCM), 109, 112–113 ordinal position, PreparedStatement parameters, 493 output printSQLExceptions( ), 351–352 printSQLWarnings( ), 353
■P
packages, utilities. See utilities ParameterMetaData, 45 passing additional properties to JDBC drivers MySQL connection properties database URLs, 122 java.util.Properties, 121–122
Find it faster at http://superindex.apress.com/
ODBC Programmer’s Reference Web site, 6 OdbcConnectionFactory class (BCM), 109, 112 Open Database Connectivity (ODBC), 6–7 Oracle BatchUpdateException, 369, 373 BLOBs, 233. See also BLOBs creating tables, 233 extracting, 241 inserting records, 237–240 serializing objects, 246–252 checking JDBC installations, 148–151 CLOBs, 259. See also CLOBs creating tables, 260 defining, 259–260 deleting, using servlets, 300–303 empty_clob( ) function, 270 extracting, 271 inserting, using servlets, 285–290 selecting/displaying CLOBs using servlets, 274–277, 280–283 updating, using servlets, 293–297 connecting to Oracle databases, 97–100 connection pool project, 55 connection properties connecting as SYSDBA and SYSOPER, 147 passing properties using DriverManager getConnection( ) method, 123–124 using roles for SYS logon, 123–124 core functionality of JDBC drivers, 81–82 creating DataSource objects, 156–159 without JNDI, 165 database preparation for custom type mapping, 136–137 DataTruncation, 378–379 Date, 310–314 Date/Time normalization, 336–339 determining whether databases accept transactions, 115 executing batch statements, using Statement, 405–407 JDBC drivers, 63–65 driver names, 38 KPRB driver, connecting with, 65 obtaining, 34 OCI driver, 64–65 Thin driver, 39, 63–65 JDBC URL format, 594–597 key JDBC concepts for Oracle databases, 69–71 limiting maximum connections, 145–146 OBJECT creating, using JDBC, 409–410 creating, using SQL*Plus, 408–409 deleting OBJECTs from Oracle tables, 413–414 inserting, using JDBC, 410–411
301
302
■INDEX
java.util.Properties and database URLs, 122–123 preventing timeouts using autoReconnect, 130–131 Oracle connection properties database URLs and Properties object, 123 using roles for SYS logon, 123–124 passing input parameters to PreparedStatement, 493–495 Array objects, 495–498 Oracle, 496–498 BigDecimal objects, 502–505 MySQL, 504–505 Oracle, 502–504 BinaryStream objects, 506–512 MySQL, 511–512 Oracle, 506–511 BLOB objects, 513–518 MySQL, 517–518 Oracle, 514–517 boolean values, 519–522 MySQL, 521–522 Oracle, 520–521 byte arrays, 526–530 MySQL, 529–530 Oracle, 528–529 byte data type, 522–526 MySQL, 525–526 Oracle, 524–525 character streams, 530–534 MySQL, 533–534 Oracle, 532–533 CLOBs, 534–538 MySQL, 537–538 Oracle, 535–537 Date, 538–541 MySQL, 540–541 Oracle, 539–540 double data type, 542–545 MySQL, 544–545 Oracle, 543–544 float data type, 542–545 MySQL, 544–545 Oracle, 543–544 InputStream objects, 498–502 MySQL, 501–502 Oracle, 500–501 int data type, 522–526 MySQL, 525–526 Oracle, 524–525 long data type, 522–526 MySQL, 525–526 Oracle, 524–525 NULL, 545–548 MySQL, 548 Oracle, 546–547
Object, 548–553 MySQL, 550, 553 Oracle, 550, 553 ordinal position values, 493 REF CURSOR objects, 553–557 Oracle, 556–557 setXXX( ) method summary, 493–495 short data type, 522–526 MySQL, 525–526 Oracle, 524–525 String objects, 558–561 MySQL, 560–561 Oracle, 559–560 Time objects, 561–564 MySQL, 564 Oracle, 563 Timestamp objects, 561–564 MySQL, 564 Oracle, 563 URL objects, 565–567 MySQL, 567 Oracle, 566 passwords connecting with MySQL JDBC drivers, 67–69 invoking getDataSource( ) with specific users/passwords, 157–158 invoking getDataSource( ) without specific users/passwords, 158–159 Oracle connection properties, 123 performance improvement, 51–63 avoiding generic search patterns, 57 avoiding memory leaks, 61–63 caching PreparedStatement objects, 61 driver selection, 52 managing database Connection objects, 53–57 choosing optimal transaction isolation levels, 56–57 controlling transactions, 55–56 setting optimal Connection properties, 53 using connection pools, 54–55 minimizing database metadata methods usage, 52 retrieving only required data, 58 batching multiple update statements, 59–61 choosing appropriate cursors, 58–59 using Statement object vs. PreparedStatement object, 58 simplifying SQL queries, 52 Performance Management Guide, 51 pools of connections, 1, 49–51 application optimization, 54–55 pool managers, 49 borrowing Connection objects from, 131–132 soft closing Connection, 570
■INDEX
passing using java.util.Properties, 121–122 passing using java.util.Properties and database URLs, 122–123 preventing timeouts using autoReconnect, 130–131 Oracle connection properties connecting as SYSDBA and SYSOPER, 147 passing using DriverManager getConnection( ) method, 123–124 passing to drivers (Oracle) using database URLs and Properties object, 123
■Q
queries. See also retrieving counting rows using Statement, 401 getting rows using Statement, 398–399 limiting rows returned from SQL servers, 115–117 simplifying SQL queries, 52 Statement. See Statement query plans, 382 question mark (?), specifying connection properties, 121
■R
random GUID generation, 608, 613 RDBMS (relational database management system), 6 reconnecting (autoReconnect parameter, Connection object), 130–131 records, 6 inserting, 22 retrieving, 22–23 writing, using custom mapping, 138–142, 144 REF CURSOR objects, passing to PreparedStatement, 553–557 registering databases, 11 DataSource objects, 161 problems with file-based DataSource objects, 163–164 retrieving deployed/registered DataSource objects, 164–165 using file-based systems, 162–163 JDBC drivers MySQL, 66 Oracle JDBC Thin driver, 63 relational database management system (RDBMS), 6 relational databases, 6 columns, 6 creating DataSource objects, 157–160 rows/records, 6 tables/views, 6
Find it faster at http://superindex.apress.com/
populating databases, 22 position(Blob pattern, long stat) method (Java.sql.Blob interface), 232 position(byte[ ] pattern, long start) method (Java.sql.Blob interface), 232 position(Clob searchstr, long start) method (Java.sql.Clob interface), 258 position(String searchstr, long start) method (Java.sql.Clob interface), 258 precompiling statements, 382 preorder tree traversal algorithm, 415 PreparedStatement, 27, 45, 382 batch updates, 59–61 caching, 61 closing, 24, 578–579 methods, 493 setXXX( ), 494–495 passing input parameters. See passing input parameters to PreparedStatement versus Statement, 58 previous( ) method, scrollable ResultSets, 210, 215 primary keys, generating random GUIDs, 608, 613 printSQLExceptions( ), 351–352 printSQLWarnings( ), 353 processing result sets, 23 programming (JDBC), 15–25 closing objects, 23 Connection, 25 PreparedStatement, 24 result sets, 23 Statement, 24 connecting to existing databases, 17–18 creating databases, 15–16 tables, 19–21 dropping databases, 16 populating databases, 22 processing result sets, 23 retrieving table records, 22–23 programs core JDBC functionality test program MySQL, 79–81 Oracle, 81–82 debugging, 75–76 running MySQL databases, 72–73 Oracle databases, 70–71 properties DataSource objects, 154, 160–161 MySQL connection properties passing using database URLs, 122
303
304
■INDEX
relative(int) method, scrollable ResultSets, 215 releasing resources, 23–25 reporting exceptions closing Connection, 571–575 closing PreparedStatement, 576, 579 closing ResultSet, 576 closing ResultSet, Statement, and Connection together, 581 closing Statement, 578 closing Statement and Connection together, 580 resources, releasing, 23–25 ResultSets, 27, 169–173 adding objects to lists end of lists, 206 head of lists, 207 checking for SQL warnings, 120–121, 355 choosing cursors, 58–59 closing, 23, 575–576 closing ResultSet, Statement, and Connection together, 580–581 concurrency, 180 getting/setting, 181 creating scrollable ResultSets, 383–385 insensitive, 383–384 sensitive, 384 creating updatable ResultSets, 385–386 DataTruncation, 379 debugging/displaying ResultSet objects, 606–608 determining whether fetched values are NULL, 191 Excel files reading/extracting data from, 200–203 writing data to, 203–205 getting BLOB data from tables, 194–198 getting CLOB data from tables, 199 getting column names, 191–193 getting data from ResultSets, 185–191 knowing column names, position, and type, 187–190 not knowing column names, position, and type, 190–191 getting number of table rows, 193–194 getting rows, 183–187 using Statement, 398 holdability, 181 checking, 182–183 getting, 183 setting, 182 JDK definition, 175 matching wildcards in SQL statements, 200 methods absolute(int), 215 afterLast( ), 210, 215 beforeFirst( ), 215
cancelRowUpdates( ), 221–222 deleteRow( ), 226–229 first( ), 215 getConcurrency( ), 219–220 getType( ), 214 insertRow( ), 222–226 last( ), 215 next( ), 210, 215 previous( ), 210, 215 refreshRow( ), 229 relative(int), 215 retrieving Date, Time, and Timestamp, 309 scrolling methods, 214–216 updateRow( ), 220–221 objects, 175–176, 183 preferred collection class for storing, 205–207 processing, 23 REF CURSOR objects, passing to PreparedStatement, 553–557 relationship to other classes/interfaces, 174 retrieving whole rows/records at once, 208 row prefetch, setting using Statement, 395–396 scrollable ResultSet objects, 209–218 creating, 212–214 determining database support for, 212 determining whether ResultSets are scrollable, 214 getting cursor position, 216–217 getting number of rows, 217–218 scrolling methods, 214–216 types, 175–176 database support, 178–180 getting types, 178 setting type, 176–178 updatable ResultSet objects canceling updates, 221–222 creating, 219 deleting rows, 226–229 determining database support for, 218–219 determining whether ResultSets are updatable, 219–220 inserting rows, 222–226 refreshing rows, 229 updating rows, 220–221 ResultSetMetaData, generic search patterns, 57 retrieving automatically generated keys using Statement MySQL, 388–391 Oracle, 392–394 binary data, using Statement, 402
■INDEX
refreshing, 229 updating, 220–221 retrieving only required data, 58 RowSetMetaData, 45 rules, autocommit mode, 134 running programs, 12 MySQL databases, 72–73 Oracle databases, 70–71
■S
scroll-insensitive result sets, 176 scroll-sensitive result sets, 176 scrollable ResultSets, 209–218 choosing appropriate cursor, 58–59 creating, 212–214, 383–385 insensitive, 383–384 sensitive, 384 determining database support for, 212, 384–385 determining whether ResultSets are scrollable, 214 getting cursor position, 216–217 getting number of rows, 217–218 scrolling methods, 214–216 searches, generic search patterns, 57 SELECT queries counting rows using Statement, 401 getting rows using Statement, 398–399 selecting BLOB data types, 240–243 CLOBs JFrames, 270–274 servlets, MySQL, 277–279, 284–285 servlets, Oracle, 274–277, 280–283 SELECT command, 14 sensitive scrollable ResultSets, 213 creating, 384 SEQUENCE object (Oracle), 392 serializing objects, 246 MySQL databases, 254–256 Oracle databases, 246–252 unserializing objects, 246–252 servlets deleting CLOBs MySQL, 303–305 Oracle, 300–303 inserting CLOBs MySQL, 290–292 Oracle, 285–290 selecting/displaying CLOBs MySQL, 277–279, 284–285 Oracle, 274–277, 280–283 updating CLOBs MySQL, 297–300 Oracle, 293–297 setAsciiStream(long pos) method (Java.sql.Clob interface), 258
Find it faster at http://superindex.apress.com/
BLOBs, 240–243 CLOBs JFrames, 270–274 servlets, MySQL, 277–279, 284–285 servlets, Oracle, 274–277, 280–283 Date, 309–310 current date, using utilities, 587 Oracle Date/Time types, 313–314 deployed/registered DataSource objects, 164–165 JDBC driver version number, 597 long columns/fields, using utilities, 582–583 OBJECT values from Oracle tables, 411–413 required data only, 58 batching multiple update statements, 59–61 choosing appropriate cursors, 58–59 using Statement object vs. PreparedStatement object, 58 rows, using Statement, 398–399 SQL table data, 15 SQLException details, 349–350 getting all instances using printSQLExceptions( ), 351–352 SQLWarning instances, 353 table names, 584–585 table and view names together, 585–586 Time, 309–310 Timestamp, 309–310, 320 trimmed strings, using utilities, 587–588 view names, 585 table and view names together, 585–586 REVOKE command, 14 RmiJdbc, 82 roles (Oracle) using for SYS logon, 123–124 roll back controlling updates, 118–119 handling rollback( ) when exceptions occur, 44 ROLLBACK command, 14 rolling back and closing Connection objects, 573–575 rolling back transactions, 48 rows, 6 getting from tables (result sets), 183–187. See also ResultSets getting number of table rows, 193–194 optimal row prefetch values, 53 prefetch, setting using Statement, 395–396 ResultSets canceling updates, 221–222 deleting, 226–229 getting number of rows in scrollable ResultSets, 217–218 inserting rows, 222–226
305
306
■INDEX
setBinaryStream(long pos) method (Java.sql.Blob interface), 232 setBytes(long pos, byte[ ] bytes) method (Java.sql.Blob interface), 232 setBytes(long pos, byte[ ] bytes, int offset, int len) method (Java.sql.Blob interface), 232 setCharacterStream(long pos) method (Java.sql.Clob interface), 258 setNextWarning( ), 353 setString(long pos, String str) method (Java.sql.Clob interface), 258 setString(long pos, String str, int offset, int len) method (Java.sql.Clob interface), 259 setXXX( ) method (PreparedStatement), 494–495. See also passing input parameters to PreparedStatement sharing Connection objects, 50 short data type, passing to PreparedStatement, 522–526 MySQL, 525–526 Oracle, 524–525 SHOW VARIABLES statement (MySQL), 146 SimpleDateFormat, 323 SimpleText database, 82 simplifying SQL queries, 52 singleton classes, 109 soft closes, 25, 49 Connection, using pool managers, 570 sorting restrictions BLOBs, 234 CLOBs, 261 SQL, 13–15 commands, 13–14 data types. See data types executing SQL statements, 386–387 mapping between Java to JDBC SQL types, 39–42 queries, simplifying, 52 statements, 14 wildcards, 200 tables, 14–15 SQL Server limiting rows returned from, 115–117 making connections, 94–95 URL formats, 94 SQL*Plus creating OBJECTs, 408–409 inserting OBJECT values into Oracle tables, 410 retrieving OBJECT values from Oracle tables, 411–413 SQLData interface, 136 implementing, 137–138 SQLException, 42–43, 341, 344–345 chaining, 350–351
checked vs. unchecked exceptions, 346 constructors, 345 debugging/displaying SQLException objects, 605 example usage, 348–349 getting all instances using printSQLExceptions( ), 351–352 getting details, 349–350 methods, 345 relationship to other classes, 346–347 SQLState codes returned by SQLException.getSQLState( ), 358–364 code formats, 358 MySQL codes, 364 Oracle codes, 359–363 SQLWarning, 42, 341, 352–353 checking for, 119–121 using Connection object, 119–120 using ResultSet object, 120–121 using Statement object, 120 constructors, 352 creating/traversing warnings, 355–356 debugging/displaying SQLWarning objects, 605–606 determining whether warnings have occurred, 353–355 getting all instances, 353 methods, 353 stack traces, 344 starting transactions, 48–49 Statement, 14, 27, 381–382 batch updates, 59–61 checking for SQL warnings, 120, 354 closing, 24, 576–578 closing ResultSet, Statement, and Connection together, 580–581 closing Statement and Connection objects together, 579–580 creating ResultSet objects, 209–211 scrollable ResultSets, 383–385 updatable ResultSets, 385–386 creating Statement objects, 382–383 determining whether a SQL warning occurred, 395 executing batch statements, 403–405 MySQL, 407–408 Oracle, 405–407 executing SQL statements, 386–387 PreparedStatement, 382. See also PreparedStatement preparing/precompiling statements, 382 query plans, 382 retrieving automatically generated keys MySQL, 388–391 Oracle, 392–394 setting fetch size, 395–396 tables
■INDEX
subnames (JDBC URLs), 38 subprotocols (JDBC URLs), 38 subtrees, deleting, 427–430 sun.jdbc.odbc, 8 using, 10–13 sun.jdbc.odbc.ee, 8 Sybase URL formats, 94 SYS logon (Oracle), using roles for, 123–124 SYSDBA and SYSOPER connections (Oracle), 147 system variables, viewing (MySQL), 146 System.currentTimeMillis( ), 316 System.properties( ), loading JDBC drivers from command lines, 32 inside programs, 30–32
■T
tables, 6 binary data getting, using Statement, 402 inserting, using Statement, 401–402 BLOBs column restrictions, 234 creating, 233–234 defining BLOB data types, 233 deleting, 243–246 getting BLOB data, 194–198 inserting records, 236–237, 240 restrictions, 234 retrieving BLOB data types, 240–243 CLOBs creating, 260 defining BLOB data types, 259–260 deleting MySQL CLOBs using servlets, 303–305 deleting Oracle CLOBs using servlets, 300–303 getting CLOB data, 199 inserting into MySQL databases using servlets, 290–292 inserting into Oracle databases using servlets, 285–290 inserting records, 265–270 restrictions for using, 261 selecting/displaying CLOBs in JFrames, 270–274 selecting/displaying CLOBs using servlets, MySQL, 277–279, 284–285 selecting/displaying CLOBs using servlets, Oracle, 274–277, 280–283 updating MySQL CLOBs using servlets, 297–300 updating Oracle CLOBs using servlets, 293–297 counting rows, using Statement, 401 creating, 19–21
Find it faster at http://superindex.apress.com/
counting rows, 401 creating, 387–388 deleting rows, 400 dropping, 388 getting binary data, 402 getting rows, 398–399 inserting binary data, 401–402 inserting rows, 399 storing Java types, MySQL, 396–397 storing Java types, Oracle, 397–398 updating rows, 399–400 versus PreparedStatement, 58 statements, 14 storing long text fields using utilities, 583–584 streams BinaryStream, passing to PreparedStatement, 506–512 character streams, passing to PreparedStatement, 530–534 InputStream. See InputStream string concatenation formatting integers, 590 formatting strings, 588 StringBuffer append( ) formatting integers, 590–591 formatting strings, 589 formatting BigDecimal objects, 593–594 Double data types, 592–594 Strings CLOBs. See CLOBs converting Timestamp to Month-Day-Year strings, 331–332 Dates converting String dates to java.sql.Date objects, 319–320 converting String dates to java.util.Date objects, 317 creating yesterday’s date from String dates, 317–318 formatting BigDecimal objects, 593 Double data types, 591 strings, 588–590 getting trimmed strings, using utilities, 587–588 materializing CLOB data, 264–265 passing to PreparedStatement, 558–561 MySQL, 560–561 Oracle, 559–560 SimpleDateFormat, 323 Time HH:MM:SS format, 324–325 MM/DD/YYYY format, 323–324
307
308
■INDEX
Date/Time/Timestamp (Oracle), 311 storing Java types using Statement, MySQL, 396–397 storing Java types using Statement, Oracle, 397–398 using Statement, 387–388, 396–398 defining, Date/Time/Timestamp (Oracle), 311 deleting rows, using Statement, 400 dropping, using Statement, 388 getting BLOB data, 194–198 CLOB data, 199 rows, using Statement, 398–399 table names, using utilities, 584–586 inserting records, 22 Oracle Date/Time types, 312 inserting rows, using Statement, 399 MySQL table types, 77 ResultSet objects, 607–608. See also ResultSets retrieving records, 22–23. See also ResultSets only required data, 58 Oracle Date/Time types, 313–314 rows getting (result sets), 183–187. See also ResultSets getting number of table rows, 193–194 SQL, 14–15 updating rows, using Statement, 399–400 testing connection validity, 125–126, 129 JDBC driver installation MySQL, 32–33 Oracle, 33–34 Thin driver (Oracle), 63 connecting with, 64–65 registering, 63 Thread, loading JDBC drivers, 32 three-tier model for JDBC, 84–86 Throwable class, 347 throwing exceptions, 44 Time, 307–308. See also Date converting current time to java.sql.Date objects, 322 java.util.Date to HH:MM:SS strings, 325 java.util.Date to MM/DD/YYYY strings, 323–324 creating HH:MM:SS format strings, 324 java.sql.Time objects, 320–321 determining day of week, 322 getting current timestamp as a java.util.Time object, 316 mapping, 308 MySQL, 310
normalization, 335–339 Oracle, 310–314 passing to PreparedStatement, 561–564 MySQL, 564 Oracle, 563 retrieving, 309–310 time zones, getting dates for specific zones, 340 timeouts, preventing (autoReconnect parameter), 130–131 Timestamp, 307–308 converting java.sql.Timestamp objects to java.util.Date objects, 335 Timestamp to Month-Day-Year strings, 331–332 getting current timestamp as a java.util.Time object, 316 as a java.util.Timestamp object, 316 getting date labels from java.sql.Timestamp objects, 333–335 getting Timestamp objects, 320 making java.sql.Timestamp objects for given Year, Month, Day, Hour, 339–340 mapping, 308 MySQL, 310 Oracle, 310–314 passing to PreparedStatement, 561–564 MySQL, 564 Oracle, 563 retrieving, 309–310 TNSNAMES string, 39 tracing JDBC operations, 75–76 transaction branches, 78 transactions, 46–47 ACID (atomicity, consistency, isolation, and durability), 47 application optimization, optimal transaction isolation levels, 56–57 autocommit mode, 48 controlling transactions for application optimization, 55–56 determining whether databases accept transactions, 113–115 distributed MySQL support, 77 Oracle support, 78 explicit model 1, 49 model 2, 49 isolation levels, 56–57 MySQL transaction support, 76–77 Oracle transaction support, 77–78 rolling back transactions, 48 starting/ending, 48–49 traversing warnings, 355–356
■INDEX
■U
UDTs (user-defined types), 134. See also type mapping inserting UDTs into databases, 138–142, 144 SQLData interface, 136–138 unchecked vs. checked exceptions, 346 Unicode, materializing CLOB data, 263–264 Unix CLASSPATH environmental variable settings, 73 unserializing objects, 246–248, 252 updatable ResultSet objects canceling updates, 221–222 creating, 219, 385–386 deleting rows, 226–229 determining database support for, 218–219, 386 determining whether ResultSets are updatable, 219–220 inserting rows, 222–226 refreshing rows, 229 updating rows, 220–221 updateRow( ) method, updatable ResultSets, 220–221
updating, 14 batch updates, 59–61 BatchUpdateException, 43, 341, 364–373 JDBC batch update support, 414–415 mapping trees into SQL. See mapping trees into SQL using batch updates CLOBs, using servlets MySQL, 297–300 Oracle, 293–297 controlling commit and rollback updates, 118–119 ResultSets. See updatable ResultSet objects rows, using Statement, 399–400 SQL tables, 15 URLs MySQL drivers building URLs in JDBC driver format, 594, 597 connecting to driver manager using database URL, 66–68 Connector/J URL format, 66 passing additional properties to drivers using database URLs MySQL, 122–123 Oracle, 123 passing to PreparedStatement, 565–567 MySQL, 567 Oracle, 566 selecting/displaying CLOBs using servlets MySQL, 284–285 Oracle CLOBs, 280–283 syntax for connecting to databases, 38–39 vendor URL formats, 94 user parameter, connecting with MySQL JDBC drivers, 67–69 user-defined types (UDTs), 134. See also type mapping inserting UDTs into databases, 138–142, 144 SQLData interface, 136 implementing, 137–138 usernames, Oracle connection properties, 123 users invoking getDataSource( ) with specific users/passwords, 157–158 invoking getDataSource( ) without specific users/passwords, 158–159 utilities, 569 building URLs in JDBC driver format, 594, 597 closing Connection objects, 569–575 closing ResultSet, Statement, and Connection together, 580–581 closing Statement and Connection together, 579–580 committing and closing, 571–573 logging exceptions, 570–574
Find it faster at http://superindex.apress.com/
trees, mapping into SQL using batch updates, 415–423, 427–430 deleting existing nodes, 420–423, 426–427 deleting existing subtrees, 427–430 inserting new nodes, 416–420 trimmed strings, getting using utilities, 587–588 trimParameter( ), CLOBs, 290, 297 troubleshooting. See also debugging handling errors/exceptions, 42–44 performance improvement. See performance improvement running out of cursors, 61–63 truncate(long len) method Java.sql.Blob interface, 232 Java.sql.Clob interface, 259 try blocks, 343–344 two-tier model for JDBC, 83–84 type 1 drivers, 35 type 2 drivers, 35–36 type 3 drivers, 36 type 4 drivers, 36–37 type mapping, 134–135 creating SQL to Java type map entries, 135–144 implementing SQLData, 137–138 Java type map support, 135 Oracle database preparation, 136–137 using a Connection type map, 138 writing new records, 138–144 SQLData interface, 136 implementing, 137–138 user-defined types (UDTs), 134
309
310
■INDEX
not reporting exceptions, 570–574 reporting exceptions, 571–575 rolling back and closing, 573–575 soft closing using pool managers, 570 closing PreparedStatement objects, 578–579 closing ResultSet objects, 575–576 closing ResultSet, Statement, and Connection together, 580–581 closing Statement objects, 576–578 closing ResultSet, Statement, and Connection together, 580–581 closing Statement and Connection together, 579–580 converting input streams to byte arrays, 586–587 DbUtils package, 598–604 core classes/interfaces, 598 design goals, 598 example usage, 598–604 package components, 598 Web sites, 598, 602–603 debugging/displaying ResultSet objects, 606–608 debugging/displaying SQLException objects, 605 debugging/displaying SQLWarning objects, 605–606 formatting Double data types, 591–593 formatting Double objects, 594 formatting integers int data type, 590–591 Integer objects, 591 formatting java.math.BigDecimal objects, 593–594 formatting strings, 588–590 generating random GUIDs, 608, 613 getting current date, 587 getting table names, 584–585 getting table and view names together, 585–586 getting trimmed strings, 587–588 getting view names, 585 JDBC drivers getting driver version, 597 loading JDBC drivers, 588 returning long columns/fields, 582–583 storing long text fields, 583–584
■V
variables CLASSPATH environmental variable, 73 viewing MySQL system variables, 146 versions of JDBC, 78–79 viewing. See displaying; retrieving views, 6 getting view names using utilities, 585–586
■W
warnings debugging/displaying SQLWarning objects, 605–606 checking for, 119–121 using Connection object, 119–120 using ResultSet object, 120–121 using Statement object, 120, 395 java.sql.DataTruncation, 341, 373–375 constructor, 375 creating/using DataTruncation objects, 375–379 determining whether DataTruncation has occurred, 374 methods, 375 using with ResultSet, 379 java.sql.SQLWarning, 42, 341, 352–353 constructors, 352 creating/traversing warnings, 355–356 determining whether warnings have occurred, 353–355 getting all instances, 353 methods, 353 Web sites BEA WebLogic Server, 84 FreeTDS, 82 IBM WebSphere, 84 JDBC, 2 drivers, 32 resources, 74 jxDBCon open-source JDBC driver framework, 82 Microsoft ODBC Programmer’s Reference, 6 MySQL, 34 Oracle, 34 RmiJdbc, 82 SimpleText database, 82 Sun, JDBC-ODBC bridge, 13 WebLogic connection pool, 51, 55 WebLogic Server, 84 WebSphere, 84 wildcards matching in SQL statements, 200 specifying connection properties, 121 Windows CLASSPATH environmental variable settings, 73 wrapping exceptions, 357–358 writing JDBC drivers, 82 to Microsoft Excel, 203–205 records, using custom mapping, 138–144
■X-Y-Z
XML, listing available parameters for creating connections, 103–106 years. See Date
FIND IT FAST with the Apress SuperIndex
™
Quickly Find Out What the Experts Know
L
eading by innovation, Apress now offers you its SuperIndex™, a turbocharged companion to the fine index in this book. The Apress SuperIndex™ is a keyword
and phrase-enabled search tool that lets you search through the entire Apress library. Powered by dtSearch™, it delivers results instantly. Instead of paging through a book or a PDF, you can electronically access the topic of your choice from a vast array of Apress titles. The Apress SuperIndex™ is the perfect tool to find critical snippets of code or an obscure reference. The Apress SuperIndex™ enables all users to harness essential information and data from the best minds in technology. No registration is required, and the Apress SuperIndex™ is free to use. 1
Thorough and comprehensive searches of over 300 titles
2
No registration required
3
Instantaneous results
4
A single destination to find what you need
5
Engineered for speed and accuracy
6
Will spare your time, application, and anxiety level
Search now: http://superindex.apress.com