Macro tutorial http://www.tutorialspoint.com/ms_excel_2010/excel_using_macros.htm http://office.microsoft.com/en-001/excel-help/create-or-delete-a-macro-HP010342374.aspx Create or delete a macro Show All To automate a repetitive task, you can quickly record a macro in Microsoft Excel. You can also create a macro by using the Visual Basic Editor in Microsoft Visual Basic for Applications (VBA) 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 VBA 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. 1. If the Developer tab is not available, do the following to display it: 1. Click the File tab. 2. Click Options, and then click Customize Ribbon. 3. In the Customize Ribbon category, in the Main Tabs list, select the Developer check box, and then click OK. 2. To set the security level temporarily to enable all macros, do the following: 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. 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. 1. On the Developer tab, in the Code group, click Record Macro. 2. 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. 3. 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, see Excel shortcut and function keys. 4. 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. 5. In the Description box, type a description of the macro. 6. Click OK to start recording. 7. Perform the actions that you want to record. 8. 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 VBA 1. If the Developer tab is not available, do the following to display it: 1. Click the File tab. 2. Click Options, and then click Customize Ribbon. 3. In the Customize Ribbon category, in the Main Tabs list, select the Developer check box, and then click OK. 2. To set the security level temporarily to enable all macros, do the following: 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. 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. 1. On the Developer tab, in the Code group, click Visual Basic. 2. If needed, in the Visual Basic Editor, on the Insert menu, click Module. Note Modules are automatically created for all sheets in the workbook. 3. In the code window of the module, type or copy the macro code that you want to use. 4. To run the macro from the module window, press F5. 5. 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: 1. Click the File tab. 2. Click Options, and then click Customize Ribbon. 3. In the Customize Ribbon category, in the Main Tabs list, select the Developer check box, and then click OK. 2. To set the security level temporarily to enable all macros, do the following: 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. 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. 1. 2. 3. 4. 5. Tip 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. To copy the whole macro, include the Sub and End Sub lines in the selection. 6. On the Edit menu, click Copy. Tip You can also right-click and then click Copy, or you can press CTRL+C. 7. In the Procedure box of the code window, click the module where you want to place the code. 8. 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: Open the workbook that contains the macro that you want to delete. 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: 1. On the View tab, in the Window group, click Unhide. 2. Under Unhide workbooks, click PERSONAL, and then click OK. 2. If the Developer tab is not available, do the following to display it: 1. Click the File tab. 2. Click Options, and then click Customize Ribbon. 3. In the Customize Ribbon category, in the Main Tabs list, select the Developer check box, and then click OK. 1. On the Developer tab, in the Code group, click Macros. 2. In the Macros in list, select the workbook that contains the macro that you want to delete. For example, click This Workbook. 3. In the Macro name box, click the name of the macro that you want to delete. 4. Click Delete. Quick start: Create a macro If you have tasks in Microsoft Excel that you do repeatedly, you can record a macro to automate those tasks. A macro is an action or a set of actions that you can run as many times as you want. When you create a macro, you are recording your mouse clicks and keystrokes. After you create a macro, you can edit it to make minor changes to the way it works. Suppose that every month, you create a report for your accounting manager. You want to format the names of the customers with overdue accounts in red, and also apply bold formatting. You can create and then run a macro that quickly applies these formatting changes to the cells you select. How? Before you record a macro Make sure the Developer tab is visible on the ribbon. By default, the Developer tab is not visible, so do the following: 1. Click the File tab, click Options and then click the Customize Ribbon category. 2. Under Customize the Ribbon, in the Main Tabs list, click Developer, and then click OK. Record a macro 1. In the Code group on the Developer tab, click Record Macro, and then click OK to start recording. 2. Perform some actions in your worksheet, such as typing some text, selecting some columns or rows, or filling down some data. 3. In the Code group on the Developer tab, click Stop Recording. Take a closer look at the macro and experiment You can learn a little about the Visual Basic programming language by editing a macro that you have recorded. To edit a macro, in the Code group on the Developer tab, click Macros, select the name of the macro that you recorded, and click Edit. This starts the Visual Basic Editor. Take a look at the code, and see how the actions that you recorded appear as code. Some of the code will probably be clear to you, and some of it may be a little mysterious. Experiment with the code, close the Visual Basic Editor, and run your macro again. This time, see if anything different happens! Next steps To learn more about creating macros, see Create or delete a macro. To learn about how to run a macro, see Run a macro. To learn about how to enable macros in Excel, see Change macro security settings in Excel. Save time by creating and running macros in Excel 2010 Quick Reference Card See also Create or delete a macro Quick start: Create a macro All Microsoft Office Training Excel macro best practices It's usually a good idea to turn on the Use Relative References option because the Macro Recorder will be more likely to produce the steps that you expect. Turn on this option by clicking the Use Relative References button in the Code group on the Developer tab. Learn how to show the Developer tab (by default, it's not shown) in the "Record a macro" section, below. Inspect your macro in the Visual Basic Editor after you've recorded it to see if you can improve it or at least understand how it works by looking at its instructions. Do this by selecting your macro in the Macros dialog box (in the Code group on the Developer tab, click Macros) and then clicking the Edit button. Make a habit of saving your macros to the personal workbook so that you can reuse them on your computer. Do this by selecting the Personal Macro Workbook option in the Store macro in list when you record a macro. Record a macro First, make sure the Developer tab is visible, because that's where all the macro commands are found: 1. Click the File tab, click Options, and then click Customize Ribbon. 2. Under Main Tabs, make sure the Developer check box is selected. Then, start the Macro Recorder: 1. 2. 3. 4. On the Developer tab, in the Code group, click Record Macro. Optionally, you can assign your macro a shortcut key combination so that it's easy to run. Click OK to start the Macro Recorder. In your workbook, perform the actions that you want recorded, which can include typing words or numbers, clicking cells, clicking buttons, dragging cells, formatting, and more. 5. When you're done with the actions that you want recorded, click Stop Recording. Run your macro 1. On the Developer tab, in the Code group, click Macros. 2. In the Macros dialog box, find your macro and click Run. Note If you assigned your macro a keyboard combination (for example, CTRL+SHIFT+M) when you started the macro recorder, you can use that shortcut to run the macro. Record a macro in your personal macro workbook By saving a macro to the personal macro workbook, you make your macro available every time you open or create an Excel workbook on your computer. It's best to make this decision when you first record your macro, because it requires a bit of work (and knowledge of the Visual Basic Editor environment) to later move a macro to the personal macro workbook. 1. Click Record Macro and then, in the Store In list, select Personal Macro Workbook. 2. Click OK to record your macro, and click Stop Recording when you're done. If you have already created a macro in your current workbook and would like to copy it to your personal workbook, you can record an empty macro whose instructions you can then replace. Do the following: 1. On the Developer tab, in the Code group, click Record Macro. 2. In the Store In list, select Personal Macro Workbook. 3. Give the macro the same name as the macro that you've already saved to your current workbook (that is, the macro whose instructions you want to copy). 4. Click OK to start the Macro Recorder, and then immediately stop the recording. 5. On the Developer tab, in the Code group, click Macros, select the macro in the current workbook that contains the instructions you want to copy, and then click Edit. 6. In the Visual Basic Editor, select the entire macro, starting with "Sub" and ending with "End Sub." 7. Click CTRL+C to copy the macro, and then close the Visual Basic Editor. 8. Click Macros again and this time select the macro you just recorded to your personal macro workbook (in the list, it's probably preceded by PERSONAL.XLSB!). 9. Click Edit to start the Visual Basic Editor, and select all the lines of the macro, starting with "Sub" and ending with "End Sub." 10. Paste the macro that you previously copied over these instructions to replace them. 11. Close the Visual Basic Editor to save your changes. When you close Excel after you've saved a macro to your personal macro workbook, you'll be prompted to save the changes to the personal workbook. Be sure to click Yes to save those changes. Add a macro as a button to the Quick Access Toolbar or to a custom group To add your macro to the Quick Access Toolbar as a button: 1. 2. 3. 4. Click the File tab, and then click Options. Click Quick Access Toolbar. Under Choose Commands from, select Macros. Find and select your macro in the list. 5. Click Add and then click OK. 6. To change the name of the macro that's shown on the Quick Access Toolbar, click Modify and type the name you want displayed in the Display name box. 7. Use the Move Up and Move Down buttons (to the right of the box) to change the position of the buttons on the Quick Access Toolbar. To add a custom group to a tab and then add your macro to that custom group as a button: 1. Click the File tab, and then click Options. 2. Click Customize Ribbon. 3. In the pane on the right, select the tab on which you want to create a custom group, such as the Developer tab. 4. Create a new group by clicking New Group. 5. Click Rename to change the name of your custom group, and then click OK. 6. With your new group selected, in the pane on the left, find and select your macro under Choose Commands from. 7. Click Add to add the macro to your custom group. 8. To change the text that's shown next to the icon, click Rename and type the text you want displayed in the Display name box. As an optional step, choose a new symbol for your button in the Rename dialog box. 9. Click OK.