Académique Documents
Professionnel Documents
Culture Documents
Excel Macros
Dave Goody
Course Rules
What is a Macro?
• A Macro is a piece of code that enables the same
processing or key-strokes to be repeated
Running Macros
• As well as running Macros as shown before, they can
also be run in various other ways
• From a Button
• Select View, Toolbars, Forms
Excel Macros 8
Running Macros
• Click on the Button icon and then click on the workbook
to place the button
• Select “Filter” and click OK
Running Macros
• From a Drawing
• Select View, Toolbars, Drawing (if necessary)
• Place any drawing on the workbook
• Right-click the drawing, then choose Assign Macro
Excel Macros 9
Running Macros
• Select “Filter” and click OK
• You can now run the Macro by clicking on the drawing
The VB Editor
• To access the VB editor, select Tools, Macro, Macros,
then select a Macro and click “Edit”
Excel Macros 10
The VB Editor
• To access the VB editor, select Tools, Macro, Macros,
then select a Macro and click “Edit”
The VB Editor
• Comments start with an apostrophe (‘)
• Keywords are in blue
• Code is in black
• Errors are in red
Excel Macros 11
The VB Editor
• Objects, Properties and Methods
The VB Editor
• Applying a Method to an Object is done as follows:-
• Object.Method
• e.g. Wipe the table is done by: Table.Wipe
The VB Editor
• Objects can have objects within them; this is known as a
collection
• e.g. Table.leg.height = 5
• where Leg is an Object within the Table Object
• or even Table.leg(1).height = 5
• where this is leg 1 of the Table Object
The VB Editor
• Create a Macro to turn the spreadsheet Gridlines on and
off (remember how to record a Macro?)
• This is done by selecting Tools, Options, View,
Gridlines
Excel Macros 13
The VB Editor
• Once you have created your Macro and checked that it
works, have a look at the code (Tools, Macro, Macros,
select the Macro and click “Edit”)
The VB Editor
• Unfortunately, you may notice that our Macro only turns
the Gridlines off, but not back on again
• To do this, we will need to add an “if” condition and test
the current value of the Property
The VB Editor
• We are now going to create a Macro that enters values
into a cell
• Record a Macro named “Name”, click on cell A13 and
enter your name
• Stop recording and have a look at the code
The VB Editor
• To make this Macro work anywhere, remove the line
• Range(“A13”).Select
• which positions the Active Cell
The VB Editor
• Enter the code as follows:-
• This sets the font name and size, but saves doing
• Selection.font.name = “letter gothic 12”
• Selection.font.size = 15.5
• Try running your new Macros
The VB Editor
• A Macro can also be made to prompt the User for input
• From within the VB Editor, create a new Macro
“InputName” with the following code:-
• Now, click on the cell one below the current active cell,
then click the “Relative Referencing” button again,
followed by “Stop”
• Now have a look at the Macro code in the VB Editor
Excel Macros 17
• This finds the first free cell in the current column, then
prompts for a name to be entered
Error Handling
• We should ensure that if an error occurs when running a
macro, we handle it rather than letting the macro crash
and grind to a halt
• On your “Filter” macro, enter a new first line
• On error goto MyError
• MyError:
• MSGBox(“Pick a cell!”)
Excel Macros 19
Error Handling
• Try selecting a range of cells outside the data entered,
and run the “Filter” macro
• Rather than getting an error message, you should see
the message you have specified
Functions
• The VB Editor also gives you the opportunity to write
your own functions
• For instance, you could write one that converts
Centigrade to Fahrenheit, or perhaps to do currency
conversion
Functions
• To use your new function, enter a value into a cell and
then enter your function into another
Excel Macros 22
Functions
• Functions can have more than one variable passed to
them
• Try creating another function called Exchange
Functions
• You run this in the same way as the Fahrenheit function,
but you pass it two variables
Excel Macros 23
The End