How to Write VBA Code in Excel (With Easy Steps)

Get FREE Advanced Excel Exercises with Solutions!

Visual Basic Analysis is referred to as VBA. Excel VBA is the programming language for Office applications such as MS-Excel, MS-Word, and MS-Access. This is a very effective tool for working in Excel. In this article, we will show how to write VBA code in Excel.


Download Practice Workbook


What Is VBA Code?

We use VBA to write macros that automate simple and complex tasks in Excel. Microsoft Excel users frequently find themselves performing the same set of tasks repeatedly. These tasks can vary in complexity, ranging from simple copy-and-paste operations to more involved ones like data uploads to websites.  We can make these types of tasks to be automated using VBA macros, which reduce the number of manual steps required to one button press.


Step-by-Step Procedures to Write VBA Code in Excel

In this article, we will discuss the step-by-step procedure for entering and editing a VBA code in Excel. We will use the following steps to illustrate the procedures. This is our sample dataset.

how to write vba code in excel


Step 1: Opening Visual Basic Tab

In the first step, we will open the Visual Basic Tab. This is the environment where we write our code.

  • Firstly, go to the Developer tab in the ribbon.
  • From there, select the Visual Basic tab.
  • Consequently, the Visual Basic window will be opened.

opening visual basic window to write vba code in excel


Step 2: Inserting Module

In the second step, we will insert a module to write down the code.

  • After that, in the Visual Basic tab, click on Insert.
  • Then, select the Module option.
  • Consequently, a coding module will appear.

inserting module to write vba code in excel


Similar Readings


Step 3: Writing and Saving Code

A text editor is similar to a VBA module. It is shown in a different window (the Code window). We can choose the text, copy and cut, add and paste, move around the sheet, and can perform other operations on it.

  • Then, write down the code in the module.
  • After that, save the code.


Step 4: Running Code

  • Finally, click on the Run button.
  • To run the code, choose the Run command from the drop-down menu.

In this case, we will write a simple code that tells by looking at a number whether it is an odd or an even number.

  • Consequently, Excel will execute your code.

Read More: How to Make VBA Code Run Faster (15 Suitable Ways)


Conclusion

VBA codes are really effective tools to solve any problem in Excel. After going through this article, users will be able to write and edit their VBA code properly in excel. This will allow them to automate the repetitive tasks and make their life easier. They can perform mammoth tasks with the click of a button by using VBA codes.


Related Articles

Tags:

Khan Muhammad Kawser
Khan Muhammad Kawser

Hello! Welcome to my Excel blog! It took me some time to be a fan of Excel. But now I am a die-hard fan of MS Excel. I learn new ways of doing things with Excel and share them here. Not only a how-to guide on Excel, but you will get also topics on Finance, Statistics, Data Analysis, and BI. Stay tuned! You can check out my courses at Udemy: udemy.com/user/exceldemy/

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo