You are on page 1of 9

Create or delete a macro

To automate a repetitive task, you can quickly record a macro in Microsoft Office Excel. You can also create a macro by
using the Visual Basic Editor in Microsoft Visual Basic to write your own macro script, or to copy all or part of a macro to a
new macro. After you create a macro, you can assign it to an object (such as a toolbar button, graphic, or control) so that
you can run it by clicking the object. If you no longer use a macro, you can delete it.

What do you want to do?


Record a macro
Create a macro by using Microsoft Visual Basic
Copy part of a macro to create another macro
Assign a macro to an object, graphic, or control
Delete a macro

Record a macro
When you record a macro, the macro recorder records all the steps required to complete the actions that you want your
macro to perform. Navigation on the Ribbon is not included in the recorded steps.
NOTE The Ribbon is a component of the Microsoft Office Fluent user interface.

1. If the Developer tab is not available, do the following to display it:


a. Click the Microsoft Office Button
, and then click Excel Options.
b. In the Popular category, under Top options for working with Excel, select the Show Developer tab in the Ribbon check
box, and then click OK.
2. To set the security level temporarily to enable all macros, do the following:
a. On the Developer tab, in the Code group, click Macro Security.

b. Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then
click OK.
NOTE To help prevent potentially dangerous code from running, we recommend that you return to any one of the settings that

disable all macros after you finish working with macros. For more information about how to change the settings, see Change
macro security settings in Excel.

3. On the Developer tab, in the Code group, click Record Macro.


4. In the Macro name box, enter a name for the macro.
NOTE The first character of the macro name must be a letter. Subsequent characters can be letters, numbers, or underscore

characters. Spaces cannot be used in a macro name; an underscore character works well as a word separator. If you use a
macro name that is also a cell reference, you may get an error message that the macro name is not valid.
1. To assign a CTRL combination shortcut key to run the macro, in the Shortcut key box, type any lowercase letter or
uppercase letter that you want to use.
NOTE The shortcut key will override any equivalent default Excel shortcut key while the workbook that contains the

macro is open. For a list of CTRL combination shortcut keys that are already assigned in Excel, seeExcel shortcut and
function keys.
2. In the Store macro in list, select the workbook where you want to store the macro.
TIP If you want a macro to be available whenever you use Excel, select Personal Macro Workbook. When you

select Personal Macro Workbook, Excel creates a hidden personal macro workbook (Personal.xlsb) if it does not already
exist, and saves the macro in this workbook. In Windows Vista, this workbook is saved in the C:\Users\user
name\AppData\Local\Microsoft\Excel\XLStart folder. In Microsoft Windows XP, this workbook is saved in the
C:\Documents and Settings\user name\Application Data\Microsoft\Excel\XLStart folder. Workbooks in the XLStart folder
are opened automatically whenever Excel starts. If you want a macro in the personal macro workbook to be run
automatically in another workbook, you must also save that workbook in the XLStart folder so that both workbooks are
opened when Excel starts.
3. In the Description box, type a description of the macro.
4. Click OK to start recording.
5. Perform the actions that you want to record.
6. On the Developer tab, in the Code group, click Stop Recording
TIP You can also click Stop Recording

on the left side of the status bar.

Top of Page

Create a macro by using Microsoft Visual Basic


1. If the Developer tab is not available, do the following to display it:
a. Click the Microsoft Office Button
, and then click Excel Options.
b. In the Popular category, under Top options for working with Excel, select the Show Developer tab in the Ribbon check
box, and then click OK.
2. To set the security level temporarily to enable all macros, do the following:
a. On the Developer tab, in the Code group, click Macro Security.

b. Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then
click OK.
NOTE To help prevent potentially dangerous code from running, we recommend that you return to any one of the settings that

disable all macros after you finish working with macros.


3. On the Developer tab, in the Code group, click Visual Basic.
4. If needed, in the Visual Basic Editor, on the Insert menu, click Module.
NOTE Modules are automatically created for all sheets in the workbook.

5. In the code window of the module, type or copy the macro code that you want to use.
6. To run the macro from the module window, press F5.
7. In the Visual Basic Editor, on the File menu, click Close and Return to Microsoft Excel when you finish writing the
macro.
Top of Page

