Uploaded by thomas

VBA PROGRAMMING IN BUSINESS ECONOMICS

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