1. Trang chủ
  2. » Công Nghệ Thông Tin

microsoft excel vba programming for the absolute beginner 2002

425 481 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 425
Dung lượng 8,85 MB

Nội dung

TEAMFLY Team-Fly ® Microsoft ® Excel VBA DUANE BIRNBAUM Programming 00 XLVBA FM.qxd 2/25/03 7:12 AM Page i © 2002 by Premier Press, Inc. All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, includ- ing photocopying, recording, or by any information storage or retrieval system without written permission from Premier Press, except for the inclusion of brief quotations in a review. The Premier Press logo, top edge printing, and related trade dress are trademarks of Premier Press, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. Microsoft is a registered trademark of Microsoft Corporation. Important: Premier Press cannot provide software sup- port. Please contact the appropriate software manufac- turer’s technical support line or Web site for assistance. Premier Press and the author have attempted through- out this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer. Information contained in this book has been obtained by Premier Press from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, Premier Press, or oth- ers, the Publisher does not guarantee the accuracy, ade- quacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information. Readers should be particularly aware of the fact that the Internet is an ever-changing entity. Some facts may have changed since this book went to press. ISBN: 1-931841-04-7 Library of Congress Catalog Card Number: 2001099839 Printed in the United States of America 02 03 04 05 RI 10 9 8 7 6 5 4 3 2 1 Publisher: Stacy L. Hiquet Marketing Manager: Heather Buzzingham Managing Editor: Sandy Doell Series Editor: Andy Harris Project Editor: Estelle Manticas Editorial Assistant: Margaret Bauer Technical Reviewer: Greg Perry Copy Editor: Linda Seifert Interior Layout: Shawn Morningstar Cover Design: Mike Tanamachi Indexer: Sherry Massey Proofreader: Jenny Davidson 00 XLVBA FM.qxd 2/25/03 7:12 AM Page ii pecial thanks to my family—Jill, Aaron, and Joshua. You were all won- derfully patient with me while I worked on this book. Your love and understanding are greatly appreciated. Thanks to all the people at Premier Press, especially Stacy, who gave me the opportunity to write this book, and to everyone else behind the scenes who worked to make it look good. Special thanks to Estelle for all the help, and for putting up with my numerous changes during the review process. I would also like to thank Andy Harris for recommending me to Premier, and for his guidance in the early development of the book. Thanks to Greg Perry for an outstanding technical review. I only wish I’d had time to include more of his sug- gestions; doing so would have made the book even better. Finally, thanks to all of the contributors of the software and support files on the CD and to the reader who supports them (and who, of course, also purchases this book). A c k n o wl e d gm e n t s S 00 XLVBA FM.qxd 2/25/03 7:12 AM Page iii uane Birnbaum began programming in graduate school, where he wrote custom software for interfacing the various electronic devices required for his experiments and analyzing the data obtained from them. Since completing his Ph.D. in physical chemistry, he has been working as a post-doctoral and research scientist in academia and industry while continuing to teach on a part-time basis. For the last five years he has been work- ing as a research scientist in the biotechnology industry and serving as a part- time lecturer in the Computer Science department of Indiana University/Purdue University. He teaches introductory classes in data analysis, database design, and Visual Basic. A b o u t t he A u t h o r D 00 XLVBA FM.qxd 2/25/03 7:12 AM Page iv Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x Visual Basic for Applications with Excel. . . . . . . 1 Project: Colorful Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 The VBA Integrated Development Environment (IDE) . . . . . . 3 Getting to the IDE from Excel . . . . . . . . . . . . . . . . . . . . . . 3 Components of the IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Programming Components within Excel . . . . . . . . . . . . . . . . 8 Macro Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 The Visual Basic Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Getting Help with VBA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Excel Help. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 VBA Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Installing VBA Help Files . . . . . . . . . . . . . . . . . . . . . . . . . 15 Constructing the Colorful Header Program . . . . . . . . . . . . . 15 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Beginning Programs with VBA . . 21 Project: Time of Your Life . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Variables, Data Types, and Constants. . . . . . . . . . . . . . . . . . 22 Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Simple Input and Output with VBA . . . . . . . . . . . . . . . . . . . 37 Collecting User Input with InputBox() . . . . . . . . . . . . . . . 37 Output with MsgBox() . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Manipulating Strings with VBA Functions . . . . . . . . . . . . . . 39 Fun with Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 C on t e n t s 1 CHAPTER 2 CHAPTER 00 XLVBA FM.qxd 2/25/03 7:12 AM Page v Constructing the Time of Your Life Program . . . . . . . . . . . . 44 Project Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Project Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Project Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Adding the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Procedures and Conditions . . . 51 Project: Poker Dice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 VBA Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Event Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Sub Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Function Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Logical Operators in VBA. . . . . . . . . . . . . . . . . . . . . . . . . 64 Conditionals and Branching. . . . . . . . . . . . . . . . . . . . . . . 67 Constructing the Poker Dice Program . . . . . . . . . . . . . . . . . 72 Project Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Project Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Project Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Adding the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Loops and Arrays . . . . . . . . . . . 87 Project: The Math Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Looping with VBA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Do Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Input Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Validation with the InputBox() Function . . . . . . . . . . . . . 95 Validation with a Spreadsheet Cell . . . . . . . . . . . . . . . . . 97 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . 101 Multi-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . 104 Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Programming Formulas into Worksheet Cells . . . . . . . . . . 109 A1 Style References . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 R1C1 Style References . . . . . . . . . . . . . . . . . . . . . . . . . . 111 vi Ta b l e of C o n ten t s 3 CHAPTER 4 CHAPTER 00 XLVBA FM.qxd 2/25/03 7:12 AM Page vi Chapter Project: Math Game. . . . . . . . . . . . . . . . . . . . . . . . 112 Project Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Project Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Project Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Adding the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Basic Excel Objects . . . . . . . . 133 Project: Battlecell. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 VBA and OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Objects Defined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 VBA Collection Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 The Object Browser. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Top-Level Excel Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 The Application Object. . . . . . . . . . . . . . . . . . . . . . . . . . 145 The Workbook and Window Objects . . . . . . . . . . . . . . . 145 The Worksheet Object . . . . . . . . . . . . . . . . . . . . . . . . . . 153 The Range Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Working with Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Chapter Project: Battlecell . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Project Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Project Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Project Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Adding the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Enhancing VBA Programs: Adding Multimedia and Intelligence . . . . . . . . . . . 187 Project: Enhanced Battlecell . . . . . . . . . . . . . . . . . . . . . . . . 188 Adding Multimedia to a Program . . . . . . . . . . . . . . . . . . . . 188 Animation in VBA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Sound in VBA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Simulating Intelligence in Programs. . . . . . . . . . . . . . . . . . 203 Chapter Project: Enhanced Battlecell . . . . . . . . . . . . . . . . . 204 Project Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Project Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Project Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 vii Ta b l e of C o n ten t s 5 CHAPTER 6 CHAPTER 00 XLVBA FM.qxd 2/25/03 7:12 AM Page vii Adding the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 UserForms and Additional Controls . . . . . . . . 227 Project: Blackjack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 UserForms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Adding a UserForm to a Project . . . . . . . . . . . . . . . . . . 229 Components of the UserForm Object . . . . . . . . . . . . . . 229 Adding ActiveX Controls to a UserForm . . . . . . . . . . . . 231 Showing and Hiding UserForms . . . . . . . . . . . . . . . . . . 233 Modal Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Custom Dialog Boxes with UserForms . . . . . . . . . . . . . 235 Chapter Project: Blackjack . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Project Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Project Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Project Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Adding the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Data Access, File I/O, Error Handling, and Debugging. . . . 269 Project: Word Find. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 File Input and Output (I/O) . . . . . . . . . . . . . . . . . . . . . . . . . 270 Using VBA File I/O Methods . . . . . . . . . . . . . . . . . . . . . 271 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Using the On Error Statement . . . . . . . . . . . . . . . . . . . . 283 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Break Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 The Immediate Window. . . . . . . . . . . . . . . . . . . . . . . . . 289 The Watch Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 The Locals Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Chapter Project: Word Find . . . . . . . . . . . . . . . . . . . . . . . . . 292 Project Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Project Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Project Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 Adding the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 viii Ta b l e of C o n ten t s 7 CHAPTER 8 CHAPTER 00 XLVBA FM.qxd 2/25/03 7:12 AM Page viii Excel Charts . . . . . . . . . . . . . . 313 Project: The Alienated Game . . . . . . . . . . . . . . . . . . . . . . . 314 The Chart Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 Accessing Existing Charts . . . . . . . . . . . . . . . . . . . . . . . 314 Manipulating Charts. . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Creating Charts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Chart Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Chapter Project: The Alienated Game. . . . . . . . . . . . . . . . . 332 Project Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Project Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Project Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Adding the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 VBA Shapes . . . . . . . . . . . . . . 357 Project: Excetris. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 The Shapes Collection and Shape Objects. . . . . . . . . . . . . 358 Manipulating a Shape Object . . . . . . . . . . . . . . . . . . . . 360 The ShapeRange Collection Object . . . . . . . . . . . . . . . . . . 362 Activating Shape Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 365 The OLEObjects Collection . . . . . . . . . . . . . . . . . . . . . . . . . 366 Chapter Project: Excetris . . . . . . . . . . . . . . . . . . . . . . . . . . . 368 Project Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 Project Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 Project Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 Adding the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 A Final Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396 Index. . . . . . . . . . . . . . . . . . . . 397 ix Ta b l e of C o n ten t s 9 CHAPTER 10 CHAPTER 00 XLVBA FM.qxd 2/25/03 7:12 AM Page ix [...]... interest to the VBA programmer can be found in the VBA IDE There are a few programming- related components that you access from the Excel application The components I am referring to are the Macro items found under the Tools menu, and three of the available toolbars—Visual Basic, Control Toolbox, and Forms—found in the View menu in Excel TE Microsoft Excel VBA Programming for the Absolute Beginner 8... number of tools for use in the development of your project Chapter 1 The VBA Integrated Development Environment (IDE) 01 XLVBA CH01.qxd 2/25/03 7:15 AM Page 4 Microsoft Excel VBA Programming for the Absolute Beginner 4 Alternatively, select the Visual Basic toolbar from the View/Toolbars menu item in Excel When the toolbar is displayed, select the Visual Basic Editor icon in the middle of the toolbar (see... The Menu bar 01 XLVBA CH01.qxd 2/25/03 7:15 AM Page 6 Microsoft Excel VBA Programming for the Absolute Beginner 6 (do not change any names), then open the VBA IDE Once in the IDE, display the Project Explorer and Properties windows If the Project Explorer and Properties windows are not already displayed you can access them through the View menu item (see Figure 1.5) You can also use the keystrokes Ctrl+R... taking advantage of the enhancements VBA can provide Visual Basic for Applications with Excel Before learning how to program in VBA you have to learn how to use the software required for creating your projects The VBA development software is included with each component of the Microsoft Office suite of programs, including Excel Starting the VBA development software places you in the VBA programming environment... control FIGURE 1.9 The Control toolbox Visual Basic for Applications with Excel The Macro dialog box displaying available VBA programs 01 XLVBA CH01.qxd 2/25/03 7:15 AM Page 10 Microsoft Excel VBA Programming for the Absolute Beginner 10 FIGURE 1.10 The Command Button control placed on a worksheet You can access the properties of the Command Button control while in design mode With the Command Button... Page 14 14 Microsoft Excel VBA Programming for the Absolute Beginner Excel Help If you are a regular Excel or MS Office user then chances are you’ve used the online help Whether or not you use the Office Assistant, you probably know how to get to the Help window shown in Figure 1.14 No matter what your opinion concerning that paper clip, it is absolutely critical that you get comfortable with the online... Basic for Applications with Excel ActiveX Control Chapter 1 TA B L E 1 1 S E L E C T E D P R O P E R T I E S O F ACTIVEX CONTROLS USED IN THE COLORFUL HEADER PROJECT 01 XLVBA CH01.qxd 2/25/03 7:15 AM Page 18 FIGURE 1.15 AM FL Y VBA IDE showing the code window for the worksheet containing the ActiveX controls of the Colorful Header project TE Microsoft Excel VBA Programming for the Absolute Beginner. .. using the online help in Excel To access the VBA help, you must have the IDE open and active Index tab The Keywords entry box The selected Help document The Topic list FIGURE 1.14 The Excel Help window 01 XLVBA CH01.qxd 2/25/03 7:15 AM Page 15 15 To look up documentation concerning a known keyword in VBA, (for example, the syntax requirements for a particular VBA keyword), first select that keyword in the. .. bar The object list FIGURE 1.12 The VBA IDE showing the code window for the worksheet named Sheet1 The procedure list The editor Visual Basic for Applications with Excel The Name property is an important property of any ActiveX control The value of the Name property should be changed to something meaningful as soon as the control is added to the worksheet Typically, an abbreviated word telling us the. .. Adding the Code As I stated in the algorithm, all of the code is to be placed in the Click() event procedure of the Command Button control The code window can be accessed via the VBA IDE by double-clicking on the Command Button control while in design mode You can also select the appropriate object (cmdInsertHeading) from the object drop-down list in the code window for the worksheet on which the ActiveX . of the enhancements VBA can provide. FIGURE 1.2 Accessing the VBA IDE from the Tools menu in Excel 01 XLVBA CH01.qxd 2/25/03 7:15 AM Page 3 Alternatively, select the Visual Basic toolbar from the. programs that did not allow for much user interaction because the programmers dictated the flow of the program. In event-driven programming, the user dictates the flow of the program, and it is up. software required for creating your projects. The VBA development software is included with each component of the Microsoft Office suite of programs, including Excel. Starting the VBA development

Ngày đăng: 17/04/2014, 09:17

TỪ KHÓA LIÊN QUAN