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.
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
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:
Note:
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.
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!
The undo list can be a lifesaver when working in a macro. Unfortunately, the undo list is not preserved when you run a ...
Discover MoreMacros are stored as part of a workbook so that they are always available when you have the workbook open. If you want to ...
Discover MoreExcel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments