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.


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.


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

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


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: 20 Practical Coding Tips to Master Excel VBA


Download Practice Workbook


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

What is ExcelDemy?

ExcelDemy - Learn Excel & Get Excel Solutions Center provides online Excel training , Excel consultancy services , free Excel tutorials, free support , and free Excel Templates for Excel professionals and businesses. Feel free to contact us with your Excel problems.
Kawser Ahmed
Kawser Ahmed

Kawser Ahmed is a Microsoft Excel Expert, Udemy Course Instructor, Data Analyst, Finance professional, and Chief Editor of ExcelDemy. He is the founder and CEO of SOFTEKO (a software and content marketing company). He has a B.Sc in Electrical and Electronics Engineering. As a Udemy instructor, he offers 8 acclaimed Excel courses, one selected for Udemy Business. A devoted MS Excel enthusiast, Kawser has contributed over 200 articles and reviewed thousands more. His expertise extends to Data Analysis,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo