VBA Code

What Is Excel VBA Code?

VBA code is a human-readable and editable programming language that is written from scratch, else Excel generates its own code when we record a macro. Using VBA macros, we can automate all difficult tasks in Excel.

VBA is a programming language built by Microsoft for Office Applications. In Excel, we do a lot of manual work on a regular basis. It takes a considerable amount of time and affects productivity. Hence, in Excel, we have an automation coding language called VBA that can automate tasks.

Key Takeaways
  • VBA (Visual Basic for Applications) code is a programming code that we can write, read and edit with ease.
  • Using VBA macros helps us automate tasks and reduce a tremendous amount of time and work.
  • The Record Macro option will record all activities done in Excel and create VBA code.
  • VBA Macro codes are executed line by line.
  • Once the VBA code is generated, we must save the workbook as a “Macro-Enabled Workbook”.

Enable Developer Tab

Visual Basic for Applications is available under the Developer tab in Excel. The following image shows Visual Basic under the Developer tab in Excel.

Vba Code - Developer tab

However, we may not see the Developer tab sometimes. The following steps will guide you to enable the Developer tab.

Step 1: Go to the File tab.

Vba Code - Developer tab - step 1

Step 2: Click on Options.

Vba Code - Developer tab - step 2

Step 3: This will bring the Excel Options window. Click on Customize Excel Ribbon.

Vba Code - Developer tab - step 3

Step 4: Check the Developer tab box under Main Tab.

Vba Code - Developer tab - step 4

Step 5: Click on OK. Now we will be able to see the Developer tab. Under the Developer tab, we have Macros.

Vba Code - Developer tab - step 5

How To Write VBA Code In Excel?

As a beginner in VBA, it is tough to understand the VBA code from scratch. However, we have an option called Record Macro to get our journey going.

Record Macro is a feature available in Excel to record all the activities that we perform in Excel. It is like a recorder that captures all the activities. Recording Macros is available under the Developer tab.

Vba Code - Record Macro

We will show you a simple example of recording a macro and reading the code.

Step 1: Go to the Developer tab and click on the Record Macro option.

Vba Code - Record Macro - step 1

Step 2: This will open the Record Macro window as follows.

Vba Code - Record Macro - step 2

Step 3: Give a name for the macro. While naming a macro, please look into the following criteria.

Vba Code - Record Macro - step 3 - remember
Vba Code - Record Macro - step 3 - Trial macro

Step 4: Click on OK, and the recording starts. Now, Excel will record all the activities and store them in the VBA code.

The first activity we do is to select cell A1.

Vba Code - Record Macro - step 4

Step 5: Enter the text My First Macro in the selected cell.

Vba Code - Record Macro - step 5

Step 6: We are still in cell A1. After entering the text, hit the Enter key, and it will select the below cell i.e., cell A2.

Vba Code - Record Macro - step 6

As we can see, it has selected cell A2.

Step 7: Now, again select cell A1 and apply the following formatting.

Font Name = Times New Roman

Font Size = 14

Font Color = Blue

Font Style = Bold

Vba Code - Record Macro - step 7

Step 8: To get started with VBA, these activities are enough. Click on Stop Recording under the Developer tab.

Vba Code - Record Macro - step 8

Now the recording has stopped, and no activities will be recorded.

Execute The Macro

Step 1: Delete the cell A1 value and select any other value.

Vba Code - execute code - step 1

Step 2: Go to the Developer tab and click on the Macros option.

Vba Code - execute code - step 2

Step 3: This will bring the Macro window with the list of all the macros. Choose the macro name that we have given while recording the macro i.e., Trial_Macro, and click on OK.

Vba Code - execute code - step 3

The moment we click on OK, it will perform all the activities that we did while the recording was on, and we can see the text in cell A1.

Vba Code - execute code - step 3-output

Understand The Code

Recording is done through codes in VBA. To view the codes of all the activities, we must go to the Visual Basic Editor window.

