Recording a Macro
If you are not sure whether you have a personal.xls,
you can let Excel record a macro in your personal.xls. If you don't have one
then Excel will create a personal.xls workbook for you in the XLSTART directory
which is in the same directory as you are running Excel.exe from.
- From menu bar: Tools -> Macro -> Record New
Macro
- In the middle drop down choose "record to
personal Macro workbook" which is your personal.xls, or if you just want
macro available in the current workbook choose "This Workbook".
- Supply a name, or accept the suggested name
as "Macro1", then start the tasks or steps you want to record
- Click on the stop button on the floating
macro dialog box that is over your sheet, or use
Tools-> Macro-> Stop Recording
- Alt+F11, to get to the Visual
Basic Editor
- Ctrl+R, to bring up the Project
Window
- F7, brings up the Code
- Ctrl+G, brings up the Immediate
window
- you usually would see the Project window
(left), Code window (right), Immediate window (bottom) at the same time.
- under VBAProject (personal.xls) or
the name of your current workbook if you chose "This workbook" earlier -
select Modules, then the highest numbered module, double click on the
Module name (i.e. Module1)
- You should see the macro you just recorded
in the code window [see sample]
Use ready pre-written Macros
Instead of recording your own macro, install a macro
from a newsgroup, or from a web page. Bring up VBE, within the Project
Explorer select your project, which is your workbook name. You will install
the provided macro in your workbook, or if you want the macro to always be
accessible you would install in your personal.xls workbook.
If you do not see modules in your
VBAProject (personal.xls) or another project workbook which you've
selected, then invoke the menu Insert ->Module [see
sample] or
right-click on your VBAProject (personal.xls) in the Project Explorer [see
sample]. The first
module created in a project would be module1. You can
rename it. You can put several macros in a module or create a new module for
some addition macros.
Double-click on the selected module name. Copy
code (Ctrl+C) from your source then paste (Ctrl+V) the code into a standard
module that you just picked. Next, run your macro but keep in mind when you
use a macro, you can not undo with Ctrl+Z the result that is done.
Run your Macro
Press Alt+F8, select the macro
you installed and click the [Run]
button.
Why my macro does not run?
- Macros will not run if the security setting
is set to High, Check under Tools -> Macro -> Security, set to Medium.
If the security setting is set a High and you try to run macros in the
workbook you will see the following message:
"The macros in the project are disabled. Please refer to the
online help or documentation of the host application to determine how to
enable macros".
- Syntax errors will show up in
Red and the macro will not run. This would be
a frequent occurrence for macros copied from newsgroups where a statement
got split between two lines and needs to be rejoined. Fix any syntax error.
- In the VBE (Alt+F11)
the Run Sub/UserForm, Break, and Reset buttons must all
be dark blue (not grayed out). If the middle button is grayed out, you are
in break mode from
macro failure or a chosen break point (debugging) -- fix your macro errors
and then press the Run button to continue, or the Reset button to allow
starting over.
- If the macro doesn't run at all, perhaps it
is installed as the wrong type of macro. Standard macros are
installed in Modules. Class macros such as
Event macros are installed in Sheets, or in ThisWorkbook class module
for Workbook Events.