Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Automation
Through
Macros
MODU LE
Microsoft Excel 2007 - Automation Through Macros For The Haas School, UC Berkeley Dash Designs Consulting
Microsoft Excel 2007 - Automation Through Macros For The Haas School, UC Berkeley Dash Designs Consulting
Excel 2007
Automation Through Macros
Table of Contents
Planning and Recording Macros ... 4
Making Macros Flexible .............. 16
Reference Workbook: Macros Workbook.xlsx
Microsoft Excel 2007 - Automation Through Macros For The Haas School, UC Berkeley Dash Designs Consulting
CHAPTER
Automation
Through
Macros
Jerry Maletsky
Dash Designs Consulting
Technology Training and Consulting
CHAPTER
Save time
CHAPTER
CHAPTER
1
SETTING MACRO SECURITY
Microsoft Office takes precautions before opening files that contain macros. That is because viruses can be contained within macros. Antivirus software that is installed with
Microsoft Windows, and kept updated, scans files for known viruses before they are
opened. In addition, Microsoft Office contains Macro Setting Security options that help
protect files with macros from being opened inadvertently without the users knowledge
that they contain macros.
Within Microsoft Excels Trust Center command are the Macro Settings options. This
settings dialog box has several options that range from disabling macros automatically before a file can be opened to opening any file regardless whether or not they contain macros.
Security settings can be viewed in Microsoft Excel through the, OFFICE BUTTON,
EXCEL OPTIONS BUTTON, TRUST CENTER, MACRO SETTINGS command.
Steps:
Click Microsoft Office Button
Click the Excel Options button
Click the Trust Center category (left panel)
Click Trust Center Settings... button (right panel)
Click Macro Settings category (left panel)
CHAPTER
CHAPTER
1
RECORDING A MACRO
Macros are recorded much like recording a television show. After the recorder is activated
it memorizes every step taken to complete the required tasks.
Before recording a macro, it is a very good idea to practice and try out the steps needed to
be recorded. That way, it is less likely that steps will be missed as well as ensuring that
the steps all work.
In order to record a macro, the user needs to open the Record Macro dialog box. There
will be several options necessary to set before the macro recording begins. These include
the macro name, a shortcut key to play it back, and the location in which to store the
macro.
The following defines the options available
MACRO NAME:
The first character of the macro name must be a letter. Other characters can be
letters, numbers, or underscore characters.
Spaces are not allowed in a macro name; an underscore character works well as
a word separator.
Do not use a macro name that is also a cell reference or you can get an error
message that the macro name is not valid.
The shortcut key letter you use cannot be a number or special character such as
@ or #.
The macro can also be executed from the Macro dialog box, a toolbar button, or
even a menu command.
This Workbook ( macro can only be run in the workbook it is being recorded in
or when that workbook is opened)
DESCRIPTION: (Optional)
Enter a summary description that will help recall what tasks the macro performs.
Microsoft Excel 2007 - Automation Through Macros - Dash Designs Consulting
CHAPTER
RECORDING A MACRO
View Tab: Macros: Record New Macro Dialog Box
Steps:
View Tab: Macro: Record Macro command
Type a name for the macro
(no spaces use an underscore if necessary)
Give it a shortcut key (optional) Use Shift key + Letter so you dont
overwrite the regular command shortcut keys
Note: Once you click OK in the Record Macro dialog box the macro begins
recording every step you perform. Dont worry about small mistakes. The
macro plays back so quickly that you probably wont even notice them. The
macro can be edited afterwards (VBA) or re-recorded.
Microsoft Excel 2007 - Automation Through Macros - Dash Designs Consulting
CHAPTER
1
RECORDING ABSOLUTELY
OR
RELATIVELY
When you create a macro Microsoft Excel records those steps either in
Absolute Reference mode or Relative Reference mode. The mode in which
the macro is recorded may depend on the tasks and the steps that need to be executed in order to successfully execute.
When a macro is recorded in Absolute Reference mode any cell you select or
navigate to will be recorded as just that cell address (i.e. selecting cell B10 will be
recorded as selecting cell B10).
When a macro is recorded in Relative Reference mode any cell you select or navigate to will be recorded as that cell address relative to the cell previously selected.
For example, if the active cell is A5 and the next cell selected is B10, in Relative
Reference mode Excel will record that as moving to the cell 1 column to the right
and 5 rows down.
Some macros will execute more efficiently in Absolute Reference mode while others will be more effective in Relative Reference mode.
Excel will continue to record macros with relative references until you quit Excel or
until you click Relative Reference again, so that it is not selected.
RECORDING ABSOLUTELY
OR
CHAPTER
RELATIVELY
Steps:
After clicking OK to start the recorder...
Set the recording mode to relative or absolute
Perform the necessary tasks
Click the Stop Recording command when completed
CHAPTER
EXECUTING A MACRO
Once a macro has been recorded, it can be played back when needed. There are
several methods that can be used to execute the macro. It can be played back using the Macros dialog box, pressing the shortcut key combination (i.e.
Ctrl+Shift+H), or clicking on a toolbar button or menu command that it has been
assigned.
10
CHAPTER
11
CHAPTER
1
EDITING VBA CODE
Occasionally after recording a macro, you might find that some steps were omitted,
some labels were mistyped, some formatting options need to changed. Although
the macro can be re-recorded, it might be more efficient to just edit the necessary
changes in the VBA code. You do not have to be an expert at VBA in order to do
this. Many of the recorded steps are reflective of the task that you might have performed with your mouse.
In addition, omitted steps can be recorded separately and then copied and pasted
into the original macro code.
Steps:
Click View Tab: Macros button: View Macros command
In the Macros dialog box, select the preferred macro
Click Edit button
Edit the macro as needed
Close the VBA window
When the workbook is saved the macro will be updated
12
CHAPTER
Personal.xlsb workbook
13
CHAPTER
1
ADDING MORE COMMANDS TO A MACRO
Sometimes when a macro is recorded and reviewed, it might be noticed that some
tasks were omitted. Instead of re-recording the entire macro, a new macro can be
recorded with just those steps and then added to the original macro through Copy
and Paste commands.
Steps:
After recording the new macro with the additional steps...
14
CHAPTER
Original
Macro
Original
Macro and
New Macro
Original
Macro With
Added Com-
15
CHAPTER
Argument
16
Required/Optional
Prompt
Required!
Title
Optional
Default
Optional
Xpos
Optional
Ypos
Optional
Helpfile
Optional
Context
Required
(if Helpfile argument is
used)
Description
Text that will appear as the message in
the Input box (up to 1,024 characters).
Must be entered in double-quotes.
Text that will appear in the Title Bar of
the Input box dialog. Must be entered
in double-quotes.
Specifies entry to be entered by default.
Must be entered in double-quotes.
Specifies the horizontal position from the
left edge of the Input box to the left
edge of the screen.
Specifies the vertical position from the
top edge of the Input box to the top
edge of the screen
Specifies the help file (if created) to display if a Help button (added to the Input
box automatically)
Specifies help context number assigned
by author to the help file.
CHAPTER
Before:
Sub OpenWkbk()
'
' Chicago Macro
' Macro recorded 11/1/2007 by Jerry Maletsky Opens the Macros Workbook
'
' Keyboard Shortcut: Ctrl+Shift+O
'
Workbooks.Open Filename:= _
"C:\Documents and Settings\Jerry Maletsky\My
Documents\Macros.xlsx"
End Sub
After
Sub OpenWkbk()
'
' OpenWkbk Macro
' Macro recorded 7/13/2005 by Jerry Maletsky Opens the Specified Workbook
'
' Keyboard Shortcut: Ctrl+Shift+O
'
OpenBk=InputBox(Enter the name of the workbook to open,Open Workbook)
Workbooks.Open Filename:= (OpenBk)
End Sub
17
CHAPTER
2
USING MESSAGE BOXES IN MACROS
Message boxes can display information to the user as the macro is being executed.
For example, a message box can let the user know what task is about to happen or
ask the user for permission to proceed.
If the Message box contains multiple options (see Buttons argument below), additional VB code is required to process the buttons. One method would be an IF
ThenElse statement (see topic further on).
The syntax for the Message box consists of the following:
Name=MsgBox(prompt,buttons,title,helpfile,context)
Argument
18
Required/Optional
Prompt
Required!
Buttons
Optional
Description
Text that will appear as the message in
the Message box (up to 1,024 characters). Must be entered in doublequotes.
Specifies the number of buttons and the
type of buttons to display. If not specified an OK button will display by default.
Title
Optional
Helpfile
Optional
Context
Required
(if Helpfile argument is used)
Value
Button Type(s)
0
OK
1
OK, Cancel
2
Abort, Retry, Ignore
3
Yes, No, Cancel
4
Yes, No
5
Retry, Cancel
Text that will appear in the Title Bar of
the Input box dialog. Must be entered
in double-quotes.
Specifies the help file (if created) to display if a Help button (added to the Message box automatically)
Specifies help context number assigned
by author to the help file.
CHAPTER
Sub OpenWkbk()
'
' OpenWkbk Macro
' Macro recorded 11/1/2007 by Jerry Maletsky Opens the Specified Workbook
'
' Keyboard Shortcut: Ctrl+Shift+O
'
Info = MsgBox("You are about to open a workbook")
OpenBk=InputBox(Enter the name of the workbook to open,Open Workbook)
Workbooks.Open Filename:= (OpenBk)
End Sub
19
CHAPTER
2
USING IFTHENELSE STATEMENTS IN MACROS
IFThenElse Statements can help process options set by the user such as in a message box with multiple buttons.
The syntax for the IFThenElse Statement consists of the following:
If condition Then
Statements
Else
Else Statements
End If
When there are more than 2 options available the ElseIf argument can be used:
If condition Then
Statements
ElseIf
ElseIf Statements
Else
Else Statements
End If
Argument
20
Required/Optional
Description
Condition
Required!
Statements
Required!
Else Statements
Required!
CHAPTER
21
CHAPTER
2
USING IFTHENELSE STATEMENTS IN MACROS
22
CHAPTER
Navigation
Direction
Ctrl+Home
Ctrl+End
Ctrl+
Ctrl+
Ctrl+
Ctrl+
Ctrl+End
Ctrl+
Ctrl+
Ctrl+
Ctrl+
23
CHAPTER
2
ASSIGNING A MACRO TO A TOOLBAR OR MENU
To make macros more easily accessible, they can be assigned to toolbars or menus.
In addition, they can be assigned to graphic images (buttons, pictures) placed on the
worksheet.
Generally macros assigned to toolbars or menus are stored in the Personal Macro
Workbook, while macros assigned to buttons or images on a worksheet are generally stored in the This Workbook option.
Steps:
Click Quick Access Toolbar Customize Button
Click The More Commands Option
From Choose Commands From list (top left), Choose Macros
Select the Macro from the displayed list
To Change Button Image and Name, click Modify button
Click OK
24
CHAPTER
25
CHAPTER
2
ASSIGNING A MACRO TO A WORKSHEET BUTTON OR IMAGE
Steps:
Assigning a Macro To A Worksheet Button
26
CHAPTER
27