Mastering ® VBA for Microsoft Office 2007 Richard Mansfield WileyPublishing,Inc. Mastering ® VBA for Microsoft Office 2007 Mastering ® VBA for Microsoft Office 2007 Richard Mansfield WileyPublishing,Inc. AcquisitionsEditor:TomCirtin DevelopmentEditor:DavidClark TechnicalEditor:RussMullen ProductionEditor:RachelGunn CopyEditor:LizWelch ProductionManager:TimTate VicePresidentandExecutiveGroupPublisher:RichardSwadley VicePresidentandExecutivePublisher:JosephB.Wikert VicePresidentandPublisher:NeilEdde BookDesigner:MaureenForysandJudyFung Compositor:LaserwordsPrivateLimited,Chennai,India Proofreaders:KathyPope,SheilahLedwidge Indexer:NancyGuenther CoverDesigner:RyanSneed CoverImage:(c)PeteGardner/DigitalVision/gettyimages Copyright© 2008byWileyPublishing,Inc.,Indianapolis,Indiana PublishedsimultaneouslyinCanada. ISBN:978-0-470-27959-5 Nopartofthispublicationmaybereproduced,storedinaretrievalsystemortransmittedinanyformorbyanymeans,electronic, mechanical,photocopying,recording,scanningorotherwise,exceptaspermittedunderSections107or108ofthe1976UnitedStates CopyrightAct,withouteitherthepriorwrittenpermissionofthePublisher,orauthorizationthroughpaymentoftheappropriate per-copyfeetotheCopyrightClearanceCenter,222RosewoodDrive,Danvers,MA01923,(978)750–8400,fax(978)646–8600. RequeststothePublisherforpermissionshouldbeaddressedtotheLegalDepartment,WileyPublishing,Inc.,10475Crosspoint Blvd.,Indianapolis,IN46256,(317)572–3447,fax(317)572–4355,oronlineathttp://www.wiley.com/go/permissions. LimitofLiability/DisclaimerofWarranty:Thepublisherandtheauthormakenorepresentationsorwarrantieswithrespectto theaccuracyorcompletenessofthecontentsofthisworkandspecificallydisclaimallwarranties,includingwithoutlimitation warrantiesoffitnessforaparticularpurpose.Nowarrantymaybecreatedorextendedbysalesorpromotionalmaterials.Theadvice andstrategiescontainedhereinmaynotbesuitableforeverysituation.Thisworkissoldwiththeunderstandingthatthepublisher isnotengagedinrenderinglegal,accounting,orotherprofessionalservices.Ifprofessionalassistanceisrequired,theservicesofa competentprofessionalpersonshouldbesought.Neitherthepublishernortheauthorshallbeliablefordamagesarisingherefrom. ThefactthatanorganizationorWebsiteisreferredtointhisworkasacitationand/orapotentialsourceoffurtherinformationdoes notmeanthattheauthororthepublisherendorsestheinformationtheorganizationorWebsitemayprovideorrecommendationsit maymake.Further,readersshouldbeawarethatInternetWebsiteslistedinthisworkmayhavechangedordisappearedbetween whenthisworkwaswrittenandwhenitisread. Forgeneralinformationonourotherproductsandservicesortoobtaintechnicalsupport,pleasecontactourCustomerCare DepartmentwithintheU.S.at(800)762–2974,outsidetheU.S.at(317)572–3993orfax(317)572–4002. Wileyalsopublishesitsbooksinavarietyofelectronicformats.Somecontentthatappearsinprintmaynotbeavailableinelectronic books. LibraryofCongressCataloging-in-PublicationData Mansfield,Richard,1945- MasteringVBAforMicrosoftOffice2007/RichardMansfield. —2nded. p.cm. ISBN978-0-470-27959-5(paper/website) 1.MicrosoftOffice.2.MicrosoftVisualBasicforapplications.3.Business–Computerprograms.I.Title. HF5548.4.M525M35462008 005.5–dc22 2008008481 TRADEMARKS:Wiley,theWileylogo,andtheSybexlogoaretrademarksorregisteredtrademarksofJohnWiley&Sons,Inc. and/oritsaffiliates,intheUnitedStatesandothercountries,andmaynotbeusedwithoutwrittenpermission.Microsoftisa registeredtrademarkofMicrosoftCorporationintheUnitedStatesand/orothercountries.Allothertrademarksarethepropertyof theirrespectiveowners.WileyPublishing,Inc.,isnotassociatedwithanyproductorvendormentionedinthisbook. 10987654321 Dear Reader ThankyouforchoosingMasteringVBAforMicrosoftOffice2007.Thisbookispartofafamilyof premiumqualitySybexbooks,allwrittenbyoutstandingauthorswhocombinepracticalexperi- encewithagiftforteaching. Sybexwasfoundedin1976.Morethanthirtyyearslater,we’restillcommittedtoproducing consistentlyexceptionalbooks.Witheachofourtitleswe’reworkinghardtosetanewstandard fortheindustry.Fromtheauthorsweworkwith,tothepaperweprinton,ourgoalistobringyou thebestbooksavailable. Ihopeyouseeallthatreflectedinthesepages.I’dbeveryinterestedtohearyourcomments andgetyourfeedbackonhowwe’redoing.Feelfreetoletmeknowwhatyouthinkaboutthisor [email protected],orifyouthinkyou’vefound atechnicalerrorinthisbook,pleasevisithttp://sybex.custhelp.com. Customerfeedbackis criticaltooureffortsatSybex. Bestregards, NeilEdde VicePresidentandPublisher Sybex,anImprintofWiley TomygoodfriendDavidLeeRoach