How to Edit Macros in Excel: The Ultimate Beginner's Guide

Last updated on April 14, 2023 By Victor Chan

Microsoft Excel is undeniably a powerful tool for crunching numbers, analyzing data, and creating professional reports.

However, the true power of Excel lies in its ability to automate repetitive tasks through the use of macros, which are powered by Visual Basic for Applications (VBA).

This comprehensive guide will dive deep into the ins and outs of editing Excel macros. You'll learn how to edit macros like a pro, unlock your spreadsheet's full potential, and boost your productivity.

A Quick Introduction to Excel Macros and VBA

Macros are your best friend if you're looking for a way to automate repetitive tasks in Excel.

Macros are a series of commands that you can record and run with a single click or a keyboard shortcut. They can save you time, improve your accuracy, and make your work more efficient.

Visual Basic for Applications (VBA) is the programming language behind Excel macros and other Microsoft Office applications.

Macros save time, reduce the potential for human error, and can be simple (such as formatting cells) or complex (performing calculations or generating reports).

But what if you want to change something in your macro? Maybe you want to add a new feature, fix an error, or optimize your code. How do you edit a macro in Excel?

In the rest of this article, I'll show you how to edit a macro in Excel using the Visual Basic Editor (VBE). You'll learn how to access the VBE, modify your code, and run your macro. You'll also discover some tips and tricks to make your macro editing easier and faster.

Let's get started!

What is the Visual Basic Editor?

The Visual Basic Editor is a tool that allows you to write, edit, and debug VBA code in Excel. VBA stands for Visual Basic for Applications, which is the programming language that Excel macros use.

The VBE has a user interface that consists of several windows and toolbars. Here are some of the main components of the VBE:

  • Toolbox: This toolbar contains various controls that you can use to create user forms in VBA. User forms are custom dialog boxes that allow users to interact with your macros.
  • Project Explorer: This window shows all the workbooks and worksheets that contain VBA code in your Excel session. You can use it to navigate and organize your projects.
  • Code Window: This window shows the VBA code of the selected module or object. You can use it to write and edit your code.
  • Properties Window: This window shows the properties of the selected object, such as its name, caption, color, etc. You can use it to change the appearance and behavior of your objects.
  • Immediate Window: This window allows you to execute VBA statements on the fly. You can use it to test your code, display values of variables, or perform calculations.

You can customize the layout of the VBE by resizing, moving, docking, or hiding the windows and toolbars. You can also use keyboard shortcuts to switch between them.

How to Access the Visual Basic Editor to View Macros

There are several ways to access the Visual Basic Editor in Excel. Here are some of the most common methods:

  • Method 1: Press Alt + F11 on your keyboard. This is the fastest and easiest way to open the VBE.
  • Method 2: Click on the Developer tab on the Ribbon. If you don't see this tab, go to File > Options > Customize Ribbon and check the Developer box under Main Tabs. Then click on Visual Basic in the Code group.
  • Method 3: Right-click on any worksheet tab and select View Code. This will open the VBE and show the code of that worksheet.

Once you open the VBE, you can see all the macros that you have recorded or written in Excel. To find a specific macro, you can use the Project Explorer or the Macro dialog box.

To use the Project Explorer, expand the workbook that contains your Macro and look for a folder called Modules. Inside this folder, you'll find one or more modules that store your macros. Double-click on a module to open its code window and see its macros.

To use the Macro dialog box, go back to Excel and click on Macros in the Code group of the Developer tab. Alternatively, press Alt + F8 on your keyboard. This will show a list of all the macros in your workbook or all open workbooks. Select a macro and click on Edit to open its code window in the VBE.

How to Edit a Macro in Microsoft Excel

By learning how to edit macros in Excel, you'll gain the ability to harness the true power of VBA, creating custom solutions tailored to your unique needs.

Step 1: Access the VBA Editor and Locate Your Macro

