Excel® 2007 VBA Macro Programming About the Author Richard Shepherdhas worked for many years for major banks and corporations in the United Kingdom writing spreadsheet macros to solve specific problems. He has worked for National Grid plc (electricity distribution), Hertz Car Leasing, NatWest plc (retail banking), Schroders plc (fund management), BNP Paribas (investment banking), Lloyds TSB plc (corporate banking), The Royal Bank of Scotland plc (investment banking), and the British National Health Service. He has developed advanced spreadsheets and macros for budgeting, business planning, and profit and loss reporting. He originally started working with Lotus 123 spreadsheets but moved some years ago to Microsoft Excel and Microsoft Access. He qualified as an accountant with the Chartered Association of Certified Accountants in 1976 and has been a Fellow since 1981. He can be contacted at [email protected], but he cannot promise to answer every e-mail! About the Technical Editor Todd Meisterhas been developing using Microsoft technologies for over ten years. He’s been a technical editor on over 50 titles ranging from SQL Server to the .NET Framework. Besides technically editing titles, he is an assistant director for Computing Services at Ball State University in Muncie, Indiana. He lives in central Indiana with his wife, Kimberly, and their four remarkable children. Excel® 2007 VBA Macro Programming Richard Shepherd New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto Copyright © 2010 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. ISBN: 978-0-07-162701-6 MHID: 0-07-162701-4 The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-162700-9, MHID: 0-07-162700-6. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training pro- grams. To contact a representative please e-mail us at [email protected]. Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information. ATTICA This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, dis- seminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own non- commercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to com- ply with these terms. THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MER- CHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. To my wife, Elaine, and my son, Alexander. This page intentionally left blank Contents at a Glance Part I Programming in Excel VBA Chapter 1 The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2 Variables, Arrays, Constants, and Data Types . . . . . . . . . . . . . . . 13 Chapter 3 Modules, Functions, and Subroutines . . . . . . . . . . . . . . . . . . . . . 27 Chapter 4 Programming Basics: Decisions and Looping . . . . . . . . . . . . . . . . . 35 Chapter 5 Strings and Functions and Message Boxes . . . . . . . . . . . . . . . . . 45 Chapter 6 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Chapter 7 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Chapter 8 Errors and the Error Function . . . . . . . . . . . . . . . . . . . . . . . . . 91 Chapter 9 Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Chapter 10 Common Dialog Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Chapter 11 Command Bars and Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Part II Object Models Chapter 12 The Excel Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Chapter 13 The Excel Object Model—Main Objects . . . . . . . . . . . . . . . . . . . 151 Chapter 14 Using Excel to Interact with Other Office Programs . . . . . . . . . . . . 171 vii viii Excel 2007 VBA Macro Programming Part III Advanced Techniques in Excel VBA Chapter 15 Charts and Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Chapter 16 Working with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Chapter 17 API Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Chapter 18 Class Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Chapter 19 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Chapter 20 Working with XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Chapter 21 The Ribbon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Chapter 22 Pivot Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 PartIV VBA in Action Chapter 23 Converting Labels to Numbers and Numbers to Labels . . . . . . . . . . . 261 Chapter 24 Transposing a Range of Cells . . . . . . . . . . . . . . . . . . . . . . . . . 265 Chapter 25 Adding Formula Details into Comments . . . . . . . . . . . . . . . . . . . 269 Chapter 26 Calculating a Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 Chapter 27 Reversing a Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Chapter 28 Who Created the Workbook? . . . . . . . . . . . . . . . . . . . . . . . . . 277 Chapter 29 Evaluating a Cell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Chapter 30 Sorting Worksheets into Alphabetical Order . . . . . . . . . . . . . . . . 285 Chapter 31 Replacing Characters in a String . . . . . . . . . . . . . . . . . . . . . . . 287 Chapter 32 Timed Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Chapter 33 Auto-Totaling a Matrix of Numbers . . . . . . . . . . . . . . . . . . . . . 293 Chapter34 Absolute and Relative Formulas . . . . . . . . . . . . . . . . . . . . . . . 297 Chapter 35 Coloring Alternate Rows and Columns of the Spreadsheet . . . . . . . . 303 Chapter 36 Coloring Cells Containing Formulas . . . . . . . . . . . . . . . . . . . . . . 309 Contents at a Glance ix Chapter 37 Summing Cells by Reference to a Master Cell . . . . . . . . . . . . . . . . 313 Chapter 38 Globally Changing a Range of Values . . . . . . . . . . . . . . . . . . . . 317 Chapter 39 Displaying Hidden Sheets Without a Password . . . . . . . . . . . . . . . 321 Chapter 40 Searching Multiple Sheets and Workbooks . . . . . . . . . . . . . . . . . 327 Chapter 41 Brighten Up Your Comments . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Chapter 42 Importing a CSV File Containing Leading Zeros . . . . . . . . . . . . . . . 347 Chapter 43 Working with Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 Chapter44 Turning Your VBA Code into an Add-In . . . . . . . . . . . . . . . . . . . . 357 Appendix ASCII Character Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373