Copy part of a macro to create another macro


1. If the Developer tab is not available, do the following to display it:
a. Click the Microsoft Office Button
, and then click Excel Options.
b. In the Popular category, under Top options for working with Excel, select the Show Developer tab in the Ribbon check
box, and then click OK.
2. To set the security level temporarily to enable all macros, do the following:
a. On the Developer tab, in the Code group, click Macro Security.

b. Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then
click OK.
NOTE To help prevent potentially dangerous code from running, we recommend that you return to any one of the settings that

disable all macros after you finish working with macros.


3.
4.
5.
6.
7.

Open the workbook that contains the macro that you want to copy.
On the Developer tab, in the Code group, click Macros.
In the Macro name box, click the name of the macro that you want to copy.
Click Edit.
In the code window of the Visual Basic Editor, select the lines of the macro that you want to copy.
TIP To copy the whole macro, include the Sub and End Sub lines in the selection.

1. On the Edit menu, click Copy.


TIP You can also right-click and then click Copy, or you can press CTRL+C.

2. In the Procedure box of the code window, click the module where you want to place the code.
3. On the Edit menu, click Paste.
TIP You can also right-click and then click Paste, or you can press CTRL+V.
TIP You cannot edit your Personal Macro Workbook file (Personal.xlsb) because it is a hidden workbook that is always

open. You must first unhide it by using the Unhide command. You can also open it in the Visual Basic Editor by pressing
ALT+F11.
Top of Page

Assign a macro to an object, graphic, or control


1. On a worksheet, right-click the object, graphic, or control to which you want to assign an existing macro, and then
click Assign Macro.
2. In the Macro name box, click the macro that you want to assign.
Top of Page

Delete a macro
1. Do one of the following:
o Open the workbook that contains the macro that you want to delete.
o If the macro that you want to delete is stored in the personal macro workbook (Personal.xlsb), and this workbook is hidden,
do the following to unhide the workbook:
On the View tab, in the Window group, click Unhide.
Under Unhide workbooks, click PERSONAL, and then click OK.
2. If the Developer tab is not available, do the following to display it:
a. Click the Microsoft Office Button
, and then click Excel Options.
b. In the Popular category, under Top options for working with Excel, select the Show Developer tab in the Ribbon check
box, and then click OK.
3. On the Developer tab, in the Code group, click Macros.

4. In the Macros in list, select the workbook that contains the macro that you want to delete. For example, clickThis
Workbook.
5. In the Macro name box, click the name of the macro that you want to delete.
6. Click Delete.

Create a Macro
Developer Tab

| Command Button

| Assign a Macro

| Visual Basic Editor

With Excel VBA you can automate tasks in Excel by writing so called macros. In this chapter, learn how to create a simple macro which will be
executed after clicking on a command button. First, turn on the Developer tab.

Developer Tab
To turn on the Developter tab, execute the following steps.

1. Right click anywhere on the ribbon, and then click Customize the Ribbon.

Advertisement

2. Under Customize the Ribbon, on the right side of the dialog box, select Main tabs (if necessary).

3. Check the Developer check box.

4. Click OK.

5. You can find the Developer tab next to the View tab.

Command Button
To place a command button on your worksheet, execute the following steps.

1. On the Developer tab, click Insert.

2. In the ActiveX Controls group, click Command Button.

3. Drag a command button on your worksheet.


Advertisement

Assign a Macro
To assign a macro (one or more code lines) to the command button, execute the following steps.

1. Right click CommandButton1 (make sure Design Mode is selected).

2. Click View Code.

The Visual Basic Editor appears.

3. Place your cursor between Private Sub CommandButton1_Click() and End Sub.

4. Add the code line shown below.

Note: the window on the left with the names Sheet1, Sheet2 and Sheet3 is called the Project Explorer. If the Project Explorer is not visible, click
View, Project Explorer. To add the Code window for the first sheet, click Sheet1 (Sheet1).

5. Close the Visual Basic Editor.

6. Click the command button on the sheet (make sure Design Mode is deselected).

Result:

Congratulations. You've just created a macro in Excel!

Visual Basic Editor


To open the Visual Basic Editor, on the Developer tab, click Visual Basic.

The Visual Basic Editor appears.