macro

Upload: sharun-suresh

Post on 17-Mar-2016

3 views

Category:

Documents


0 download

DESCRIPTION

macro

TRANSCRIPT

  • Show All

    To automate repetitive tasks, you can quickly record a macro (macro: An action or a set of actions that you can use to automate tasks. Macros are recorded in the Visual Basic for Applications programming language.) in Microsoft Office Excel. You can also create a macro by using the Visual Basic Editor (Visual Basic Editor: An environment in which you write new and edit existing Visual Basic for Applications code and procedures. The Visual Basic Editor contains a complete debugging toolset for finding syntax, run-time, and logic problems in your code.) 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 need to 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, all steps that are needed to complete the actions that you want to record are recorded by the macro recorder. Navigation on the Ribbon is not included in the recorded steps.

    Excel > Macros

    Create a macro

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

    2. To set the security level temporarily to enable all macros, do the following:

    1. Click the Microsoft Office Button , and then click Excel Options.

    2. 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.

    1. On the Developer tab, in the Code group, click Macro Security.

    Page 1 of 5Create a macro

    03/01/2015ms-help://MS.EXCEL.12.1033/EXCEL/content/HP10014111.htm

  • Top of Page

    NOTE To help prevent potentially dangerous code from running, we recommend that you return to any of the settings that disable all macros after you finish working with macros.

    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. Following 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. 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.

    5. To assign a CTRL combination shortcut key (shortcut key: A function key or key combination, such as F5 or CTRL+A, that you use to carry out a menu command. In contrast, an access key is a key combination, such as ALT+F, that moves the focus to a menu, command, or control.) 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, see Excel shortcut and function keys.

    6. In the Store macro in list, select the workbook in which 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 doesn't already exist, and saves the macro in this workbook. In Microsoft Windows XP, this workbook is saved in the C:\Documents and Settings\user name\Application Data\Microsoft\Excel\XLStart folder so that it can be loaded automatically whenever Excel starts. In Microsoft Windows Vista, this workbook is saved in the C:\Users\user name\Application Data\Microsoft\Excel\XLStart folder. 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.

    7. To include a description of the macro, in the Description box, type the text that you want.

    8. Click OK to start recording.

    9. Perform the actions that you want to record.

    10. 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.

    2. Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then click OK.

    Page 2 of 5Create a macro

    03/01/2015ms-help://MS.EXCEL.12.1033/EXCEL/content/HP10014111.htm

  • Create a macro by using Microsoft Visual Basic

    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:

    2. To set the security level temporarily to enable all macros, do the following:

    1. Click the Microsoft Office Button , and then click Excel Options.

    2. 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.

    NOTE To help prevent potentially dangerous code from running, we recommend that you return to any 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, 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. On the File menu, click Close and Return to Microsoft Excel when you finish writing the macro.

    1. On the Developer tab, in the Code group, click Macro Security.

    2. Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then click OK.

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

    1. Click the Microsoft Office Button , and then click Excel Options.

    Page 3 of 5Create a macro

    03/01/2015ms-help://MS.EXCEL.12.1033/EXCEL/content/HP10014111.htm

  • 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

    2. To set the security level temporarily to enable all macros, do the following:

    2. 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.

    NOTE To help prevent potentially dangerous code from running, we recommend that you return to any of the settings that disable all macros after you finish working with macros.

    3. Open the workbook that contains the macro that you want to copy.

    4. On the Developer tab, in the Code group, click Macros.

    5. In the Macro name box, click the name of the macro that you want to copy.

    6. Click Edit.

    7. In the code window of the Visual Basic Editor, select the lines of the macro that you want to copy.

    TIP To copy the entire macro, include the Sub and End Sub lines in the selection.

    8. On the Edit menu, click Copy.

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

    9. In the Procedure box of the code window, click the module where you want to place the code.

    10. On the Edit menu, click Paste.

    TIP You can also right-click and then click Paste on the shortcut menu, or you can press CTRL+V.

    1. On the Developer tab, in the Code group, click Macro Security.

    2. Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then click OK.

    Page 4 of 5Create a macro

    03/01/2015ms-help://MS.EXCEL.12.1033/EXCEL/content/HP10014111.htm

  • Top of Page

    Delete a macro

    Top of Page

    See Also

    Enable or disable macros in Office documents Overview of macro security

    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 on the shortcut menu.

    2. In the Macro name box, click the macro that you want to assign.

    1. Open the workbook that contains the macro that you want to delete.

    2. On the Developer tab, in the Code group, click Macros.

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

    3. In the Macros in list, select This Workbook.

    4. In the Macro name box, click the name of the macro that you want to delete.

    5. Click Delete.

    1. Click the Microsoft Office Button , and then click Excel Options.

    2. 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.

    Page 5 of 5Create a macro

    03/01/2015ms-help://MS.EXCEL.12.1033/EXCEL/content/HP10014111.htm