SANNE WØHLK VBA PROGRAMMING IN BUSINESS ECONOMICS DJØF Publishing Copenhagen
To my children Louise, Kristoffer, Sebastian, and Mikkel I hope that you will always appreciate the joy of learning
Sanne Wøhlk VBA Programming in Business Economics DJØF Publishing Copenhagen 2010
Sanne Wøhlk VBA Programming in Business Economics First Edition, Second Print 2011 2010 by DJØF Publishing Copenhagen DJØF Publishing is a company of the Association of Danish Lawyers and Economists All rights reserved. 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 or otherwise without the prior written permission of the Publisher. Print: Toptryk Grafisk, Gråsten Printed in Denmark 2011 ISBN 978 87 574 2267 2 Sold and distributed in North America by: International Specialized Book Services (ISBS) Portland, OR 97213, USA www.isbs.com Sold and distributed in Scandinavia by: DJØF Publishing Copenhagen Denmark www.djoef forlag.dk Sold and distributed in all other countries by: The Oxford Publicity Partnership Ltd. Towcester NN12 6BT, UK www. oppuk.co.uk DJØF Publishing 17, Lyngbyvej P.O.Box 2702 DK 2100 Copenhagen Denmark Phone: +45 39 13 55 00 Fax: +45 39 13 55 55 E mail: forlag@djoef.dk www.djoef forlag.dk
5 Table of Contents Introduction... 11 Chapter 1 VBA and Your First Program... 13 1.1 What is VBA... 13 1.2 The Developer Tab... 14 1.3 The VBA Editor... 15 1.4 Insert a Module... 17 1.5 Write Your First Macro... 18 1.6 Execute your First Macro... 18 1.7 Save a Workbook that Contains Macros... 21 1.8 Exercises... 21 Chapter 2 The Macro Recorder... 23 2.1 How to Record a Macro... 23 2.2 Viewing the Recorded Code... 26 2.3 Using Relative References... 29 2.4 Limitations of the Macro Recorder... 30 2.5 Exercises... 31 Chapter 3 Try it Out... 33 3.1 Getting Started... 33 3.2 Variables... 35 3.3 Accessing the Worksheet... 35 3.4 Input and Output... 36 3.4.1 Message Box... 37 3.4.2 Input Box... 37 3.5 Using Excel and VBA Functions... 38 3.6 Making Decisions... 40 3.7 Looping... 42 3.8 Good Programming Practice... 43 3.8.1 Structure Your Code... 43 3.8.2 Naming Convention... 44 3.8.3 Commenting/Documenting... 44 3.8.4 Indention... 44 3.9 Putting it all Together... 45 3.9.1 Example: Delayed Products... 45 3.10 Exercises... 47
6 Chapter 4 Variables... 49 4.1 Types of Variables... 49 4.2 Declaring Variables... 50 4.3 Scope of Variables... 52 4.3.1 Local Variables... 52 4.3.2 Module and Global Variables... 52 4.3.3 Who is in Charge?... 53 4.4 Arrays... 54 4.4.1 Dynamic Arrays... 56 4.4.2 Multi Dimensional Arrays... 57 4.5 Exercises... 58 Chapter 5 Subs and Functions... 61 5.1 Custom Procedures... 61 5.1.1 Subs... 61 5.1.2 Functions... 63 5.1.3 Using Arguments in Procedures... 65 5.1.4 Public vs Private Procedures... 68 5.2 VBA Functions... 69 5.2.1 Number and Math Functions... 70 5.2.2 Date and Time Functions... 71 5.2.3 Financial Functions... 74 5.2.4 Other Functions... 76 5.3 Worksheet Functions... 78 5.3.1 Using Worksheet Functions in Your Code... 79 5.3.2 Examples of Worksheet Functions... 81 5.4 Exercises... 84 Chapter 6 Decisions and Looping... 87 6.1 Arithmetic and Comparison... 87 6.1.1 Arithmetic... 87 6.1.2 Comparison... 88 6.2 Decisions... 89 6.2.1 If Then Else... 90 6.2.2 Select Case... 92 6.3 Looping... 94 6.3.1 The For Next Loop... 94 6.3.2 The While Loop... 100 6.3.3 The Do Loops... 106 6.3.4 Be Aware of Endless Looping... 110 6.4 Nested Loops... 111 6.4.1 Example: Calculate Distances... 113 6.4.2 Example: Gantt Chart... 116 6.4.3 Example: Sequencing of Jobs... 119
7 6.5 Jumping... 121 6.5.1 GoTo... 121 6.5.2 Exit... 123 6.6 Exercises... 124 Chapter 7 Workbooks, Worksheets, and Ranges... 127 7.1 The Object Model... 127 7.1.1 Referring to an Object... 128 7.1.2 Properties and Methods... 129 7.1.3 Collections... 129 7.1.4 Object Variables... 130 7.1.5 The Object Browser... 130 7.2 Ranges... 131 7.2.1 Size of the Region... 133 7.2.2 Sorting... 134 7.2.3 Other Range Properties and Methods... 136 7. 3 Worksheets... 136 7. 4 Workbooks... 137 7. 5 The Application Object... 138 7. 6 For Each Loops... 140 7. 7 With Statements... 142 7. 8 Exercises... 143 Chapter 8 Security... 145 8. 1 Set Macro Security... 145 8. 2 Trusted Sources... 147 8.2. 1 Trusted Locations... 147 8.2. 2 Trusted Digital Certificates... 149 8. 3 Create a Digital Certificate... 150 8.3. 1 Sign Your Code with Your Digital Certificate... 151 8. 4 Protecting Your Work... 152 8.4. 1 Prevent Others from View or Changing Your Code... 153 8.4. 2 Prevent Others from Executing Your Program... 153 Chapter 9 Debugging... 157 9. 1 Avoid the Need for Debugging... 157 9. 2 VBA Debugging Tools... 158 9.2. 1 Step Into or Single Stepping... 159 9.2. 2 Step Over... 159 9.2. 3 Step Out... 159 9.2. 4 Break Point... 160 9.2. 5 Break Mode... 160 9.2. 6 Locals... 161 9.2. 7 Watch... 161 9.2. 8 The Immediate Window... 161
8 9. 3 Run Time Errors... 162 9. 4 Exercises... 164 Chapter 10 Design of Programs... 165 10. 1 The Process of Creating a Program... 165 10. 2 Designing a Program... 168 10.2. 1 Example: Simple Production Planning... 168 10.2. 2 Example: Reservation Problem... 170 10. 3 Use of Flow Charts in Designs... 173 Chapter 11 Making it Easier to Use Your Code... 175 11. 1 The Personal Workbook... 175 11. 2 Executing a Macro... 176 11.2. 1 Use Alternative Buttons to Execute a Macro... 177 11.2. 2 Use a Shortcut Key to Execute a Macro... 177 11. 3 Quick Access Toolbar... 178 11. 4 Toolbar in the Ribbon... 179 11. 5 Create and Use Your Own Add In... 180 11.5. 1 Preparing the Workbook... 181 11.5. 2 Creating the Add In... 182 11.5. 3 Installing an Add In... 183 11. 6 Exercises... 185 Chapter 12 Files... 187 12. 1 The File System... 187 12. 2 Working with Folders... 188 12.2. 1 Create a New Folder... 188 12.2. 2 Delete a Folder... 189 12.2. 3 Display Sub Folders... 189 12. 3 Working with Files... 190 12.3. 1 Get Information about Files... 190 12.3. 2 Check if a File Exists... 191 12.3. 3 Check if a Workbook is Open... 192 12.3. 4 Count Workbooks in Folder... 193 12. 4 Working with Text Files... 193 12. 5 Exercises... 197 Chapter 13 Strings... 199 13. 1 Working with Strings... 199 13. 2 String Functions... 201 13. 3 Exercises... 203
9 Chapter 14 Charts... 205 14. 1 Chart Sheets and Embedded Charts... 205 14. 2 Basic Chart Operations... 208 14.2. 1 Check if a Chart Exists... 208 14.2. 2 Delete Charts... 209 14.2. 3 Display Names of Charts... 210 14. 3 Charts in Action... 210 14.3. 1 Example: Sales... 210 14.3. 2 Example: Advertisement... 215 14.3. 3 Example: Members... 219 14.3. 4 Example: Sales Revisited... 222 14.3. 5 Example: The Traveling Salesman Problem... 223 14.3. 6 Example: The Vehicle Routing Problem... 226 14. 4 Exercises... 229 Chapter 15 Built in Dialog Boxes... 231 15. 1 Message Boxes... 231 15. 2 Input Boxes... 234 15. 3 The Application Input Box... 236 15. 4 Other Built in Dialog Boxes... 238 Chapter 16 User Forms... 239 16. 1 Controls... 239 16. 2 Event Programming... 242 16. 3 Designing the First User Form... 244 16.3. 1 Insert the User Form... 245 16.3. 2 Changing the Appearance of the User Form... 246 16.3. 3 Show the User Form... 247 16.3. 4 Adding Controls to the User Form... 248 16.3. 5 Properties... 249 16.3. 6 Tab Order... 250 16.3. 7 Short Cut Keys... 251 16.3. 8 Automating the User Form... 251 16. 4 User Forms in Action... 253 16.4. 1 Example: Selecting Flights... 253 16.4. 2 Example: Select Background Picture... 257 16.4. 3 Example: People Information Revisited... 260 16.4. 4 Example: Running Experiment... 262 16. 5 Exercises... 267 Chapter 17 Solver... 271 17. 1 Introduction to Solver... 271 17.1. 1 Preparing the Worksheet... 271 17.1. 2 Using the Solver... 273 17. 2 Preparing the File for Combining Solver and VBA... 278
10 17. 3 Using Solver from the VBA Code... 280 17.3. 1 A Small Example... 280 17.3. 2 A Walk Through the Functions... 282 17. 4 Solver in Action... 289 17.4. 1 Example: Adding Additional Constraints... 289 17.4. 2 Example: Transportation Problem... 290 17.4. 3 Example: Solving Problems of Flexible Size.... 293 17. 5 Exercises... 295 Chapter 18 Simulation... 299 18. 1 Generation of Random Numbers... 299 18. 2 Use Simulation to Compare Strategies... 301 18.2. 1 Example: Production Planning... 302 18.2. 2 Example: Inventory Control... 306 18. 3 Introduction to Crystal Ball... 309 18.3. 1 Example: Investment... 311 18.3. 2 Example: Sales... 316 18. 4 Using Crystal Ball from the VBA Code... 320 18.4. 1 Example: Sales Revisited... 320 18.4. 2 Example: Sales Extended Version... 322 18. 5 Exercises... 325 Index... 329