How to write a VBA macro in Excel 2010

If you know VBA (Visual Basic for Applications) programming then you can create your own VBA macros and run them from within Excel.

To write a VBA macro, follow these steps:

1. On the Excel Ribbon click the Developer tab. If it is not in the ribbon, click here to see how to show the Developer tab.

2. On the Code group click Visual Basic.

3. You will be switched to Visual Basic window. It has two panes. The left is the Project pane, which lists your workbook, worksheets and any VBA modules. The right is the code editor.

4. On the VBA toolbar click Insert and select Module.

5. The module name will be listed in the project pane, and you will be switched to the right pane where you can enter your code. The VBA window will finally look like this:

7. When you are done with your code, on the File menu click Close and return to Microsoft Excel.

8. Your new macro is now ready to be run.

9. To run your macro:

  • On the Developer tab, Code group click Macros. A list of available macros will be displayed.

  • Select your macro and click Run.