To begin editing macros in Excel, you first need to access the Visual Basic for Applications (VBA) Editor. Follow these simple steps:

  1. 1. Open Excel and navigate to the workbook containing the Macro you'd like to edit.
  2. 2. Press Alt + F11 to open the VBA Editor.
  3. 3. In the Project Explorer window (usually on the left side), expand the workbook's node and locate the module containing the Macro.

Now that you're in the VBA Editor, you're ready to start editing your Macro.

Step 2: Understand the Structure of Macro Code in Your Workbook

Before making changes to your Macro, it's crucial to understand its structure. VBA macros typically follow this format:

Sub MacroName()
    'Your VBA code goes here
End Sub
  • Sub and End Sub define the beginning and end of the Macro.
  • MacroName is the name of your Macro, which should be descriptive and easy to understand.
  • The VBA code that makes up the Macro is contained within these lines.

Understanding this structure will make it easier to edit your Macro and achieve your desired outcome.

Step 3: Edit Your Macro Code in the Code Window

With a clear understanding of the basics and the code structure, you can dive into editing your Macro.

When you edit the code in the Code Window, here are some common modifications you might want to make:

  1. 1. Add comments to explain the purpose of specific lines of code. Comments in VBA start with an apostrophe ('). For example:
    'This line formats the selected cells as currency.
    Selection.NumberFormat = "$#,##0.00"
  1. 2. Change the order of commands to alter the sequence of actions. For example, you might want to move a line of code that formats cells to the beginning of the Macro to ensure proper formatting before performing calculations.
  2. 3. Modify existing commands to adjust their functionality. For example, you might update the number format in the example above to display percentages instead of currency:
    Selection.NumberFormat = "0.00%"
  1. 4. Add new commands to enhance your Macro's capabilities. For example, you might add a line of code to automatically save the workbook after the Macro has run:
  1. 5. Delete unnecessary commands to streamline your Macro and optimize its performance. Remember to save your changes by clicking the Save button in the VBA Editor or pressing Ctrl + S.

Remember to save your changes by clicking the Save button in the VBA Editor or pressing Ctrl + S.

Step 4: Test Your Edited Macro

After editing your Macro, it's essential to test it to ensure it works as intended.

To run your Macro from Excel:

  1. 1. Return to Excel by pressing Alt + F11 or clicking the Excel icon in the VBA Editor's toolbar.
  2. 2. Press Alt + F8 to open the Macro dialog box.
  3. 3. Select the Macro you edited and click "Run."

If the macro runs successfully and produces the desired outcome, congratulations! You've successfully edited your macro in Excel. If not, revisit your code in the VBA Editor, make adjustments, and test again until you achieve the desired result.

Alternatively, you can run your macro from inside the VBA Editor by clicking on Run > Run Sub/UserForm or pressing F5 on your keyboard when the text cursor is placed inside the macro.

You can also use the Debug toolbar to step through your code line by line, set breakpoints, or watch the values of variables. Visual basic code is something you will pick up over time with more usage.

If you encounter any errors, you can use the Watch window and Immediate window to find and fix them.

Step 5: Save Your Edited Macro

Don't forget to save your edited macro! There's nothing worse than forgetting to save your work after making changes.

You can save your macro in the same workbook where you created it or in a separate workbook that you can use as a macro library.

  • To save your macro in the same workbook, go to File > Save or press Ctrl + S on your keyboard.
  • To save your macro in a different workbook, go to File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm) as the file type.

Once you've saved your Macro, you can close the VBA Editor and return to Excel to start using your edited Macro.

Advanced Editing Techniques and Best Practices

