Excel VBA Code

In Excel, we can find VBA under the “Developer” tab for developers. To access the “Developer” 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, a programming language written together.

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

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

Enable Developer Tab

First, you must ensure 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 on the ribbon.

Examples to Use Excel VBA Code

#1 – Paste Code in Module

To write VBA code, you first need to go to the “Developer” tab and click on “Visual Basic.”

  • Next, go to “File.” Click on “Options.” Go to “Customize Ribbon” in excel. Check the “Developer” tab on the right side. Once you select the options, click on “OK.”

You can press the excel shortcut keyPress The Excel Shortcut KeyAn Excel shortcut is a technique of performing a manual task in a quicker way.read more “ALT + F11” to open Visual Basic.

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

Next, go to “Insert” and click on “Module.”

As soon as you click on the “Module,” it will insert it, and you can see the “Module” name. And also, you will see a white plain board on the right.

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

After pasting the code, you need to execute it.

We can conduct the execution process by pressing the shortcut key F5 or clicking the green run button on the top.

It will execute the copied VBA code in your window.

#2 – Save the Workbook with VBA Code

We know you do not need any special introduction to save the Excel file. But when it comes to the VBA code in the Excel workbook, you need one special introduction.

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

It says the workbook you are trying to save contains a VB project, and it cannot save as a normal Excel workbook. So instead, save this workbook as a “Macro-Enabled” workbook.

Click on “Save As” or press the F12 key. You will see below a window.

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

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

#3 – Assign Macro Code to Shapes

Whenever we need to run VBA, we need to return to the visual basic editor and run. It 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 macroExcel MacroA macro in excel is a series of instructions in the form of code that helps automate manual tasks, thereby saving time. Excel executes those instructions in a step-by-step manner on the given data. For example, it can be used to automate repetitive tasks such as summation, cell formatting, information copying, etc. thereby rapidly replacing repetitious operations with a few clicks. read more to shapes.

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

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

Step 3: Right-click and select “Edit Text.”

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

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

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

Step 7: Click on “OK.” Now, this button holds the macro code. Whenever you click on this button, it will execute this code.

This article is a guide to VBA Code. Here, we learn how to copy, insert and run the VBA code in Excel, along with simple and practical examples. Below are some useful Excel articles related to VBA: –

  • Case Statement in VBACase Statement In VBAVBA Case Statement is a function that assists the users to execute multiple logical tests and arrive at results in two ways, i.e. first, if the result or logical test is TRUE and second, if the result or logical test is FALSE.read moreSelect a Range in VBASelect A Range In VBAThe selection range is the desired area by the user to put effects, add data, or make changes in those specific cells only. The range is defined by formulas by the addresses of two cells separated by a colon. The cell where the selection range begins is still active.read moreExcel VBA Save AsExcel VBA Save AsVBA Save As method is used to save the excel file to a specific location. To save the workbook using vba code we use the object Workbook with the SaveAs function.read moreVBA Select Case Statement