Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Writing a Macro from Scratch.

Writing a Macro from Scratch

Written by Allen Wyatt (last updated October 21, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


2

Many of the tips used in ExcelTips rely upon macros in order to run. Some readers may not know how to enter a macro from scratch in Excel. There are actually two ways you can create macros. First you can record a macro, which is appropriate when you want to record a series of steps you perform quite often. The second method of creating a macro, writing one from scratch, is much more powerful.

To create a macro from scratch, follow these steps:

  1. Make sure the Developer tab of the ribbon is displayed.
  2. In the Code group, click Macros. Word displays the Macros dialog box. (You can also display the Macros dialog box by simply pressing Alt+F8.)
  3. Using the Macros In drop-down list (near the bottom of the dialog box), select where you want your new macro stored.
  4. In the Macro Name box, type a descriptive name you want assigned to the macro you are writing. (Make sure the name doesn't have any spaces in it.)
  5. Optionally, you can enter information in the Description box.
  6. Click on Create. The VBA Editor is started and you can write your macro. (You can also paste macro code from other sources, such as ExcelTips.)
  7. When you are through, close the macro window by selecting the Close and Return to Microsoft Excel option from the File menu, or press Alt+Q.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13520) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Writing a Macro from Scratch.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Default Headers and Footers

Changing the look of default headers and footers for your Excel workbooks.

Discover More

Using Multiple Print Settings

Do you have a worksheet from which you need to print only portions of the data available? There are two ways you can ...

Discover More

Changing Macro Cell References Based on Edits

Place a cell reference in a macro, modify the structure of your worksheet, and you may soon find that the cell reference ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Preserving the Undo List

The undo list can be a lifesaver when working in a macro. Unfortunately, the undo list is not preserved when you run a ...

Discover More

Removing All Macros

Macros are stored as part of a workbook so that they are always available when you have the workbook open. If you want to ...

Discover More

Removing Pictures for a Worksheet in VBA

Excel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 2?

2023-10-24 01:58:02

Tomek

You can get to the VBA Editor simply by pressing Alt+F11. Once there you can navigate to the module where you want to place the macro. The only problem may be that your open workbook or even your personal macro workbook may not have any macro module yet. In such case starting to create a macro as described in this tip will create a new module where you can then write your macro. This may be convenient,but also may make managing macros a bit more complicated. The reason is that after doing it several times in several Excel sessions you may end up with a multitude of modules with recent macros being added to the module created most recently. Then you have to re-organize your macros (move them) so that related ones are in the same module.

Don't get me wrong, the macros will likely work as expected, even if they are scattered between different modules in the same workbook. I just prefer to have full control of where my macros are.

BTW : once in the VBA editor you can easily insert a new module into the workbook, or sub or function into the selected module using <Insert> from the menu, and easily select whether they are Public or Private. Alternatively, if you just type "Sub " followed by a valid macro name, the editor will automatically provide the required parentheses as well as "End Sub" statement.


2023-10-21 13:50:55

J. Woolley

Re. the Tip's step 5, you cannot click the Options... button to enter information in the Description box until after you have created the macro. Clicking the Options... button opens the Macro Options dialog where you can edit the Description and optionally define a Shortcut key. If you define a Shortcut, you can include Shift but not Ctrl (which is implied) or Alt. But avoid overriding a commonly used Excel shortcut; for example, f overrides Excel's Find shortcut, but Shift+F does not.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.