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

Understanding Default Insert Date Formatting

Insert a date into Word, and you are presented with a variety of formats you can choose from for that date. The default ...

Discover More

Copying Large, Object-Rich Worksheets from a Corrupted Workbook

Salvaging information from a corrupted workbook can be a lot of work. This tip looks at how you can approach the problem ...

Discover More

Protecting Document Areas from Global Replacements

You may have boilerplate text that you need to include in your document, and it would be detrimental to accidently change ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Using SUM In a Macro

Want to use a worksheet function (such as SUM) from within a macro? Here's how easy it is to accomplish the task.

Discover More

Pulling Apart Characters in a Long String

You can easily use formulas to pull apart text stored in a cell. For instance, if you need to pull individual characters ...

Discover More

Stopping a Checked Box from being Unchecked

When creating user forms for use in Excel, you are provided with a range of controls you can add, including check boxes. ...

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 two more than 7?

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.