Editing macros in Excel can be fun and rewarding, but it can also be challenging and frustrating at times. Here are some tips and tricks that can help you edit macros more easily and effectively:

  • Use descriptive names for your macros. This will make your code more readable and easier to understand. For example, instead of naming your macro Macro1, name it something like SortDataByDate or FormatReportHeader.
  • Use variables and constants to store and manipulate data. Variables allow you to temporarily store data, such as user inputs or calculated values, and use them throughout your macro. For example:
    Dim UserName As String
    UserName = InputBox("Enter your name:")
    MsgBox "Hello," & UserName & "!"
  • Use consistent naming conventions for your macros, variables, and procedures. This will make your code easier to read and understand, and help prevent errors caused by using the wrong variable or procedure name.
  • Use indentation and spacing to organize your code. This will make your code more neat and structured. You can use the Tab key to indent your code blocks, and use blank lines to separate different sections of your code.
  • Use comments to document your code. Comments are lines of text that are ignored by VBA but can help you and others understand what your code does. You can use comments to explain the purpose of your macro, the logic behind your code, or any special notes or warnings. Comments are always displayed as green text. To write a comment, start with an apostrophe (') followed by your text. For example:
'This comment sits outside the macro

Sub SortDataByDate()
    'This comment sits inside the macro
End Sub

You can also comment part of the line if you put an apostrophe somewhere in the line. In that case, code after the apostrophe will be skipped. For example:

    Sheet1.Range("A1").Value = "Test" 'This bit is a comment and will be skipped

If your comment requires more than one line, use the apostrophe on each line, as the following example illustrates.

    'This comment is too long to fit on a single line, so we break
    'it into two lines. Some comments might need three or more lines.
  • Implement error handling to gracefully handle unexpected situations, such as invalid user inputs or missing data. The On Error statement allows you to specify what the macro should do in case of an error. For example:
Sub MyMacro()

    On Error GoTo ErrorHandler
    ' Your macro code goes here

    Exit Sub


    MsgBox "An error occurred: " & Err.Description

End Sub
  • Organize your code with procedures and functions. Break your macro into smaller, reusable procedures and functions to make it more readable, maintainable, and modular. For example, you might create a separate function to calculate a value and then call that function from within your macro.

Did You Find This Information Helpful?

Congratulations! You've taken a significant step towards unlocking the full potential of Excel by learning how to edit macros.

As you continue to explore the world of VBA and Excel automation, remember these key takeaways:

  1. 1. Understand the purpose of macros and how they can save time, reduce errors, and streamline your workflow.
  2. 2. Access the VBA Editor using Alt + F11 and locate your macro in the Project Explorer.
  3. 3. Familiarize yourself with the code structure to ensure you can confidently edit your macro.
  4. 4. Make modifications by adding comments, changing the order of commands, modifying existing commands, adding new commands, or deleting unnecessary commands.
  5. 5. Test your edited macro to ensure it works as intended, making adjustments as needed.

Remember, practice makes perfect. The more you experiment with macros and VBA, the more proficient you'll become. So, go forth and conquer the world of Excel automation – the sky's the limit!

I hope you enjoyed this article and found it useful. If you have any questions or feedback, check the FAQs and comments section below. I'd love to hear from you. Happy editing!

FAQs: Editing Macros in Excel VBA

Here are some frequently asked questions about editing macros in Excel:

Q: How do I delete a macro in Excel?

A: To delete a macro in Excel, follow these steps:

  • Go to the Developer tab on the Ribbon and click on Macros in the Code group.
  • Select the macro you want to delete from the list and click Delete.
  • Confirm your action by clicking on OK.

Q: How do I copy a macro from one workbook to another?

A: To copy a macro from one workbook to another, you can either copy and paste the text of the Macro or follow these steps to transfer the whole module:

  • Open both workbooks in Excel and save them as macro-enabled workbooks (*.xlsm).
  • Go to the Visual Basic Editor (Alt + F11) and locate the module containing the Macro you want to copy in the Project Explorer window.
  • Right-click on the module and select Export File from the menu. Choose a location and a name for the file and click on Save. This will save your module as a *.bas file.
  • Switch to the workbook where you want to copy the Macro and go to the Visual Basic Editor.
  • Right-click on any folder under the workbook name in the Project Explorer window and select Import File from the menu. Browse to the location where you saved the *.bas file and select it. Click on Open. This will import your module with the Macro into the workbook.

Q: How do I run a macro automatically when I open a workbook?

A: To run a macro automatically when you open a workbook, follow these steps:

  • Go to the Visual Basic Editor (Alt + F11) and double-click on ThisWorkbook under the workbook name in the Project Explorer window.
  • In the code window, select Workbook from the left drop-down list at the top.
  • Select Open from the right drop-down list at the top. This will create an empty procedure called Workbook_Open.
  • Inside this procedure, type the name of the Macro that you want to run, without parentheses. Or use the keyword Call. For example:
Private Sub Workbook_Open()
    SortDataByDate          'Method 1
    Call SortDataByDate     'Method 2
End Sub
  • Save your workbook as a macro-enabled workbook (*.xlsm) and close it.
  • The next time you open your workbook, your Macro will run automatically.

Q: How do I protect my macros from being edited or deleted by others?

A: To protect your macros from being edited or deleted by others, follow these steps:

  • Go to the Visual Basic Editor (Alt + F11), right-click on any folder under the workbook name in the Project Explorer window, and select VBAProject Properties from the menu.
  • In the VBAProject - Project Properties dialog box, go to the Protection tab and check the Lock project for viewing box.
  • Enter a password and confirm it. Click on OK.
  • Save your workbook as a macro-enabled workbook (*.xlsm) and close it.
  • The next time you open your workbook, you will need to enter the password to access the VBA code.

Q: How do I share my macros with others?

A: To share your macros with others, you have a few options:

  • You can send them your macro-enabled workbook (*.xlsm) as an attachment via email or other platforms. They will need to enable macros when they open your workbook to use your macros.
  • You can export your modules as .bas files and send them to others. They will need to import them into their workbooks and save them as macro-enabled workbooks (.xlsm) to use your macros.
  • You can copy and paste your code into a text file or an online platform like Pastebin or GitHub and send them the link. They will need to copy and paste your code into their Visual Basic Editor and save their workbooks as macro-enabled workbooks (*.xlsm) to use your macros.

Author: Victor Chan

Victor expertly teaches Microsoft Excel to people all over the world. He has millions of views of his popular Excel explainer videos on YouTube. These show time-saving shortcuts and real-world applications explained with easy-to-follow visuals.

Victor has over 20 years of experience using Excel as a professional for Big 4 Audit Firm Deloitte and two global tech companies. He knows firsthand that being more productive with Excel can lead to greater job satisfaction and career growth.

Connect on YouTube, LinkedIn, Twitter.

Recommended Posts

Discover the PROVEN Blueprint for transforming your Excel skills, supercharging your productivity, and standing out in your career! My course helps you to learn Excel VBA and save hours of time even if you have zero prior experience with programming.

Solve tricky Excel problems and take your work to the next level! Get customized solutions for your unique needs. Save time and gain insights with truly expert Excel solutions from only $97 per task.

Get a clear overview of your project progress using the Excel project timeline. Use it to communicate the big picture, track task progress, and stay on top of your project goals. Stay organized with our project timeline!

Our cheat sheets provide quick and easy reference to commonly used Excel VBA concepts and code snippets.

Unlock new levels of productivity and efficiency with our cheat sheets, and write VBA code like a pro in no time.


Are you looking to upskill and stay ahead of the curve? Excel is a powerful tool that keeps growing in demand. We round up the best online courses for learning Excel.

Are you looking to up your spreadsheet game? Excel is an invaluable tool that can help you stay organized and save time. From data analysis to budgets, Excel can do it all!

Today, having Excel skills is more critical than ever. Those who know how to use Excel are more likely to find higher-paying jobs. And get promoted faster.


Step up your Excel game! Join our free email newsletter and get updates on how to become more awesome at Excel.