Go to the Developer tab and click on the Visual Basic option.

Vba Code - Visual basic

This will open the Visual Basic Editor window as shown below.

Note: The shortcut keys to open the Visual Basic Editor window is ALT + F11.

Vba Code - Visual basic editor

Double-click on Module1 to see the code.

Vba Code - Module1

At first look, the code may look alien, but let us explain it to you in simple terms.

The first word is Sub Record_Macro.

SUB is the word to start the macro name. Hence, we can see this word in blue color.

The very first thing we did while recording the macro was give a name to the macro i.e., Trial_Macro. So, Excel considers it as the start of the program, so the program starts with the word Sub and is followed by the macro name i.e., Record_Macro.

Vba code - First Action

After the recording started, we selected the cell A1, and Excel recorded it as Range(“A1”).Select

Vba code - Second Action

After we select cell A1, we entered the value My First Macro. It has recorded cell A1 as ActiveCell because the moment we select cell A1 it has become an active cell. VBA treats this as an active cell; hence, cell A1 is an active cell.

Vba code - Third Action

Next, again we have selected cell A1 to format, so VBA code for this is Range(“A1”).Select

Vba code - Fourth Action

After cell A1 is selected, we applied some formatting, so VBA segregated this into multiple parts.

vba code - Other Actions

The several lines of code can be trimmed to a single block. The following is the trimmed code.

Trimmed code

All four formatting tasks are under one block now. So, the overall code reads like this.

overall code

Execute Macro Line By Line To Understand It Better

To understand how VBA macro lines work, we must execute them line by line. Delete the value in cell A1 and select any other cell.

Execute macro Line 1

Open the Visual Basic Editor using the shortcut key ALT + F11, and we will see a code window like the following image.

Execute macro Line 2

Place the cursor inside the macro and press the shortcut key, F8.

Execute macro Line - 1st execution

As we can see, the first line of the macro is highlighted in yellow color. It means this line is about to execute; so, press the F8 key to start the macro running.

Execute macro Line - 2nd execution

The first line of the macro is Range(“A1”).Select, i.e., it will select the cell A1 in the current worksheet. Currently, the active cell is B3; so, press the F8 key to execute this line, and cell A1 will be selected.

Execute macro Line - 3rd execution

The next line will insert the value My First Macros in cell A1. Press the F8 key to execute this line.

Execute macro Line - 4th execution
  • Next, it will again select cell A1. Press the F8 key to execute this line.
  • The next line will initiate the WITH statement for FONT property.
  • It will change the font name to Times New Roman.
Execute macro Line - 5th execution

Next, it will change the font size to 14.

Execute macro Line - 6th execution

Next, the line will change color to BLUE (in VBA Record Macro, the blue color is represented by this number -4165632).

Execute macro Line - 7th execution

Then, the line will make the font Bold.

Execute macro Line - 8th execution

In this way, VBA macro codes are executed line by line.

VBA Code Examples

Example #1: Write Your Own Code From Scratch

Let us guide you to write VBA code from scratch on your own. Assume we need to automate the following things.

  • Task #1: Copy the data from range A1:B5.
  • Task #2: Paste the copied data to range D1:E5.
  • Task #3: Delete the data in range A1:B5.

We have the following data in Excel for writing the code.

Vba Code examples

Next, quickly open the Visual Basic Editor window by pressing the ALT + F11 shortcut keys in Excel.

Step 1: Start the macro by entering the keyword “SUB” followed by the macro name.

Examples 1- step 1

Step 2: Hit the Enter key, and “End Sub” automatically comes up.

Examples 1- step 2

Step 3: Between SUB and END SUB, we must write our code.

Next, the first task we must do is copy the range A1:B5. So, to select this cell range, we nmust use the RANGE property in VBA.

examples 1- step 3

Step 4: Enter the range of cells in double quotes as followed.

examples 1- step 4

