How To Create a VBA Macro Or Script In Excel

by David Trounce

Microsoft Excel enables users to automate features and commands using macros and Visual Basic for Applications (VBA) scripting. VBA is the programming language Excel uses to create macros. It will also execute automated commands based on specific conditions.

Macros are a series of pre-recorded commands. They run automatically when a specific command is given. If you have tasks in Microsoft Excel that you repeatedly do, such as accounting, project management, or payroll, automating these processes can save a lot of time.

Table of Contents

    Under the Developer tab on the Ribbon in Excel, users can record mouse clicks and keystrokes (macros).  However, some functions require more in-depth scripting than macros can provide. This is where VBA scripting becomes a huge benefit. It allows users to create more complex scripts.

    In this article, we will explain the following:

    Enabling Scripts & Macros

    Before you can create macros or VBA scripts in Excel, you must enable the Developer tab on the Ribbon menu. The Developer tab is not enabled by default. To enable it:

    The reason macros are not turned on by default and come with a warning is that they are computer code that could contain malware.

    Make sure the document is from a trusted source if you are working on a shared project in Excel and other Microsoft programs.

    When you are done using your scripts and macros, disable all macros to prevent potentially malicious code from infecting other documents.

    Create a Macro in Excel

    All the actions you take in Excel while recording a macro are added to it. 

    Decide where you want to store the macro from the following options:

    When done, click OK

    Specific Example Of a Macro

    Let’s start with a simple spreadsheet for customers and how much they owe. We will begin by creating a macro to format the worksheet.

    Let’s assume that you decide that all spreadsheets should use a different format such as putting first and last name in separate columns. 

    You can manually change this. Or you can create a program using a macro to automatically format it correctly for you.

    Record The Macro

    This will highlight the cells that have a balance due. We added a few customers with no balance due to further illustrate the formatting.  

    Apply The Macro

    Let’s start with the original spreadsheet before we recorded the macro to format it correctly. Click on Macros, select and Run the macro you just created.

    When you run a macro, all the formatting is done for you. This macro we just created is stored in the Visual Basic Editor.

    Users can run macros in several different ways. Read Run a macro to learn more.  

    Learn More About VBA

    To learn about VBA, click on Macro from the Developer tab. Find one you have created and click Edit.

    The code you see in the box above is what was created when you recorded your macro. 

    It is also what you will run when you want to format other customer payment spreadsheets in the same way.

    Create a Button To Get Started With VBA

    Using the same spreadsheet above with customers and how much they owe, let’s create a currency converter.

    Add Code To Give The Button Functionality

    VBA coding doesn’t take place in the Excel interface. It is done in a separate environment.  

    ActiveCell.Value = (ActiveCell * 1.28)

    Our purpose in this section is to convert the currency in our spreadsheet. The script above reflects the exchange rate from GBP to USD. The new value of a cell will be what is currently there multiplied by 1.28.

    The screenshot below shows you how the code looks in the VBA window after you insert it .

    Did It Work?

    Before you can test your code, you must first disable Design Mode (click on it) to avoid further modifications and give the button functionality.

    For this example, I put the number 4 into a cell. After clicking Convert, the number changed to 5.12. Since 4 times 1.28 is 5.12, the code was carried out correctly.

    Now that you understand how to create a macro or script in Excel, you can use them to automate a multitude of actions in Excel.

    Exit mobile version