How to Run VBA Code in Your Excel

VBA is an excellent tool in Microsoft Office suite, including MS Excel application. It can assist you to accomplish a lot of tasks not supported by the native features in Excel. Now, in this article, we will teach you to run VBA code in Excel step by step.

If you intend to write some VBA codes to achieve some of your requirements, you have to firstly figure out how to run VBA code in your Excel. Now, thereinafter, we will use MS Excel 2010 for an instance to show the detailed steps, including how to enable macro, trigger VBA editor and several means to run macro, etc.

Step 1: Show “Developer” Ribbon

  1. First off, launch Excel application.
  2. Then, go to “File” menu and click “Options”.Access "Excel Options"
  3. In the popup “Excel Options” window, turn to “Customize Ribbon” tab.
  4. Next, in the right side, find and check the “Developer” option.Enable "Developer" Ribbon
  5. Finally, click “OK” to enable the modifications.

Step 2: Change Macro Security Settings

Since Excel comes with macro auto disabled in security settings, you need to first change the settings by following the steps below:

  1. At first, in Excel main window, switch to “Developer” ribbon.
  2. Then, click “Macro Security” button in “Code” group.
  3. Next, in the new window, select “Disable all macros except digitally signed macros” or “Enable all macros” option to your liking.Change Macro Security Settings
  4. Lastly, click “OK” to save the changes.

Step 3: Trigger Excel VBA Editor

Excel VBA editor is the place where you put the code. You can get access to it by 2 means.

  1. On “Developer” tab, click the “Visual Basic” button in “Code” group.Trigger Excel VBA Editor
  2. Directly press “Alt + F11” key shortcuts to bring up “Microsoft Visual Basic for Applications” window.

Step 4: Add VBA Code

After entering Excel VBA editor, you need to add your code into either a project or a module.

1. Put Code into Existing Project

As you can see, under expanded “Microsoft Excel Objects” folder, you can see the existing projects, including the projects for each worksheet and the one for the current workbook called “ThisWorkbook”.Existing Projects

  1. To begin with, double click on a project to open its window.
  2. Then, put the code into it.Put Code in Project

2. Put Code into Module

Besides, you can add code to a module by the following steps.

  1. In the first place, click “Insert” button in toolbar.
  2. From the drop down list, select “Module”.Insert Module
  3. Then, in the new module, you can put your code.

Step 5: Run VBA Code

1. Run Macro in VBA Editor

To run VBA in the “Microsoft Visual Basic for Applications” window, you can just press “F5” key button or click the “Run” icon in the toolbar.Run Macro

2. Run Macro through Quick Access Toolbar (QAT)

Moreover, you can add macro to Quick Access Toolbar and run it through QAT by the steps below.

  1. At the outset, go to “File” > “Options”.
  2. In the popup window, access “Quick Access Toolbar” tab.
  3. Then, select “Macros” from the list of “Choose commands from”.Choose Commands from "Macros"
  4. Next, choose the macro in the left side.
  5. Later, click “Add” button in center.Add Macro to Quick Access Toolbar
  6. After that, click “OK” to save the changes to Quick Access Toolbar.
  7. Finally, in the main Excel window, to run this macro, you can click the macro button in Quick Access Toolbar, as shown in the following screenshot.Run Macro Through Quick Access Toolbar

3. Run Macro through Ribbon

In addition, like Quick Access Toolbar, you can add and run macro in ribbon.

  1. For a start, access “File” menu and hit “Options”.
  2. In “Excel Options”, go to “Customize Ribbon”.
  3. Then, create a new group on “Home” tab by “New Group”.
  4. Optionally, you can rename this group by “Rename…” button.Create Custom Group in Ribbon
  5. Subsequently, select “Macros” in the list of “Choose commands from”.
  6. After that, pitch on the desired macro in the left side.
  7. Next, click the “Add” button to add this macro to the new group.Add Macro to Custom Group in Ribbon
  8. Eventually, click “OK” to save options.
  9. Since then, to run this macro, you can click the newly added macro button in ribbon.Run Macro in Ribbon

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including mdf recovery and outlook repair software products. For more information visit www.datanumen.com

Comments are closed.