Step 5: In the entered range, we must copy, and enter a dot (.) to get the IntelliSense list of properties and methods of the RANGE property.

examples 1- step 5

As soon as we entered the dot VBA IntelliSense list showed us the properties and methods of the RANGE property.

Step 6: Choose copy method from the list.

examples 1- step 6

Step 7: This will copy the range, A1:B5. Next, we need to paste the copied data into range “D1:E5”, so again, enter the RANGE property and enter the paste area.

examples 1- step 7

Step 8: In the given range, we need to paste, so choose the “paste special” method.

examples 1- step 8

Step 9: Task #3 is to delete the values in the range A1:B5. So again, enter the range property and choose the “Clear” method.

Vba Code examples - step 9

Next, by pressing the F5 key to execute this code, and we will get the following result.

Vba Code examples - Output

In this way, we can write our macros from scratch.

Example #2: Hide All Worksheets

The following code will help you to hide all the worksheets except the worksheet “Sheet1”.

Vba Code - Example 2

Copy the above code and paste it into the Visual Basic Editor window.

Example 2 - Code

When you execute this code, it will hide all the worksheets in your workbook except the one named “Sheet1.”

Example #3: Unhide All Worksheets

Similarly, if we want to unhide all the worksheets, then we can use the following code.

Example 3 - Unhide All Worksheets

The above code will make all the hidden worksheets visible.

Example 3 - Unhide code

Saving VBA Macro Workbook

Once the recording macro is done, there is another important thing that we need to do, i.e., save the workbook as a Macro-Enabled Workbook file extension.

After the VBA code creation, if you click on the Save button, we will get the following prompt.

Saving VBA Macro Workbook

Because of the earlier saved file versions, XLSX, we have created the macro code in this file, it is giving a warning message stating, “The following features cannot be saved in macro-enabled workbooks.”

To save it, we need to save the file with the file type “Excel Macro-Enabled Workbook”.

Save File type

Once the file saves in the “Excel Macro-Enabled Workbook” file type, we will see an icon difference between the normal file extension and the macro file extension.

File Extension

XLSM is the macro file, and XLSX is the non-macro file.

Important Things To Note

  • VBA stands for Visual Basic for Applications.
  • The developer tab is hidden by default in Excel.
  • The macro name should not start with a numerical value, and it should not contain any space or special characters except the underscore (_) character.
  • ALT + F11 is the shortcut key to launch the Visual Basic Editor window.
  • Ctrl + Space is the shortcut key to see the IntelliSense list while writing macros from scratch.
  • F8 is the shortcut key to run the macro codes line by line.

Frequently Asked Questions

1. How to View VBA Code in Excel?

Once the recording macro stops, we can view the code in the Visual Basic Editor window. To launch the visual basic editor window, we have 2 ways.

Manual Way: Go to the Developer tab and click on the “Visual Basic” option.

Vba Code - Visual basic

This will launch the visual basic editor window as shown below:

Vba Code - Visual basic editor

Double-click on “Module1” to see the code.

Shortcut Way: We can also use the shortcut key ALT + F11 to launch the visual basic editor window.

2. How to save VBA code in Excel?

Once we have the VBA code in our workbook, we need to save VBA Code in the file with the file type “Excel Macro-Enabled Workbook”.

Use Save As method and choose the Save as type: as “Excel Macro-Enabled Workbook”.

Save File type

We can see the file extension as XLSM for all the “Excel Macro-Enabled Workbook”.

File Extension

3. How to stop VBA code while running?

To stop VBA code from running, we need to press the shortcut key Ctrl + Break key.

4. What unconditionally pauses the Execution of VBA code?

If we want to pause the VBA code from running for a while inside the macro, we need to use the functions Wait and Sleep.

Download Template

This article must be helpful to understand VBA Code, with its formula and examples. You can download the template here to use it instantly.

Recommended Articles

This has been a guide to VBA code. Here we explain how to write, read, and execute the code with step by step examples. You may learn more from the following articles –

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *