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

Getting User Input in a Dialog Box

Want to grab some interactive input from a user in your macro? The best way to do that is with the InputBox function, ...

Discover More

Specifying a Time Server for Windows to Use

Windows can utilize resources on the Internet to make sure that your computer's internal clock is working at the right ...

Discover More

Excel 2016 Filters and Filtering (Table of Contents)

Excel provides two ways to filter your data so that only what you want to see is displayed. Discover how filtering works ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Pulling First Letters from Parenthetical Text

When working with text in Excel, you can slice and dice it in many ways. This tip shows how to pull first letters from ...

Discover More

Displaying the "Last Modified" Date

Want to know when a workbook was last modified? Want to put that date within the header of your worksheet? Here's how to ...

Discover More

Tools on Developer Tab are Unavailable

Want to add some macros to your workbook? What do you do if you try to add the macros but the program has disabled the tools?

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 5 - 5?

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.