About Macros: Functions
About Macros: Functions
If you perform a task repeatedly in Microsoft Excel, you can automate the task with a
macro. A macro is a series of commands and functions that are stored in a Microsoft
Visual Basic module and can be run whenever you need to perform the task.
For example, if you often enter long text strings in cells, you can create a macro to
format those cells so that the text wraps.
Recording macros When you record a macro, Excel stores information about each
step you take as you perform a series of commands. You then run the macro to repeat,
or "play back," the commands. If you make a mistake when you record the macro,
corrections you make are also recorded. Visual Basic stores each macro in a new
module attached to a workbook.
Making a macro easy to run You can run a macro by choosing it from a list in the
Macro dialog box. To make a macro run whenever you click a particular button or
press a particular key combination, you can assign the macro to a toolbar button, a
keyboard shortcut, or a graphic object on a worksheet.
Managing your macros After you record a macro, you can view the macro code
with the Visual Basic Editor to correct errors or change what the macro does. For
example, if you wanted the text-wrapping macro to also make the text bold, you could
record another macro to make a cell bold and then copy the instructions from that
macro to the text-wrapping macro.
The Visual Basic Editor is a program designed to make writing and editing macro
code easy for beginners, and provides plenty of online Help. You don't have to learn
how to program or use the Visual Basic language to make simple changes to your
macros. With the Visual Basic Editor, you can edit macros, copy macros from one
module to another, copy macros between different workbooks, rename the modules
that store the macros, or rename the macros.
Macro security Excel provides safeguards that help protect against viruses that can
be transmitted by macros. If you share macros with others, you can certify them with
a digital signature so that other users can verify that they are from a trustworthy
source. Whenever you open a workbook that contains macros, you can verify their
source before you enable them.
Create a macro
Record a macro
How?
1.On the Tools menu, click Options.
2.Click the Security tab.
3.Under Macro Security, click Macro Security.
4.Click the Security Level tab, and then select the security level you
want to use.
2. On the Tools menu, point to Macro, and then click Record New Macro.
3. In the Macro name box, enter a name for the macro.
Notes
o 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.
o 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.
2. If you want to run the macro by pressing a keyboard shortcut key, enter a letter
in the Shortcut key box. You can use CTRL+ letter (for lowercase letters) or
CTRL+SHIFT+ letter (for uppercase letters), where letter is any letter key on
the keyboard. The shortcut key letter you use cannot be a number or special
character such as @ or #.
Note The shortcut key will override any equivalent default Microsoft Excel
shortcut keys while the workbook that contains the macro is open.
3. In the Store macro in box, click the location where you want to store the
macro.
If you want a macro to be available whenever you use Excel, select Personal
Macro Workbook.
1. On the Tools menu in Microsoft Excel, point to Macro, and then click Visual
Basic Editor.
2. On the Insert menu, click Module.
3. Type or copy your code into the code window of the module.
4. If you want to run the macro from the module window, press F5.
5. When you're finished writing your macro, click Close and Return to
Microsoft Excel on the File menu.
Automatic macros, such as Auto_Activate, are designed to run when you start
Microsoft Excel. For more information about these macros, see Visual Basic Help.
How?
To copy the entire macro, make sure to include the Sub and End Sub lines in
the selection.
9. Click Paste .
Tip
You can view your Personal Macro Workbook file (Personal.xls) at any time by
opening it in the Visual Basic Editor (Alt+F11). Because Personal.xls is a hidden
workbook that is always open, you must unhide it if you want to copy a macro.
Run a macro
Run a macro
How?
o Click Run.
1. Click Edit.
Tip
If you want to run a different macro while you are in the Visual Basic Editor,
click Macros on the Tools menu. In the Macro name box, enter the name of
the macro you want to run, and then click Run.
Note If you assign a macro to a button or other object that is already in use as a
hyperlink, the hyperlink information is deleted. From then on, clicking the button or
object runs the macro instead.
Edit a macro
Before you edit a macro, you should be familiar with the Visual Basic Editor. The
Visual Basic Editor can be used to write and edit macros attached to Microsoft Excel
workbooks.
How?
Delete a macro
1. Open the workbook that contains the macro you want to delete.
2. On the Tools menu, point to Macro, and then click Macros.
3. In the Macros in list, click This Workbook.
4. In the Macro name box, click the name of the macro that you want to delete.
5. Click Delete.
Stop a macro
Do one of the following:
• If you want to stop a macro that's currently running, press ESC, and click End
in the Microsoft Visual Basic dialog box.
• If you want to prevent automatically running a macro when you start
Microsoft Excel, hold down SHIFT during startup
How?
Troubleshoot macros
When I click the Refresh button, a message tells me that my macro changes will
be lost.
This message appears when you have made changes to a macro in the Visual Basic
Editor and have also changed the copy of your workbook in the Microsoft Script
Editor.
1. Click No.
2. Switch to the Visual Basic Editor window.
3. Export any modules you have changed.
4. In your workbook in Microsoft Script Editor, click Refresh on the
Refresh toolbar.
5. Click Yes.
6. Import the modules to restore the changes to your macro.
To undo an unwanted action in a macro, you can open the macro in the Visual Basic
Editor and remove any unwanted steps.
If you don't want to use the Visual Basic Editor, record the macro again without the
unwanted action.
A macro you record may not run properly in every situation. If the macro cannot run,
Microsoft Excel displays an error message. Some macros depend on certain options or
settings in Excel. For example, a macro that searches for bold text won't run properly
if bold text isn't displayed. If a macro you've recorded produces an error message,
note the error number. You can then search for "error messages" in Visual Basic Help
and find information about the message you received.