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

Moving Objects with a Chart

Excel allows you to add all sorts of objects to your worksheets. Among the objects you can add are text boxes, shapes, ...

Discover More

Importing AutoCorrect Entries

The AutoCorrect feature in Word is quite handy, but getting a lot of entries into the feature can be tedious. This tip ...

Discover More

Replacing Hidden Text

Word allows you to format text so it can be easily hidden from view and from printing. If you want to convert the hidden ...

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)

Searching Very Large Strings in a Macro

VBA provides a few different ways you can search for information within strings. This tip looks at the most efficient ...

Discover More

Saving Changes when Closing

If your macro closes workbooks, you'll want to make sure that it will save any changes you made to the workbook. Here's ...

Discover More

Copying a Set Range from Multiple Worksheets to a New Worksheet

Want to create a summary worksheet that pulls a single row of data from each worksheet in the workbook? Here are a couple ...

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