Professional Documents
Culture Documents
M I C R O S O F T
E X C E L
2 0 0 3
Introduction
T A B L E O F C O N T E N T S
What is a Macro? .................................................................................................................................8 Recording a Macro................................................................................................................................8 Running a Macro..................................................................................................................................10 Relative References...........................................................................................................................10 Assigning a Macro to a Button on a Sheet ................................................................................... 11 Assigning a Macro to a Drawn Object on a Sheet......................................................................12 Assigning a Macro to a Button on a Toolbar ................................................................................12 Customising a Toolbar .......................................................................................................................12 Creating a Toolbar .............................................................................................................................13 Button Image Editor..........................................................................................................................13 Changing a Buttons ScreenTip........................................................................................................14 Exercise 1.............................................................................................................................................15 Answers to Exercise 1.......................................................................................................................16 Question 1 ............................................................................................................................................16 Question 2 ...........................................................................................................................................16 Visual Basic Editor .............................................................................................................................17 Project Explorer.................................................................................................................................17 Procedures ...........................................................................................................................................19 Sub Procedures...................................................................................................................................19 Inserting Comments ..........................................................................................................................19 Printing a Visual Basic Module ........................................................................................................ 20 Exercise 2 ............................................................................................................................................21 Answers to Exercise 2 ..................................................................................................................... 22 Question 1 ........................................................................................................................................... 22 Introduction ....................................................................................................................................... 23 Cells Method....................................................................................................................................... 23 Range Method..................................................................................................................................... 23 Selecting Range Objects ................................................................................................................. 23 Flexible Ranges .................................................................................................................................. 23 Offset Method .................................................................................................................................. 23 Resize Method ................................................................................................................................... 25 Selection, ActiveCell and Activate Methods .............................................................................. 25 Row and Column Properties ............................................................................................................. 25 Rows and Columns Methods............................................................................................................. 25 Address Method................................................................................................................................ 25 End Method......................................................................................................................................... 25 Combining Methods ........................................................................................................................... 26 Virtual Worksheets .......................................................................................................................... 27 Range Contents .................................................................................................................................. 27 Value Property ................................................................................................................................... 28 Formula Property............................................................................................................................... 29 FormulaR1C1 Property ...................................................................................................................... 29
MTC Training Solutions 21/04/2009
Page 3
E X C E L 2 0 0 3 V B A I N T R O D U C T I O N
Techniques for Inserting Values in the Next Blank Row ......................................................... 29 NumberFormat Property ................................................................................................................. 29 Exercise 3 ........................................................................................................................................... 30 Answers to Exercise 3 ......................................................................................................................31 Question 1 ............................................................................................................................................31 Question 2 ...........................................................................................................................................31 Question 3 ...........................................................................................................................................31 Question 4 ...........................................................................................................................................31 Question 5 ...........................................................................................................................................31 Activating a Workbook .................................................................................................................... 32 Activating a Worksheet................................................................................................................... 32 ThisWorkBook.................................................................................................................................... 32 Controlling Objects with their Properties .................................................................................. 34 Setting a Property Value ................................................................................................................. 34 Returning a Property Value ............................................................................................................. 34 Performing Actions with Methods ................................................................................................ 34 Using Methods ................................................................................................................................... 34 Using Named arguments .................................................................................................................. 36 Performing Multiple actions on an Object................................................................................... 37 Exercise 4 ........................................................................................................................................... 38 Answers to Exercise 4 ..................................................................................................................... 39 Question 1 ........................................................................................................................................... 39 Question 2 .......................................................................................................................................... 39 Variables...............................................................................................................................................41 Declaring Variables ............................................................................................................................41 Local, Module-Level and Public Variables..................................................................................... 42 Naming Variables............................................................................................................................... 42 Data Types.......................................................................................................................................... 42 Variant Data ....................................................................................................................................... 42 Specific Data Types ......................................................................................................................... 42 Numeric Data ..................................................................................................................................... 43 Character Data .................................................................................................................................. 43 Date and Time .................................................................................................................................... 44 Boolean................................................................................................................................................. 44 Arrays .................................................................................................................................................. 44 Objects................................................................................................................................................ 45 Constants............................................................................................................................................. 46 Exercise 5 ........................................................................................................................................... 47 Answers to Exercise 5 ..................................................................................................................... 48 Question 6 .......................................................................................................................................... 48 Conditional Control Structures ...................................................................................................... 49 IF...THEN...ELSE ............................................................................................................................... 49 SELECT CASE .................................................................................................................................... 50
http://www.mousetraining.co.uk
Page 4
M I C R O S O F T
E X C E L
2 0 0 3
Introduction
Looping Control Structures .............................................................................................................51 Do...Loop ...............................................................................................................................................51 Repeating Statements While a Condition is True.......................................................................51 Repeating Statements Until a Condition becomes True........................................................... 52 Exiting a Do...Loop Statement from inside the Loop ................................................................ 52 FOR...NEXT......................................................................................................................................... 53 FOR EACH...NEXT............................................................................................................................. 53 Non-Structured Statements.......................................................................................................... 54 GoTo ..................................................................................................................................................... 54 Sub Procedures.................................................................................................................................. 55 Calling Procedures in Another Workbook .................................................................................... 55 Passing Arguments to Subprocedures .......................................................................................... 56 Exercise 6 ........................................................................................................................................... 57 Answers to Exercise 6 ..................................................................................................................... 58 Question 1 ........................................................................................................................................... 58 Question 2 .......................................................................................................................................... 58 Question 3 .......................................................................................................................................... 59 Question 4 .......................................................................................................................................... 60 Introduction ....................................................................................................................................... 63 Built-In Dialog Boxes........................................................................................................................ 63 Predefined Dialog Boxes.................................................................................................................. 63 InputBox Function............................................................................................................................. 63 MsgBox Function ............................................................................................................................... 64 Number and type of button: ........................................................................................................... 65 Icon style: ........................................................................................................................................... 65 Default button: .................................................................................................................................. 66 User-Defined Forms......................................................................................................................... 67 Controls ............................................................................................................................................... 67 Worksheet Forms.............................................................................................................................. 67 Placing Controls.................................................................................................................................. 67 Setting the Properties of a Control.............................................................................................. 68 Cell Links ............................................................................................................................................. 68 Input Ranges....................................................................................................................................... 68 Group Boxes and Option Buttons................................................................................................... 68 Lists and Drop-Down Lists .............................................................................................................. 68 User-Defined Forms......................................................................................................................... 68 Adding Form Controls....................................................................................................................... 69 Form Control Properties .................................................................................................................. 70 Setting Form Control Properties................................................................................................... 70 Data Properties ................................................................................................................................. 70 Form Control Naming Convention................................................................................................... 70 Initialising Form Control Properties ............................................................................................. 70 Control and Dialog Box Events ....................................................................................................... 70
MTC Training Solutions 21/04/2009 Page 5
E X C E L 2 0 0 3 V B A I N T R O D U C T I O N
Displaying and Dismissing a User Form ........................................................................................ 70 Show ..................................................................................................................................................... 70 Unload ...................................................................................................................................................71 Handling Form Controls.....................................................................................................................71 Text Boxes ..........................................................................................................................................71 Option Buttons....................................................................................................................................71 List Boxes and Combo Boxes ...........................................................................................................71 Check Boxes.........................................................................................................................................71 Command Buttons ...............................................................................................................................71 Exercise 7 ........................................................................................................................................... 73 Answers to Exercise 7 ..................................................................................................................... 74 Question 1 ........................................................................................................................................... 74 The Windows Registry ..................................................................................................................... 75 Settings Files ..................................................................................................................................... 75 Declaring Functions and Subs......................................................................................................... 75 Declaring References to DLLs........................................................................................................ 75 WritePrivateProfileString .............................................................................................................. 76 GetPrivateProfileString................................................................................................................... 77 About Toolbars and Menus.............................................................................................................. 79 Attaching Toolbars to Workbooks................................................................................................ 79 Save More Than One Toolbar Configuration............................................................................... 79 Customised Toolbars ........................................................................................................................ 79 Customising a Toolbar ...................................................................................................................... 80 Display...................................................................................................................................................81 ScreenUpdating ..................................................................................................................................81 DisplayAlerts.......................................................................................................................................81 Exercise 8 ........................................................................................................................................... 82 Errors................................................................................................................................................... 83 Types of Errors ................................................................................................................................. 83 Break Mode......................................................................................................................................... 83 Setting Breakpoints.......................................................................................................................... 83 Instant Watch ................................................................................................................................... 83 Stepping .............................................................................................................................................. 84 Error Handling ................................................................................................................................... 84 On Error Goto Label ......................................................................................................................... 84 Resume and Resume Next ............................................................................................................... 84 On Error Goto 0................................................................................................................................. 84 Err Function........................................................................................................................................ 84 Error() Function................................................................................................................................. 84 Error and Err Statements .............................................................................................................. 84 Error Handling Example ................................................................................................................... 84 Worksheet Object Events .............................................................................................................. 87 Workbook Object Events................................................................................................................ 87
http://www.mousetraining.co.uk
Page 6
M I C R O S O F T
E X C E L
2 0 0 3
Introduction
Introduction ....................................................................................................................................... 89 User-Defined Functions................................................................................................................... 89 Function Wizard ................................................................................................................................ 90 Excel Functions.................................................................................................................................. 90 Exercise 9 ............................................................................................................................................91 Answers to Exercise 9 ..................................................................................................................... 92 Question 2 .......................................................................................................................................... 92 Question 3 .......................................................................................................................................... 92 Creating an Add-In ........................................................................................................................... 93 Exercise 10 ......................................................................................................................................... 93 User Form Controls........................................................................................................................... 95 Memory Variables.............................................................................................................................. 96
Page 7
E X C E L 2 0 0 3 V B A I N T R O D U C T I O N
A macro is a sequence of instructions that can be automatically executed in order to automate frequent or complicated tasks. Macros are written in a programming language called Visual Basic and can be created by recording a task or by writing the Visual Basic program or by a combination of the two. Macros can be added to menus, assigned to keys or buttons or made to run automatically.
Recording a Macro
A macro can be easily created by instructing Excel to record a series of routine actions. This is very useful for repetitive tasks.
http://www.mousetraining.co.uk
Page 8
M I C R O S O F T
E X C E L
2 0 0 3
Introduction
To record a macro:
Mouse 1. Choose Tools from the menu bar, then Macro, Record New Macro.
2. 3. 4. 5. 6.
In the text box, type a name for the macro. The macro name can contain letters, numbers and underscores; it must begin with a letter. The name cannot contain spaces or punctuation marks. Type a brief description of the macro in the default description.
Macro name
box. The To run the macro with a shortcut key type a letter in the shortcut will be assigned to [CTRL]+[letter]. Hold [SHIFT] while typing the letter in the Shortcut key box to assign the shortcut to [CTRL]+[SHIFT]+[letter]. Choose the OK button to start recording the macro.
7. 8. 9.
The Stop Recording toolbar appears on the screen, and the word Recording appears in the status bar. Perform the actions that are to be recorded. Click the Stop Recording button from the Stop Recording toolbar or choose Tools, Macro, Stop Recording, when the macro is finished.
http://www.mousetraining.co.uk
Page 9
E X C E L 2 0 0 3 V B A I N T R O D U C T I O N
Running a Macro
To run a macro:
Keyboard 1. If a shortcut key was assigned to the macro, then press the shortcut key. Or 2. 3. Choose Tools, Macro, Macros or press [ALT]+[F8]. Type the macro name or select it from the list. Choose the Run button.
Relative References
If the relative reference option is not chosen the macro records the actual cell references and will always operate on those fixed cells, regardless of which cell is active when the macro is initiated. The following macro selects the range B3 to C5:
Sub SelectAbsolute() Range("B3:C5").Select End Sub
If a macro is recorded using relative references, cell references are recorded relative to the previously active cell. The macro then operates on cells relative to the cell that is active when the macro is initiated. The following macro selects a single cell, one row beneath the starting cell:
Sub SelectRelative() ActiveCell.Offset(1, 0).Range("A1").Select End Sub
When recording a macro, click the Relative References button on the Stop Recording toolbar. By default, Excel records in absolute mode. Recording can be switched from absolute to relative, and vice versa, during the same recording process. See also The Range Object page 23.
http://www.mousetraining.co.uk
Page 10
M I C R O S O F T
E X C E L
2 0 0 3
A button can be created on a worksheet and a macro assigned to it. The macro will be available whenever the workbook is open.
http://www.mousetraining.co.uk
Page 11
E X C E L 2 0 0 3 V B A A S S I G N I N G M A C R O S
T O
O B J E C T S