ebook img

VBA for Modelers: Developing Decision Support Systems PDF

722 Pages·2011·11.359 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview VBA for Modelers: Developing Decision Support Systems

Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. VBA FOR MODELERS DEVELOPING DECISION SUPPORT SYSTEMS WITH ® ® MICROSOFT OFFICE EXCEL FOURTH EDITION S. Christian Albright Kelley School of Business, Indiana University Australia(cid:129)Brazil(cid:129)Japan(cid:129)Korea(cid:129)Mexico(cid:129)Singapore(cid:129)Spain(cid:129)UnitedKingdom(cid:129)UnitedStates Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. This is an electronic version of the print textbook. Due to electronic rights restrictions, some third party content may be suppressed. Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. The publisher reserves the right to remove content from this title at any time if subsequent rights restrictions require it. For valuable information on pricing, previous editions, changes to current editions, and alternate formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for materials in your areas of interest. Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. VBAforModelers:DevelopingDecision ©2012,2010South-Western,CengageLearning SupportSystemswithMicrosoft®Office ALLRIGHTSRESERVED.Nopartofthisworkcoveredbythecopyright Excel®,FourthEdition hereinmaybereproduced,transmitted,stored,orusedinanyformorby S.ChristianAlbright anymeansgraphic,electronic,ormechanical,includingbutnotlimitedto photocopying,recording,scanning,digitizing,taping,webdistribution, SeniorAcquisitionsEditor:Charles informationnetworks,orinformationstorageandretrievalsystems, McCormick,Jr. exceptaspermittedunderSection107or108ofthe1976UnitedStates DevelopmentalEditor:TedKnight CopyrightAct,withoutthepriorwrittenpermissionofthepublisher. EditorialAssistant:CourtneyBavaro MarketingManager:AdamMarsh Forproductinformationandtechnologyassistance,contactusat CengageLearningCustomer&SalesSupport,1-800-354-9706 MarketingCoordinator:SuellenRuttkay Forpermissiontousematerialfromthistextorproduct, SeniorMarketingCommunicationsManager: submitallrequestsonlineatwww.cengage.com/permissions LibbyShipp Furtherpermissionsquestionscanbee-mailedto ProductionManager:JeanButtrom [email protected] PermissionsAcquisitionsManager:JohnHill LibraryofCongressControlNumber:2011934057 MediaEditor:ChrisValentine ManufacturingBuyer:MirandaKlapper ISBN-13:978-1-133-19089-9 SeniorArtDirector:StacyJenkinsShirley ISBN-10:1-133-19089-8 ProductionService&Composition: PreMediaGlobal South-Western ContentProjectManagement:PreMediaGlobal 5191NatorpBoulevard Mason,OH45040 InternalDesigner:PreMediaGlobal USA CoverDesigner:LouAnnThesing CoverImage:©iStockImages/ChrisAlbright CengageLearningproductsarerepresentedinCanadaby NelsonEducation,Ltd. Foryourcourseandlearningsolutions,visitwww.cengage.com Purchaseanyofourproductsatyourlocalcollegestoreoratour preferredonlinestorewww.cengagebrain.com Printed in the United States of America 1 2 3 4 5 6 7 15 14 13 12 11 Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. To my wonderful wife, Mary—she is my best friend and constant companion. To our talented son, Sam, his equally talented wife, Lindsay, and our first (and only) grandson, Teddy. And to Bryn, our dear Welsh corgi who does her best to keep us feeling young. Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. About the Author S. Christian Albright Chris Albright got his B.S. degree in Mathematics from Stanford in 1968 and his Ph.D.inOperationsResearchfromStanfordin1972.Sincethenhehasbeenteaching intheOperations&DecisionTechnologiesDepartmentintheKelleySchoolofBusi- nessatIndianaUniversity(IU).Hehastaughtcoursesinmanagementscience,com- puter simulation, statistics, and computer programming to all levels of business students: undergraduates, MBAs, and doctoral students. In addition, he has taught simulationmodelingatGeneralMotorsandWhirlpool,hehastaughtdatabaseanaly- sisfortheArmy,andheiscurrentlyteachingVBAprogrammingatMicrosoft.Hehas publishedover20articlesinleadingoperationsresearchjournalsintheareaofapplied probability,andhehasauthoredthebooksStatisticsforBusinessandEconomics,Prac- tical Management Science, Spreadsheet Modeling and Applications, Data Analysis for Managers, Data Analysis and Decision Making, and VBA for Modelers. He also workedwiththePalisadeCorporationonthecommercialversion,StatTools,ofhissta- tisticalStatProadd-inforExcel.Hiscurrentinterestsareinspreadsheetmodeling,the development of VBA applications in Excel, and programming in the .NET environ- ment.After39yearsofteachingatKelley,Chrisretiredattheendofthe2010–2011 academicyear,buthekeepsplentybusyrevisingtextbooksandconsultingforPalisade. On the personal side, Chris has been married for 40 years to his wonderful wife, Mary, who retired several years ago after teaching 7th grade English for 30 years and is doing various volunteer work in Bloomington. They have one son, Sam, who currently lives in Philadelphia with his wife Lindsay and their son Teddy, and is practicing business law with a Philadelphia firm. Chris has many interests outside the academic area. They include activities with his family (especially traveling with Mary), going to cultural events at IU, power walking, and reading. And although he earns his livelihood from statistics and manage- ment science, his real passion is playing classical piano music. iv Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Contents Preface xiv PART I VBA Fundamentals 1 1 Introduction to VBA Development in Excel 3 1.1 Introduction 3 1.2 VBA in Excel 2007 and 2010 4 1.3 Example Applications 5 1.4 Decision Support Systems 7 1.5 Required Background 7 1.6 Visual Basic Versus VBA 8 1.7 Some Basic Terminology 9 1.8 Summary 9 2 The Excel Object Model 10 2.1 Introduction 10 2.2 Objects, Properties, Methods, and Events 10 2.3 Collections as Objects 11 2.4 The Hierarchy of Objects 12 2.5 Object Models in General 13 2.6 Summary 17 3 The Visual Basic Editor 18 3.1 Introduction 18 3.2 Important Features of the VBE 18 3.3 The Object Browser 22 3.4 The Immediate and Watch Windows 23 3.5 A First Program 24 3.6 Intellisense 29 3.7 Color Coding and Case 30 3.8 Finding Subs in the VBE 31 3.9 Summary 33 v Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. vi Contents 4 Recording Macros 35 4.1 Introduction 35 4.2 How to Record a Macro 35 4.3 Changes from Excel 2007 to Excel 2010 37 4.4 Recorded Macro Examples 37 4.5 Summary 47 5 Getting Started with VBA 49 5.1 Introduction 49 5.2 Subroutines 49 5.3 Declaring Variables and Constants 50 5.4 Built-in Constants 58 5.5 Input Boxes and Message Boxes 59 5.6 Message Boxes with Yes and No Buttons 61 5.7 Using Excel Functions in VBA 63 5.8 Comments 64 5.9 Indenting 65 5.10 Strings 66 5.11 Specifying Objects, Properties, and Methods 69 5.12 With Construction 73 5.13 Other Useful VBA Tips 74 5.14 Good Programming Practices 76 5.15 Debugging 78 5.16 Summary 85 6 Working with Ranges 89 6.1 Introduction 89 6.2 Exercise 89 6.3 Important Properties and Methods of Ranges 91 6.4 Referencing Ranges with VBA 94 6.5 Examples of Ranges with VBA 97 6.6 Range Names and Their Scope 111 6.7 Summary 114 7 Control Logic and Loops 117 7.1 Introduction 117 7.2 Exercise 117 7.3 If Constructions 119 7.4 Case Constructions 125 7.5 For Loops 129 7.6 For Each Loops 136 7.7 Do Loops 138 7.8 Summary 143 Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Contents vii 8 Working with Other Excel Objects 149 8.1 Introduction 149 8.2 Exercise 149 8.3 Collections and Members of Collections 151 8.4 Examples of Workbooks in VBA 153 8.5 Examples of Worksheets in VBA 157 8.6 Examples of Charts in VBA 163 8.7 Summary 173 9 Arrays 176 9.1 Introduction 176 9.2 Exercise 176 9.3 The Need for Arrays 178 9.4 Rules for Working with Arrays 179 9.5 Examples of Arrays in VBA 182 9.6 Array Functions 198 9.7 Summary 198 10 More on Variables and Subroutines 203 10.1 Introduction 203 10.2 Exercise 203 10.3 Scope of Variables and Subroutines 206 10.4 Modularizing Programs 208 10.5 Passing Arguments 212 10.6 Function Subroutines 218 10.7 The Workbook_Open Event Handler 223 10.8 Summary 225 11 User Forms 230 11.1 Introduction 230 11.2 Exercise 230 11.3 Designing User Forms 233 11.4 Setting Properties of Controls 237 11.5 Creating a User Form Template 241 11.6 Writing Event Handlers 242 11.7 Displaying a User Form 252 11.8 Looping Through the Controls on a User Form 253 11.9 Working with List Boxes 253 11.10 Modal and Modeless Forms 254 11.11 Working with Excel Controls 256 11.12 Summary 260 Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. viii Contents 12 Error Handling 267 12.1 Introduction 267 12.2 Error Handling with On Error Statement 267 12.3 Handling Inappropriate User Inputs 269 12.4 Summary 271 13 Working with Files and Folders 274 13.1 Introduction 274 13.2 Exercise 274 13.3 Dialog Boxes for File Operations 276 13.4 The FileSystemObject Object 282 13.5 A File Renaming Example 285 13.6 Working with Text Files 288 13.7 Summary 292 14 Importing Data into Excel from a Database 294 14.1 Introduction 294 14.2 Exercise 294 14.3 A Brief Introduction to Relational Databases 296 14.4 A Brief Introduction to SQL 301 14.5 ActiveX Data Objects (ADO) 305 14.6 Discussion of the Sales Orders Exercise 310 14.7 Summary 314 15 Working with Pivot Tables and Tables 316 15.1 Introduction 316 15.2 Working with Pivot Tables Manually 316 15.3 Working with Pivot Tables Using VBA 325 15.4 An Example 328 15.5 Working with Excel Tables Manually 333 15.6 Working with Excel Tables with VBA 337 15.7 Summary 340 16 Working with Ribbons, Toolbars, and Menus 343 16.1 Introduction 343 16.2 Customizing Ribbons in Excel 2010 344 16.3 Using RibbonX and XML to Customize Ribbons 345 16.4 Using RibbonX to Customize the QAT 351 16.5 CommandBar and Related Office Objects 353 16.6 A Grading Program Example 354 16.7 Summary 355 Copyright 2011 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.