This guide explains how to create and use a YY macro in Microsoft Excel. A YY macro is simply a recorded or written VBA (Visual Basic for Applications) macro that you name with the letters “YY” (or any prefix you choose) to quickly identify it. It can automate repetitive tasks like formatting, data entry, or report generation. You’ll learn how to record a macro, assign it to a button or keyboard shortcut, and run it safely. First, you need to enable the Developer tab in Excel (if it’s not already visible) and ensure macros are allowed in your workbook.
What You’ll Need
- Microsoft Excel 2016 or later (Microsoft 365 recommended) on Windows or Mac.
- A workbook where you want to run the macro (save it as
.xlsm– macro-enabled – after recording). - Basic familiarity with Excel’s ribbon interface.
- If you plan to write VBA code manually, a text editor or the VBA editor (Alt+F11).
Step-by-Step
- Enable the Developer tab – In Excel, go to File > Options > Customize Ribbon. Under “Main Tabs,” check the box for Developer and click OK. On Mac, go to Excel > Preferences > Ribbon & Toolbar and check “Developer.”
- Set macro security – Go to Developer > Macro Security. Choose “Disable all macros with notification” (recommended) so you can enable macros when opening a trusted workbook. On Mac, this is under Developer > Macro Security as well.
- Plan your macro – Decide exactly what steps you want to automate. For example, if you want to apply a specific number format and bold to a selected range, note those actions.
- Start recording – Select the cell or range you want the macro to act on (if any). Go to Developer > Record Macro. In the dialog, give the macro a name like
YY_Format(must start with a letter, no spaces). Optionally assign a shortcut key (e.g.,Ctrl+Shift+Y) and store it in “This Workbook.” Click OK. - Perform the actions – Do the steps you planned. For example, select a range, press
Ctrl+1to open Format Cells, choose a number format, click OK, then pressCtrl+Bto bold. Every click and keystroke is recorded. - Stop recording – Go to Developer > Stop Recording. Your macro is now saved in the workbook’s VBA project.
- Run the macro – To test, select a new range, then go to Developer > Macros, choose your macro (e.g., YY_Format), and click Run. Alternatively, use the assigned shortcut key.
- Assign to a button (optional) – Insert a shape or button from Developer > Insert > Button (Form Control). Draw it on the sheet, then in the Assign Macro dialog, select your YY macro and click OK. Right-click the button to edit its text.
- Save as macro-enabled – Press
Ctrl+S(orCmd+Son Mac). In the Save As dialog, choose “Excel Macro-Enabled Workbook (*.xlsm)” from the file type dropdown. This preserves your macro.
Common Mistakes
- Forgetting to save as .xlsm – If you save as .xlsx, the macro is discarded. Always use .xlsm.
- Recording absolute references – By default, macros record absolute cell references (e.g., $A$1). If you want the macro to work on any selected cell, switch to relative reference mode: on the Developer tab, click “Use Relative References” before recording.
- Naming conflicts – Using a name that already exists (like “Print”) will cause an error. Use a unique prefix like “YY_”.
- Not testing on a copy – Running a macro that deletes data or formats incorrectly can ruin your work. Always test on a copy of your workbook.
- Ignoring security prompts – If you open a macro-enabled file and click “Disable Macros,” the macro won’t run. Enable content only if you trust the source.
Tips
- Use relative references – If your macro should work on any selected range, turn on “Use Relative References” before recording. This makes the macro record actions relative to the active cell.
- Keep macros short – Break complex tasks into multiple small macros (e.g., YY_Format, YY_Clear). This makes debugging easier.
- Add comments in VBA – Open the VBA editor (Alt+F11), find your macro in “Modules,” and add comments with an apostrophe (‘) to explain what each section does. This helps you later.
- Use keyboard shortcuts wisely – Avoid overwriting Excel’s built-in shortcuts (like Ctrl+C). Use
Ctrl+Shift+Letterinstead. - Store macros in Personal Macro Workbook – If you want the macro available in all workbooks, record it into “Personal Macro Workbook” (choose that in the “Store macro in” dropdown). This creates a hidden file that loads on startup.
Troubleshooting
- Macro doesn’t appear in the list – Make sure the macro is stored in “This Workbook” or “Personal Macro Workbook.” If you saved as .xlsx, the macro is gone. Re-record and save as .xlsm.
- “Macros are disabled” error – Go to Developer > Macro Security and select “Disable all macros with notification.” Then close and reopen the workbook, and click “Enable Content” when the yellow bar appears.
- Macro runs but does nothing – Check if you recorded with absolute references while expecting relative behavior. Re-record with relative references on. Also verify that the selected range is correct.
- Runtime error 1004 – This often means the macro tries to access a range that doesn’t exist. Open the VBA editor (Alt+F11), read the highlighted line, and correct the range reference.
- Shortcut key doesn’t work – If you assigned a shortcut that conflicts with Excel’s, Excel may ignore it. Reassign the macro to a different shortcut via Developer > Macros > Options.
- Button doesn’t run the macro – Right-click the button, select “Assign Macro,” and ensure the correct macro name is selected. If the macro is in a different workbook, it may not be available.
Frequently Asked Questions
Q: Can I edit a recorded macro?
A: Yes. Press Alt+F11 to open the VBA editor, find the macro under Modules, and modify the code. For example, you can change a range or add a loop.
Q: How do I make a macro run automatically when I open a workbook?
A: Name the macro Auto_Open in a standard module. It will run when the workbook is opened, provided macros are enabled. Alternatively, use the Workbook_Open event in the ThisWorkbook object.
Q: Will a macro work on Mac Excel?
A: Most recorded macros work on both Windows and Mac, but some features differ (e.g., file paths, certain dialog boxes). Test on your platform. Keyboard shortcuts differ: use Cmd instead of Ctrl on Mac.
Q: Can I share a macro with someone else?
A: Yes, send them the .xlsm file. They must enable macros when opening. Alternatively, export the module from the VBA editor (File > Export File) and import it into their workbook.
Q: What’s the difference between a macro and a VBA function?
A: A macro is a subroutine that performs actions (like formatting). A function returns a value and is used in formulas (e.g., =MyFunction()). Macros are run with a button or shortcut; functions are called from cells.
Q: Is it safe to run macros from the internet?
A: Only run macros from sources you trust. Malicious macros can delete files or spread malware. Always scan the file with antivirus software and review the code if possible.
Related
- Free Time Tracking Excel Template: How to Build One
- Free Student Attendance Template Excel: Get One
- 911 Dispatch Data Entry Practice Test: 10 Drills
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Active Workbook: Meaning, Use, and ExamplesIn Excel VBA, ActiveWorkbook refers to the workbook that is currently active in the Excel window. Use it…
- Excel VBA: Find the Last Used Row in Any WorksheetTo find the last row containing content anywhere on a worksheet in Excel VBA, use Range.Find with SearchOrder:=xlByRows…
- Excel Macro Recorder: Can It Repeat Without a Limit?Excel’s Macro Recorder does not have a built-in “repeat forever” setting: it records a set of actions once.…
- Excel VBA TextBox for Data Entry: Setup and CodeYes. In Excel for Microsoft 365 on Windows, a VBA TextBox can collect information through a UserForm or…
- Excel VBA Filter Multiple Criteria: Examples and CodeTo filter by multiple criteria in Excel VBA, use the range’s AutoFilter method: apply one criterion per field…
- Excel VBA Edit Range: How to Change Cell Values SafelyTo edit a cell or range with Excel VBA, assign a value or formula to a Range object,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.