PDFgozar.com Excel 2010 Microsoft ® ® Power Programming with VBA John Walkenbach BONUS CD-ROM! Includes valuable examples, a searchable PDF of the book, and more PDFgozar.com PDFgozar.com Excel® 2010 Power Programming with VBA by John Walkenbach PDFgozar.com Excel® 2010 Power Programming with VBA Published by Wiley Publishing, Inc 111 River Street Hoboken, NJ 07030-5774 www.wiley.com Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600 Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 7486011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions Trademarks: Wiley and the Wiley Publishing logo are trademarks or registered trademarks of John Wiley & Sons, Inc and/or its affiliates in the United States and other countries, and may not be used without written permission Excel is a registered trademark of Microsoft Corporation in the United States and/or other countries All other trademarks are the property of their respective owners Wiley Publishing, Inc., is not associated with any prod uct or vendor mentioned in this book LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ FULFILLMENT OF EACH COUPON OFFER IS THE SOLE RESPONSIBILITY OF THE OFFEROR For general information on our other products and services, please contact our Customer Care Department within the U.S at 877-762-2974, outside the U.S at 317-572-3993, or fax 317-572-4002 For technical support, please visit www.wiley.com/techsupport Wiley also publishes its books in a variety of electronic formats Some content that appears in print may not be available in electronic books Library of Congress Control Number: 2010923549 ISBN: 978-0-470-47535-5 Manufactured in the United States of America 10 PDFgozar.com About the Author John Walkenbach is author of more than 50 spreadsheet books and lives in southern Arizona Visit his Web site: http://spreadsheetpage.com PDFgozar.com Publisher’s Acknowledgments We’re proud of this book; please send us your comments at http://dummies.custhelp.com For other comments, please contact our Customer Care Department within the U.S at 877-762-2974, outside the U.S at 317-572-3993, or fax 317-572-4002 Some of the people who helped bring this book to market include the following: Acquisitions, Editorial, and Media Development Project Editor: Kelly Ewing Acquisitions Editor: Katie Mohr Technical Editor: Todd Meister Editorial Manager: Jodi Jensen Media Development Project Manager: Laura Moss-Hollister Composition Services Project Coordinator: Katherine Crocker Layout and Graphics: Carrie A Cesavice, Joyce Haughey, Jennifer Mayberry Proofreaders: Laura L Bowman, John Greenough Indexer: Broccoli Information Management Media Development Assistant Project Manager: Jenny Swisher Media Development Associate Producer: Douglas Kuhn Media Development Quality Assurance: Marilyn Hummel Editorial Assistant: Amanda Graham Sr Editorial Assistant: Cherie Case Publishing and Editorial for Technology Dummies Richard Swadley, Vice President and Executive Group Publisher Andy Cummings, Vice President and Publisher Mary Bednarek, Executive Acquisitions Director Mary C Corder, Editorial Director Publishing for Consumer Dummies Diane Graves Steele, Vice President and Publisher Composition Services Debbie Stailey, Director of Composition Services PDFgozar.com Contents at a Glance Introduction Part I: Some Essential Background Chapter 1: Excel 2010: Where It Came From 11 Chapter 2: Excel in a Nutshell 23 Chapter 3: Formula Tricks and Techniques 53 Chapter 4: Understanding Excel Files 77 Part II: Excel Application Development Chapter 5: What Is a Spreadsheet Application? 101 Chapter 6: Essentials of Spreadsheet Application Development 111 Part III: Understanding Visual Basic for Applications Chapter 7: Introducing Visual Basic for Applications .135 Chapter 8: VBA Programming Fundamentals 193 Chapter 9: Working with VBA Sub Procedures 241 Chapter 10: Creating Function Procedures 281 Chapter 11: VBA Programming Examples and Techniques 325 Part IV: Working with UserForms Chapter 12: Custom Dialog Box Alternatives 399 Chapter 13: Introducing UserForms 419 Chapter 14: UserForm Examples 455 Chapter 15: Advanced UserForm Techniques 493 Part V: Advanced Programming Techniques Chapter 16: Developing Excel Utilities with VBA 543 Chapter 17: Working with Pivot Tables 565 Chapter 18: Working with Charts 583 Chapter 19: Understanding Excel’s Events 639 Chapter 20: Interacting with Other Applications 677 Chapter 21: Creating and Using Add-Ins 703 Part VI: Developing Applications Chapter 22: Working with the Ribbon 733 Chapter 23: Working with Shortcut Menus 769 Chapter 24: Providing Help for Your Applications 789 Chapter 25: Developing User-Oriented Applications 809 PDFgozar.com vi Part VII: Other Topics Chapter 26: Compatibility Issues 825 Chapter 27: Manipulating Files with VBA 839 Chapter 28: Manipulating Visual Basic Components 871 Chapter 29: Understanding Class Modules 895 Chapter 30: Working with Colors 911 Chapter 31: Frequently Asked Questions about Excel Programming 937 Part VIII: Appendixes Appendix A: Excel Resources Online 969 Appendix B: VBA Statements and Functions Reference 977 Appendix C: VBA Error Codes 985 Appendix D: What’s on the CD-ROM 989 Index 1007 End-User License Agreement 1053 PDFgozar.com Table of Contents Introduction Part I: Some Essential Background Chapter 1: Excel 2010: Where It Came From 11 A Brief History of Spreadsheets 11 It all started with VisiCalc 11 Lotus 1-2-3 12 Quattro Pro 14 Microsoft Excel .15 Current Competition 20 Why Excel Is Great for Developers 20 Excel’s Role in Microsoft’s Strategy 22 Chapter 2: Excel in a Nutshell 23 Thinking in Terms of Objects 23 Workbooks 24 Worksheets 24 Chart sheets 26 XLM macro sheets 26 Excel 5/95 dialog sheets 28 Excel’s User Interface 28 About the Ribbon 28 Shortcut menus and the Mini Toolbar 34 Dialog boxes 35 Keyboard shortcuts 36 Smart Tags 36 Task pane 36 Customizing the Display 37 Data Entry 38 Formulas, Functions, and Names 38 Selecting Objects 40 Formatting 40 Protection Options 42 Protecting formulas from being overwritten 42 Protecting a workbook’s structure 43 Applying password protection to a workbook 43 Protecting VBA code with a password 43 Charts 44 Shapes and SmartArt 45 Database Access 46 Worksheet databases 46 External databases 47 PDFgozar.com viii Internet Features 47 Analysis Tools 48 Add-Ins 50 Macros and Programming 50 File Format 50 Excel’s Help System 51 Chapter 3: Formula Tricks and Techniques 53 About Formulas 53 Calculating Formulas 54 Cell and Range References 55 Why use references that aren’t relative? 55 About R1C1 notation 56 Referencing other sheets or workbooks 57 Using Names 58 Naming cells and ranges 59 Applying names to existing references 60 Intersecting names 61 Naming columns and rows 61 Scoping names .61 Naming constants 62 Naming formulas 63 Naming objects 65 Formula Errors 65 Array Formulas 66 An array formula example 66 An array formula calendar 67 Array formula pros and cons 68 Counting and Summing Techniques 69 Counting formula examples 70 Summing formula examples 70 Other counting tools 71 Working with Dates and Times 71 Entering dates and times 72 Using pre-1900 dates 73 Creating Megaformulas 74 Chapter 4: Understanding Excel Files 77 Starting Excel 77 File Types 80 Excel file formats 80 Text file formats .81 Database file formats 81 Other file formats 82 Working with Template Files 83 Viewing templates 83 Creating templates 84 Creating workbook templates 86 PDFgozar.com Index TintAndShade property ColorFormat object, 929, 934 FillFormat object, 931 Forecolor object, 930 overview, 924–925, 927 Title argument GetOpenFilename method, 409–410 GetOption function, 891 GetSaveAsFilename method, 412 Inputbox method, 402 title argument InputBox function, 400 MsgBox function, 219, 404 title bar removing X button, 959–960 UserForms without, 518–519, 962 Title property, AddIn object, 723 toggle buttons, 30 Toggle Folder icon, VBE, 143 Toggle procedure, 899–900 ToggleButton control, 426 ToggleButton1_Click procedure, 756 ToggleHelp procedure, 793 ToggleNumLock procedures, 900 TogglePageBreakDisplay callback procedure, 752, 753 TogglePageBreaks procedure, 544 ToggleRibbon procedure, 939 ToggleWordWrap procedure, 779 ToggleWrapText procedure, 357 TOOLBAR constant, 767 toolbars custom, 116–117, 736–737, 939 defined, 735 floating, 939 old-style, 764–767 resetting, 777 simulating with UserForms, 519–521 VBE, 142 Toolbox controls, 421–432, 448–452, 517–518, 526–528 customizing, 450–452 overview, 421 ToolTips, 157 ToolTipText property, CommandBar object, 774 Top argument AddChart method, 587 Inputbox method, 402 TransitionEffect property, MultiPage control, 486 TRANSPOSE function, 303, 349 1043 trial programs, 990 truepart argument, IIf function, 229 Trust Access to Visual Basic Project setting, Excel, 943 Trust Center dialog box, Excel, 872–873 turnkey applications, 110 TurnOffNoShiftF10 procedure, 675–676 - fmTabStyleNone setting, Style property, 512 *.txt (text) files See text (*.txt) files Txt argument, ExtractElement function, 378 Type argument AddChart method, 586 Inputbox method, 402–403 Type property ColorFormat object, 929 CommandBar object, 774 Type statement, 980 TypeName function example using, 310 function of, 201–202 nested Select Case structures, 231–232 overview, 984 toggling Boolean properties, 357 used by Workbook_SheetActivate procedures, 648– 649 typography, 127 U u prefix, 207 UBound function, 984 UCase function, 217, 275, 984 UI See user interface UI Builder, Quattro Pro, 15 Uncomment Block button, VBE, 197 underscore (line continuation) character, 4, 148, 171, 195, 944 Undo button, VBE, 148 Undo Change Case menu item, Excel, 558 Undo feature, 557–559, 656 UndoTextTools procedure, 552, 558 UnhideColumns procedure, 651 Union function, 345 union operator (,), 184 unlinked charts, creating, 621–622 Unload command, 434 Unload statement, 980 UnzipAFile procedure, 868 unzipping files, 867–868 PDFgozar.com 1044 Index UpCase function, 292 \update user workbook directory, 1004 UpdateBox procedure, 495 UpdateChart procedure, 602–603 UpdateClock procedure, 631, 671–672 UpdateColor procedure, 529 UpdateControls procedure, 509–510 UpdateDynamicRibbon procedure, 762 UpdateForm procedure, 799 UpdateLogFile procedure, 667 UpdateProgress procedure, 501, 502, 504 updating headers/footers, 651 spreadsheet applications, 129 UPPER function, 293 Use Relative References button, VBE, 162 UsedRange property, Worksheet object, 342 User Defined function category, 318 User function, 293, 294, 301 user interface (UI) defined, 28 determining most appropriate, 115, 117–122 Excel 2010, 19–20, 22, 28–37 FAQs, 964–965 Loan Amortization Wizard, 813–814 UseRandomColors procedure, 935 UserChoices array, 551 UserColor variable, 529 user-defined data types overview, 199, 216–217 passing arguments to procedures, 258 prefix for, 207 UserForm_Activate procedure, 460, 500 UserForm_Initialize procedure activating sheets, 482–484 creating resizable UserForms, 522–523 dialog box default size, 463 displaying charts in UserForms, 610 displaying UserForms without title bar, 518 handling multiple controls with one event handler, 528 Loan Amortization Wizard, 815 MyMsgBox function, 515 overview, 960 RefEdit control, 458 scrolling, 464–465 selecting colors in UserForms, 530 selecting worksheet rows, 480 Text Tools utility, 552 Windows Media Player control, 488 UserForm_QueryClose procedure, 462, 960 UserForm1 code module, Text Tools utility, 552–554 \userform1 directory, 1002 \userform2 directory, 1002 \userform3 directory, 1002 UserForms (custom dialog boxes) as alternative to dialog sheets, 28 alternatives to, 399–418 changing size of, 462–464 charts, displaying in, 531–532, 609–611, 959 checklist for, 453 closing, 434–435 code module, 641 controls, 517–518, 727, 883–886 copying, 145 creating, 119–120, 420, 435–442, 455–457, 886–893 disabling Close button, 461–462 dismissing, 432–433, 439 displaying, 432–434 duplicating Excel dialog boxes, 453 Enhanced Data Form, 534–537 events, 442–448, 640, 669–670 Export Charts utility, 709 FAQs, 959–962 Form Grid Settings section, VBE, 157 generating list of files and directories into, 961 help, displaying through, 795–799 hiding, 435, 960 inserting new, 420–421 keeping open, 960 Label control, animating, 489–491 ListBox control examples, 466–485 modeless dialog boxes, 493–497 MultiPage control example, 485–486 MyMsgBox function, 513–517 naming, 420 with no title bar, 518–519 overview, 419–420 progress indicators, 497–506 puzzles on, 537–538 release of, 19 removing, 145 resizable, 521–525 selecting colors in, 529–530 selecting ranges from, 457–459 semitransparent, 532–534 simulating toolbars with, 519–521 splash screens, 459–461 startup position of, 962 PDFgozar.com Index templates, creating, 452 testing, 432 for Text Tools utility, 548–550 Toolbox, customizing, 450–452 Toolbox controls, 422–432, 448–450, 526–528 uniform sizing, 962 unloading, 960 using Control tips in, 796 using Shape objects in, 960 video poker game on, 538–539 Windows Media Player control example, 486–488 wizards, creating, 507–512 zooming and scrolling sheets from, 464–466 user-friendly, defined, 104 UserInfo folder, Windows Registry, 96 UserInterfaceOnly option, 943 UserName property, Application object, 293 UserOption function, 892 user-oriented applications checklist for, 821 defined, 809 development concepts, 820–821 Loan Amortization Wizard, 809–820 overview, 809 users See end users utilities defined, 543 elements common to good utilities, 545 Export Charts, 708–713 overview, 108, 543–544 Text Tools, 545–563 V v prefix, 207 Val function, 401, 984 validating data monitoring specific ranges for, 657–660 user input, 332–333 UserForms, 441–442 #VALUE! error value constant for, 306 debugging functions, 313 meaning of, 66, 293 returning, 949 \value from closed workbook directory, 994 Value property ListBox control, 472 OptionButton control, 437 1045 Range object, 169–170, 181 retrieving information from cells, 186 SpinButton control, 446, 448 Value2 property, 186 values argument, SERIES formula, 601 Values property, Series object, 600, 603–604 VALUES_FROM_SERIES function, 604, 605–606 variable declaration, 154 variable-length strings, defined, 209 variables copying ranges, 327 declaring, 199, 201 displaying UserForms based on, 433 forcing declaration of, 202–203 naming, 197, 207 object, 215–216 overview, 139, 197–198 scoping, 203–206 Variance field, budget pivot table, 572, 574 variant arrays, defined, 944–945 Variant data type, 199, 201–202, 207, 301 variant data types, defined, 944 VariantDemo procedures, 201 variants, defined, 944–945 VarType function, 984 VBA See Function procedures; ranges; Visual Basic for Applications (VBA); Visual Basic for Applications (VBA) code VBA_Demo procedure, 193–194 vbAbort constant, MsgBox function, 406 vbAbortRetryIgnore constant, MsgBox function, 405 vbCancel constant, MsgBox function, 406 VBComponents collection, 875 vbCritical constant, MsgBox function, 405 vbCrLf constant, 298, 407 vbDefaultButton1 constant, MsgBox function, 405 vbDefaultButton2 constant, MsgBox function, 405 vbDefaultButton3 constant, MsgBox function, 405 vbDefaultButton4 constant, MsgBox function, 405 vbDirectory attribute, Dir function, 843 VBE See Visual Basic Editor (VBE) VBE (Visual Basic Environment), 873 vbExclamation constant, MsgBox function, 405 vbHidden attribute, Dir function, 843 VBIDE object, 871 vbIgnore constant, MsgBox function, 406 vbInformation constant, MsgBox function, 405 vbModeless argument, 494, 551 vbMsgBoxHelpButton constant, MsgBox function, 405 PDFgozar.com 1046 Index vbNewLine constant, MsgBox function, 407 vbNo constant, MsgBox function, 406 vbNormal attribute, Dir function, 843 vbOK constant, MsgBox function, 406 vbOKCancel constant, MsgBox function, 405 vbOKOnly constant, MsgBox function, 405 VBProject property, Workbook object, 874 VBProjects collection, 874–876 vbQuestion constant, MsgBox function, 405 vbReadOnly attribute, Dir function, 843 vbRetry constant, MsgBox function, 406 vbRetryCancel constant, MsgBox function, 405 vbSystem attribute, Dir function, 843 vbSystemModal constant, MsgBox function, 405 vbTab constant, 298, 407 vbVolume attribute, Dir function, 843 vbYes constant, MsgBox function, 406 vbYesNo constant, MsgBox function, 405 vbYesNoCancel constant, MsgBox function, 405 video determining display information, 389–390 recommended driver, spreadsheet application development, 130–131 video poker game, on UserForm, 538–539 View tab, Excel, 734, 742 ViewCustomViews control, 738 viruses, 944 Visible property CommandBar object, 774 Comment object, 174 visibility of workbooks, 715 VisiCalc, 11–12 Visual Basic Editor (VBE) activating, 141–142 changes to, 37 code windows, 145–153 commands, as they appear in book, components of, 142–143 customizing, 153–159 displaying Developer tab, 141 FAQs, 942–944 inserting class modules, 897 overview, 140 Project Explorer window, 143–145 Visual Basic Environment (VBE), 873 Visual Basic for Applications (VBA) See also Function procedures; ranges Application object properties, 180–181 arrays, 213–215, 362–363 assignment statements, 210–213 BASIC, 135–136 Boolean properties, toggling, 357 built-in functions, 217–220 charts, 589–592, 603–608 collections, 167–168, 220–223 Comment object, 172–179 comments, 195–197 components, 871–893 constants, 206–208 data types, 198–202, 216–217 date and time, 73, 209–210, 358–360 developing Excel utilities with, 543–563 displaying data forms using, 418 elements of, 193–194 enhancements to, 27 error codes, 985–988 fast-food restaurant chain analogy, 139–140 file manipulation, 363–364, 839–870 fonts, getting list of, 360–361 formulas versus, 114 functions, 977–984 future of, 137 language considerations, 834 learning, 115, 325–326 macro recorder, 159–166 Microsoft Office support for, 22 modules, 144–145, 147–153 object models, 136 objects, 167–171, 188–192, 220–223 origin of, 18–19 overview, 137–139 passwords, protecting code with, 43–44 printed pages, determining number of, 358 procedures, 241–244, 255–263 purpose of, 21 Range object, 182, 184–187 shortcut menus, 777–783 statements, 977–984 strings, 209 Sub procedures, 242, 244–255, 264–280 using with Ribbon, 737–743 variables, 197–198, 201–206, 215–216 VBE, 140–159 workbooks, 354, 368–370 worksheets, 354–357 XLM versus, 136 Visual Basic for Applications (VBA) code as appears in book, 3–4 CheckBox control, 752–754 PDFgozar.com Index colors, 943 controlling execution of, 223–240 entering in code windows, 147–153 examples of, 942 keyboard conventions used in book, 3–4 protecting with passwords, 43–44 speed, 959 SpinButton control events initiated by, 445–446 storing, 146–147 Visual Basic for Windows, release of, 135 VLOOKUP function, 296, 633–634 Volatile method, 295 VowelCount function, 313–314 VSTO (Microsoft Visual Studio Tools for Office), W WAV files, 390–391 Web browser accessing newsgroups, 971–972 displaying help through, 799–801 \web browser directory, 1002 Web documents, 680 Web queries, creating, 48 Web sites author’s, 8, 108 Contextures, 974 Custom UI Editor for Microsoft Office, 747 Daily Dose of Excel, 974 David McRitchie’s Excel pages, 975 file viewer, 129 HTML Help Workshop, 803 Jon Peltier’s Excel page, 974 Microsoft Excel, 970 Microsoft Knowledge Base, 970 Microsoft Office, 970 Microsoft Office compatibility pack, 81 Microsoft support, 826, 970 Mr Excel, 975 Pearson Software Consulting, 974 Pointy Haired Dilbert, 974 Pope, Andy, 521 Spreadsheet Page, 973 VBA code examples, 942 VisiCalc, 12 Wiley Publishing, Weekday function, 230, 984 WeekdayName function, 984 1047 What You See Is What You Get (WYSIWYG) mode, Lotus 1-2-3, 14 what-if models, 109 While Wend loops, 240 While.Wend statement, 980 Width # statement, 980 Width argument, AddChart method, 587 win32api.txt file, 323, 993 WindowActivate event Application object, 665 Workbook object, 647 WindowDeactivate event Application object, 665 Workbook object, 647 WindowResize event Application object, 665 Workbook object, 647 windows arranging for macro-recorder feature, 160–161 closing automatically when collapsing projects, 157 counting number of hidden, 222 docking, 158–159 Windows 7, 701 Windows Application Programming Interface (API) 64-bit version of Excel, 320 compile errors, 949–950 creating resizable UserForms, 521 Function procedures, 320–323, 386–394 Windows Calculator application activating, 681–682 displaying Scientific mode, 701 launching, 677–678 Windows check box, Excel, 43 Windows collection, 222 Windows Control Panel, 683–684 Windows directory, determining, 321–322 Windows Help system (WinHelp), 801–802 Windows Media Player control, 486–488 Windows Registry accessing, 395 color values stored in, 530 Excel settings in, 94–97 Loan Amortization Wizard default values, 818–819 reading from, 392–393 rebuilding Excel Registry keys, 97 Text Tools utility settings stored in, 555–557 when updated, 95 writing to, 392–394 Windows Scripting Host, 845 PDFgozar.com 1048 Index Windows Vista, 79 WindowsDir function, 322 WindowsOS function, 829 WinHelp (Windows Help system), 801–802 win/loss sparklines, 635 With statement, 980 With-End With constructs function of, 727 improving speed with, 216 MakeMemos procedure, 691 making macros efficient with, 164–165 overview, 220–221 when changing only one property, 151 WithEvents keyword, 615–616 wizards See also Loan Amortization Wizard adding buttons, 508 defined, 507 overview, 507–508 performing tasks with, 512 programming buttons, 508–510 programming dependencies in, 510–511 setting up MultiPage control for, 508 Word, 682, 685–695 workaround, defined, 123 Workbook object, 167, 639, 646–653 Workbook_Activate procedure, 648 Workbook_AddInInstall procedure, 726, 728–729 Workbook_BeforeClose procedure, 672, 859 Workbook_BeforePrint procedure, 645–646, 650–651 Workbook_BeforeSave procedure, 649–650 Workbook_Deactivate procedure, 650 Workbook_NewSheet procedure, 649 Workbook_Open procedure, 814, 858, 946 Workbook_SheetActivate procedure, 495, 644, 645, 648–649, 752 Workbook_SheetSelectionChange procedure, 495 WorkbookActivate event, Application object, 665 WorkbookAddinInstall event, Application object, 665 WorkbookAddinUninstall event, Application object, 665 WorkbookBeforeClose event, Application object, 665 WorkbookBeforePrint event, Application object, 665 WorkbookBeforeSave event, Application object, 665 WorkbookDeactivate event, Application object, 665 WorkbookIsOpen function, 368 workbook-level scope, 61 WorkbookName function, 374 WorkbookNewSheet event, 640, 665 WorkbookOpen event, Application object, 665 workbooks accessing add-ins as, 725–726 active, 24 add-ins versus, 703–704 automatic loading, 946 closing all, 354 closing all except active, 222–223 converting to add-ins, 707–708, 963 copying worksheets from add-ins to, 716 creating within add-ins, 716 default number of worksheets, changing, 25 defaults, using workbook template to change, 85 determining when opened, 666–667 determining whether are add-ins, 725 displaying multiple windows, 24 files, 114 hiding window containing, 24 Loan Amortization Wizard, 812–813 overview, 24 passwords, applying to, 43 protecting, 43, 125 referencing, 57 retrieving values from closed, 368–370 saving, 354, 699–700 sending as e-mail attachments, 698 Sub procedures, executing from procedures in, 250–252 templates, 85–87 for Text Tools utility, 547–548 viruses, 944 visibility of, 715 Workbooks collection, 167, 222–223 Workbooks method, Application object, 714 WorkbookSetup procedure, 641 WorkRange object, 557 worksheet databases, accessing, 46–47 \worksheet directory, 1002 Worksheet events, 639 worksheet formulas deleting values, 953 error values, 948–949 forcing recalculation of, 949–950 Function procedures, executing in, 289–290 worksheet functions custom, 22 playing sound from, 391–392 simplifying access to, 705 Worksheet object Cells property, 184–187 events, 654–662 PDFgozar.com Index objects contained in, 167 Range property, 182, 184 UsedRange property, 342 worksheet sort method defined, 362 speed of, 363 Worksheet_Activate procedure, 785 Worksheet_BeforeDoubleClick procedure, 661 Worksheet_BeforeRightClick procedure, 662, 675, 787 Worksheet_Change procedure, 654–660 Worksheet_Deactivate procedure, 785 Worksheet_SelectionChange procedure, 602, 660 WorksheetFunction object, 218 worksheet-level scope, 61 worksheets activating with ListBox control, 482–485 copying from add-ins to workbooks, 716 default number of, changing, 25 defaults, using worksheet template to change, 85–86 displaying help through, 794–795 drawing layer, 25 embedding UserForm controls into, 425 Function procedures, 283–284, 370–386 hiding all but selections, 354–356 Loan Amortization Wizard, 816–818 maximum number of cells, 25 maximum number of columns and rows, 24 multiple, purpose of using, 25 overview, 24 preventing scrolling, 952 referencing, 57 restricting access to, 704 returning maximum value across all, 381–382 saving as PDF files, 699 scrolling from UserForms, 464–466 sending as e-mail attachments, 699 size of, 25 synchronizing, 356–357 templates, 85–86 using ActiveX controls on, 120–122 in XLAM and XLSM files, 716 zooming from UserForms, 464–466 Worksheets collection, 167, 222 worksheets folder, 90 workspace files, 82 wrapper functions, 294, 322, 393 wrapping text, 17 WrapText property, 357 Write # statement, 854, 980 1049 WriteDate procedure, 837 WriteReadRange procedure, 346–347 WriteRegistry function, 394 WYSIWYG (What You See Is What You Get) mode, Lotus 1-2-3, 14 X X button, 959–960 XDATEADD(xdate1,days,fmt) function, 311 XDATEDAY(xdate1) function, 311 XDATEDIF(xdate1,xdate2) function, 311 XDATEDOW(xdate1) function, 311 XDATEMONTH(xdate1) function, 311 XDATE(y,m,d,fmt) function, 311, 312 XDATEYEARDIF(xdate1,xdate2) function, 311 XDATEYEAR(xdate1) function, 311 xl folder, 89 XL_NewWorkbook procedure, 668 XL12OrLater function, 828 xl24HourClock constant, International property, 836 xl4DigitYears constant, International property, 836 *.xla files, 81, 711 xlAlternateArraySeparator constant, International property, 835 *.xlam files See also add-ins accessing VBA procedures in add-ins, 717–718 creating, 963 defined, 711, 990 overview, 93 VBA collection membership, 714–715 visibility of, 715 worksheets and chart sheets in, 716 *.xlb files, 92–93 xlColumnSeparator constant, International property, 835 xlCountryCode constant, International property, 835 xlCountrySetting constant, International property, 835 xlCurrencyBefore constant, International property, 836 xlCurrencyCode constant, International property, 836 xlCurrencyDigits constant, International property, 836 xlCurrencyLeadingZeros constant, International property, 836 PDFgozar.com 1050 Index xlCurrencyMinusSign constant, International property, 836 xlCurrencyNegative constant, International property, 836 xlCurrencySpaceBefore constant, International property, 836 xlCurrencyTrailingZeros constant, International property, 836 xlDateOrder constant, International property, 836 xlDateSeparator constant, International property, 835 xlDayCode constant, International property, 836 xlDayLeadingZero constant, International property, 836 xlDecimalSeparator constant, International property, 835 xlGeneralFormatName constant, International property, 836 xlHourCode constant, International property, 836 *.xll files, 93, 711 xlLandscape constant, 208 xlLandscape variable, 151 xlLeftBrace constant, International property, 835 xlLeftBracket constant, International property, 835 xlListSeparator constant, International property, 835 xlLowerCaseColumnLetter constant, International property, 835 xlLowerCaseRowLetter constant, International property, 835 XLM macro language GetValue function, 368 overview, 16 VBA versus, 136 XLM macro sheets, 26 XML versus, 136 xlMDY constant, International property, 836 xlMetric constant, International property, 836 xlMinuteCode constant, International property, 836 xlMonthCode constant, International property, 836 xlMonthLeadingZero constant, International property, 836 xlMonthNameChars constant, International property, 836 xlNoncurrencyDigits constant, International property, 836 xlNonEnglishFunctions constant, International property, 836 xlPortrait constant, 208 xlPortrait variable, 151 xlRightBrace constant, International property, 835 xlRightBracket constant, International property, 835 xlRowSeparator constant, International property, 835 *.xls files, 24, 80, 81 *.xlsb files, 80 xlSecondCode constant, International property, 836 *.xlsm (Macro-Enabled) files accessing VBA procedures in add-ins, 717–718 converting XLS files to, 24 defined, 990 keeping, 963 overview, 80 parts of, 87–90 visibility of, 715 worksheets and chart sheets in, 716 XLStart directory, 77, 85 *.xlsx files, 24, 80, 990 *.xlt files, 80 xlThousandsSeparator constant, International property, 835 xlTimeLeadingZero constant, International property, 836 xlTimeSeparator constant, International property, 835 *.xltm (Macro-Enabled Template) files, 80, 87 *.xltx (template) files, 80 xlUpperCaseColumnLetter constant, International property, 835 xlUpperCaseRowLetter constant, International property, 835 *.xlw (workspace files), 82 xlWeekdayNameChars constant, International property, 836 xlYearCode constant, International property, 835 XML (eXtensible Markup Language) advantages of, 91 displaying in Web browsers, 90 Excel 2003, 19 Excel 2007, 19 exporting ranges to, 863–865 as open format, 87 XLM versus, 136 XML Paper Specification (*.xps) format, 82 XML Spreadsheet (XMLSS; *.xml) format, 81 XMLSS (XML Spreadsheet) format, 81 Xor operator, 212, 946 PDFgozar.com Index xpos argument, InputBox function, 400 *.xps (XML Paper Specification) format, 82 XValues property, Series object, 600, 603 XVALUES_FROM_SERIES function, 604, 605 Z Y zipping files, 865–867 zoom control, 37, 464–466 Zoom event, UserForm object, 670 y argument, XDATE function, 312 Year function, 984 ypos argument, InputBox function, 400 ZapRange procedure, 171 ZapTheVowels procedure, 284 *.zip (ZIP) files, 87, 90, 745 ZipFiles procedure, 865–866 1051 PDFgozar.com 1052 Index PDFgozar.com Wiley Publishing, Inc End-User License Agreement READ THIS You should carefully read these terms and conditions before opening the software packet(s) included with this book (“Book”) This is a license agreement (“Agreement”) between you and Wiley Publishing, Inc (“WPI”) By opening the accompanying software packet(s), you acknowledge that you have read and accept the following terms and conditions If you not agree and not want to be bound by such terms and conditions, promptly return the Book and the unopened software packet(s) to the place you obtained them for a full refund 1.License Grant WPI grants to you (either an individual or entity) a nonexclusive license to use one copy of the enclosed software program(s) (collectively, the “Software”) solely for your own personal or business purposes on a single computer (whether a standard computer or a workstation component of a multi-user network) The Software is in use on a computer when it is loaded into temporary memory (RAM) or installed into permanent memory (hard disk, CD-ROM, or other storage device) WPI reserves all rights not expressly granted herein 2.Ownership WPI is the owner of all right, title, and interest, including copyright, in and to the compilation of the Software recorded on the physical packet included with this Book (“Software Media”) Copyright to the individual programs recorded on the Software Media is owned by the author or other authorized copyright owner of each program Ownership of the Software and all proprietary rights relating thereto remain with WPI and its licensers Restrictions on Use and Transfer (a)You may only (i) make one copy of the Software for backup or archival purposes, or (ii) transfer the Software to a single hard disk, provided that you keep the original for backup or archival purposes You may not (i) rent or lease the Software, (ii) copy or reproduce the Software through a LAN or other network system or through any computer subscriber system or bulletin-board system, or (iii) modify, adapt, or create derivative works based on the Software (b)You may not reverse engineer, decompile, or disassemble the Software You may transfer the Software and user documentation on a permanent basis, provided that the transferee agrees to accept the terms and conditions of this Agreement and you retain no copies If the Software is an update or has been updated, any transfer must include the most recent update and all prior versions 4.Restrictions on Use of Individual Programs You must follow the individual requirements and restrictions detailed for each individual program in the “What’s on the CD-ROM” appendix of this Book or on the Software Media These limitations are also contained in the individual license agreements recorded on the Software Media These limitations may include a requirement that after using the program for a specified period of time, the user must pay a registration fee or discontinue use By opening the Software packet(s), you agree to abide by the licenses and restrictions for these individual programs that are detailed in the “About the CD” appendix and/or on the Software Media None of the material on this Software Media or listed in this Book may ever be redistributed, in original or modified form, for commercial purposes PDFgozar.com 5.Limited Warranty (a)WPI warrants that the Software and Software Media are free from defects in materials and workmanship under normal use for a period of sixty (60) days from the date of purchase of this Book If WPI receives notification within the warranty period of defects in materials or workmanship, WPI will replace the defective Software Media (b)WPI AND THE AUTHOR(S) OF THE BOOK DISCLAIM ALL OTHER WARRANTIES, EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE SOFTWARE, THE PROGRAMS, THE SOURCE CODE CONTAINED THEREIN, AND/ OR THE TECHNIQUES DESCRIBED IN THIS BOOK WPI DOES NOT WARRANT THAT THE FUNCTIONS CONTAINED IN THE SOFTWARE WILL MEET YOUR REQUIREMENTS OR THAT THE OPERATION OF THE SOFTWARE WILL BE ERROR FREE (c)This limited warranty gives you specific legal rights, and you may have other rights that vary from jurisdiction to jurisdiction Remedies (a)WPI’s entire liability and your exclusive remedy for defects in materials and workmanship shall be limited to replacement of the Software Media, which may be returned to WPI with a copy of your receipt at the following address: Software Media Fulfillment Department, Attn.: Excel 2010 Power Programming with VBA, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, or call 1-800-762-2974 Please allow four to six weeks for delivery This Limited Warranty is void if failure of the Software Media has resulted from accident, abuse, or misapplication Any replacement Software Media will be warranted for the remainder of the original warranty period or thirty (30) days, whichever is longer (b)In no event shall WPI or the author be liable for any damages whatsoever (including without limitation damages for loss of business profits, business interruption, loss of business information, or any other pecuniary loss) arising from the use of or inability to use the Book or the Software, even if WPI has been advised of the possibility of such damages (c)Because some jurisdictions not allow the exclusion or limitation of liability for consequential or incidental damages, the above limitation or exclusion may not apply to you 7.U.S Government Restricted Rights Use, duplication, or disclosure of the Software for or on behalf of the United States of America, its agencies, and/or its instrumentalities (“U.S Government”) is subject to restrictions as stated in paragraph (c)(1)(ii) of the Rights in Technical Data and Computer Software clause of DFARS 252.227-7013, or subparagraphs (c) (1) and (2) of the Commercial Computer Software - Restricted Rights clause at FAR 52.227-19, and in similar clauses in the NASA FAR supplement, as applicable 8.General This Agreement constitutes the entire understanding of the parties and revokes and supersedes all prior agreements, oral or written, between them and may not be modified or amended except in a writing signed by both parties hereto that specifically refers to this Agreement This Agreement shall take precedence over any other documents that may be in conflict herewith If any one or more provisions contained in this Agreement are held by any court or tribunal to be invalid, illegal, or otherwise unenforceable, each and every other provision shall remain in full force and effect PDFgozar.com Special Offer Save $30.00! Power Utility Pak v7 “The Excel tools Microsoft forgot” A $39.95 value Yours for only $9.95 Pro-Quality Tools VBA Source Code is Available PUP v7 is a handy collection of 60 general purpose Excel utilities, plus 50 new worksheet functions Download a trial version from the URL at the bottom of this page If you like it, use this coupon and save $30 on the licensed version You can also get the complete VBA source files for only $20.00 more Learn how the utilities and functions were written, and pick up useful tips and programming techniques in the process This is a must for all VBA programmers YES! I want Power Utility Pak v7 Name: _ Company: Address: City: _State: Zip: Check one: PUP v7 Licensed Version $9.95 Developer’s Pak: Licensed Version ($9.95) + VBA Source ($20.00) .$29.95 Upon receipt of this coupon, you will receive download instructions via e-mail Please make your e-mail address legible E-mail: _ Credit Card: Expires: Make check or money order (U.S funds only) payable to: JWalk & Associates Inc P.O Box 68797 Tucson, AZ 85737 (USA) Download a free 30-day trial version of PUP from: http://spreadsheetpage.com PUP v7 is compatible only with Excel 2007 and Excel 2010 For earlier versions of Excel, use PUP v6 PDFgozar.com PDFgozar.com Get more from Excel—learn to extend it with VBA Learn to use Visual Basic for Applications (VBA), and you can expand the already awesome power of Excel 2010 John Walkenbach, aka Mr Spreadsheet, shows you how to it in this easy-to-follow guide If you’re already an experienced Excel user, this book will make you an Excel master You'll learn a new approach to Excel and the steps involved in developing a new spreadsheet application You'll also discover how to develop VBA subroutines and functions, use advanced programming techniques, and more And if you’re switching to Excel 2010 from an earlier version, there’s a section to get you up to speed on the new features John Walkenbach, arguably the foremost authority on Excel, has written hundreds of articles and created the award-winning Power Utility Pak His 50-plus books include John Walkenbach's Favorite Excel 2010 Tips & Tricks, Excel 2010 Formulas, and the bestselling Excel Bible, all published by Wiley Visit his popular Spreadsheet Page at spreadsheetpage.com No one can teach you more about Excel than Mr Spreadsheet Let Mr Spreadsheet show you how to: CD-ROM INCLUDES: • More than 300 files used as examples in the book • Searchable PDF of the book See Appendix D for complete system requirements • Create powerful Excel apps with VBA • Develop user-friendly dialog boxes • Enhance Excel with custom worksheet functions • Write event-driven VBA code ... 990 eBook version of Excel 2010 Power Programming with VBA 990 Sample files for Excel 2010 Power Programming with VBA 990 Troubleshooting... control Excel and other Microsoft Office applications What You Need to Know This is not a book for beginning Excel users If you have no experience with Excel, a better choice might be my Excel. .. book, you need a copy of Excel 2010 Although most of the material also applies to Excel 2003 and later versions, I assume that you’re using Excel 2010 Although Excel 2007 and Excel 2010 are radically