VBA Code

Excel VBA Code

VBA or also known as Visual Basic Applications is a programming language for excel and not only excel but for most of the Microsoft office programs. We can write set of instructions in a visual basic editor which performs certain tasks for us is known as code in VBA.

In Excel, VBA can be found under the developer tab as it is meant for developers. To access the developer’s tab, we need to enable it first, and then we can access it from the tab or by the keyboard shortcut ALT + F11. A combination of codes makes a procedure, which is also known as macros in VBA, so in other words, macros are a set of codes, which is a programming language written together.

If you are new to VBA and you have no idea about it, then this article is for you.

VBA Code

You are free to use this image on your website, templates etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA Code (wallstreetmojo.com)

Enable Developer Tab

The first thing you need to do is to make sure that the developer tab in excelDeveloper Tab In ExcelEnabling the developer tab in excel can help the user perform various functions for VBA, Macros and Add-ins like importing and exporting XML, designing forms, etc. This tab is disabled by default on excel; thus, the user needs to enable it first from the options menu.read more is visible on the ribbon.

Developer Tab

If your excel is not showing the Developer tab, then follow the below steps to enable the developer tab in your excel.

  1. Go to File.


    Step 1

  2. Click on Options.


    Step 2

  3. Go to Customize Ribbon in excelRibbon In ExcelRibbons in Excel 2016 are designed to help you easily locate the command you want to use. Ribbons are organized into logical groups called Tabs, each of which has its own set of functions.read more.


    Step 3

  4. Check the Developer tab on the right side. Once you select the options, click on OK.


    Step 4

Examples to Use Excel VBA Code

#1 – Paste Code in Module

To write VBA code, the first thing you need to do is go to the Developer tab and click on Visual Basic.

Paste VBA Code Step 1

You can also press the excel shortcut key “ALT + F11” to open the Visual Basic.

shortcut for VBA

As you open the visual basic, you will see a window like the below.

Paste VBA Code Step 2

Go to Insert and click on Module.

Paste VBA Code Step 3

As soon as you click on the module, it will be inserted, and you can see the Module name, and also, you will see a white plain board on the right.

Paste VBA Code Step 4

In the white plain board, you need to paste your copied code.

Paste VBA Code Step 5

After pasting the code, you need to execute it.

We can conduct the execution process in two ways one is by pressing the shortcut key F5 or else by clicking the green run button on the top.

Paste VBA Code Step 6

The copied VBA code will be executed in your window.

#2 – Save the Workbook with VBA Code

I know you don’t need any special introduction to save the excel file, but when it comes to the VBA code contained excel workbook, you need one special introduction.

If the workbook is already saved and you just copied VBA code, then you need to change the file extension because when you click on the save option, you will see below alert message.

Alert message

This says the workbook you are trying to save contains a VB project, cannot be saved as a normal excel workbook. You need to save this workbook as a “Macro-Enabled” workbook.

Click on Save As or press the F12 key, you will see below a window.

Save as

Enter the file name as per your wish but select the Save as type “Excel Macro-Enabled Workbook (*.xlsm)

Save as Macro enabled

Click on OK to save the workbook as Macro-Enabled Workbook.

Save

#3 – Assign Macro Code to Shapes

Whenever we need to run VBA, we need to go back to the basic visual editor and run. This is a time-consuming process.

We can assign a macro by its name to one of the shapes. Follow the below steps to assign the excel macro to shapes.

Step 1: Go to Insert and select the shape as per your wish.

Assign macro Step 1

Step 2: After selecting the shape to draw this on your worksheet. Ideally, away from the data.

Assign macro Step 2

Step 3: Right-click and select Edit Text.

Assign macro Step 3

Step 4: Add the word as per your wish. I have added the word “Click Here to Run the Macro.”

Assign macro Step 4

Step 5: Again, right-click and select the option to assign a macro.

Assign macro Step 5

Step 6: Now, we will see all the macro list. Select the macro name you wish to assign.

Assign macro Step 6

Step 7: Click on, OK. Now, this button holds the macro code. Whenever you click on this button, this code will be executed.

Assign macro Step 4

Recommended Articles

This has been a guide to VBA Code. Here we learn how to Copy, Insert & Run the VBA Code in Excel along with simple and practical examplesBelow are some useful excel articles related to VBA –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>