Table of Contents

Introduction

Welcome to a comprehensive guide of “write your first macro in Excel”. In this tutorial, we will take sample data, add formula, and finally format the data to make it neat and clean, like a professional report. All of these tasks will be recorded in a macro, and we will try to run the same code on other sheets as well. This may require a few changes to the code, we will try to find the issues with the code and try to fix them. The objective of this article is to give a start to our new developers and help them to understand how you can use VBA macros without much knowledge of programming.

Understanding the Scenario

Before we get into the technical stuff, let’s clarify what we’re aiming to do. Our goal is to make formatting data in Excel easier. This means making headers look the same, adding borders where needed, and calculating averages across different sheets. By automating these tasks with a macro, we can make our work more consistent and save ourselves a lot of time.

Before

Write Your First Macro in Excel

After

Write Your First Macro in Excel

Using Shortcuts for Efficiency

When working with macros, especially when recording. You need to ensure that wherever possible, you need to use Excel shortcuts. In this example, we have used Excel Shortcuts like Ctrl + Down to move the cell selection to the bottom or Ctrl + Shift + Down to select entire data. There are many other Excel shortcuts available which you can also use while recording the macro. You can fin the most use Excel Shortcuts at https://www.excelsirji.com/excel-shortcuts-keys/

Recording the Macro

Let’s kick things off by recording our macro in Excel. Go to the “View” tab, click on “Macros,” and select “Record Macro.”

Write Your First Macro in Excel

Give your macro a name, like “Format_Sheet” and choose a shortcut, such as “Ctrl + T”

Write Your First Macro in Excel

Adding Average Formula

The very first thing we will record is to add average formula in the sheet. For that, we will select cell I1 and add header as ‘Average’.

Write Your First Macro in Excel

Let’s add the formula “=AVERAGE(D2:H2)” in cell I2. This will bring the average of Day 1 to Day 5 in average column.

Write Your First Macro in Excel

Drag the formula till the end of the data, here it is row 11. For this, we will first copy the formula and use Excel Shortcuts Ctrl + Down Key to go till the end of data.

Write Your First Macro in Excel
Write Your First Macro in Excel

Format Header

Now lets’ format header, for that we will select the header from cell A1 to I1 and change it color to light blue.

Write Your First Macro in Excel

Also change the font color to white to make it more readable.

Write Your First Macro in Excel

Adding Borders

Again let’s use Excel Shortcut Ctrl+Shift+Down key to select the entire data range including header.

Write Your First Macro in Excel

Now add borders to the selected range. Border option can be found in Home menu.

Write Your First Macro in Excel

Final Formatting Steps – Alignment and Font

We are now left with the final steps of formatting. Let’s center align all the cells.

Write Your First Macro in Excel

And change the font to Calibri 11.

Write Your First Macro in Excel

Stopping Macro Recording

Once we’ve finished our actions, it’s time to stop the macro recording. Simply go back to the “View” tab, click on “Macros,” and select “Stop Recording.”

Write Your First Macro in Excel

Understand the Code Generated from Recording

Let’s understand the code which got generated while recording. For that we will click on View>Macros>View Macros.

Write Your First Macro in Excel

Select the macro you have recorded and click on Edit

Write Your First Macro in Excel

You will be able to see the code which is generated while you were performing the steps.

Write Your First Macro in Excel

If we break the code, you will be able to understand it better. Like first 4 lines of code are the steps where we selected I1 cell, typed Average as header, selected A2 cells and enter the formula as “=AVERAGE(D2:H2)”.

Write Your First Macro in Excel

Here you may notice that the formula which got recorded is different than what we typed. The reason is while recording, Excel uses relative position instead of absolute position. Like in the recorded formula you will see RC[-5]. Here R means Rows and C means columns. RC[-5] represents -5 columns from current position. We were on cell I2 (check line 3 of the highlighted code) and -5 columns takes us to column D2. Same way RC[-1] takes us to column H2.

The next lines of code got generated when we selected I2 cell, copied the formula. Selected H2 cell. Pressed Ctrl + Down key, selected I11 cell, used Ctrl + Shift + Up key and pasted the formula.

Write Your First Macro in Excel

The next line of codes represents selecting header row, change it color to light blue and change it’s font color to white.

Write Your First Macro in Excel

The long code after that is for changing the cell borders. The first line of highlighted code represents using Ctrl + Shift + Down key. After that rest code is for changing border of the selected range.

Write Your First Macro in Excel

Finally the last part of the code represents changing font name to Calibri and size to 11.

Write Your First Macro in Excel

Testing and Debugging

Now it’s time to put our macro to the test. Apply it to another sheet and see if it works as expected. If there are any issues, like hardcoded selections or formatting errors, we’ll need to debug the macro by tweaking the code.

For testing, we will go to second sheet where cell formatting is not yet done. Then will open the view macros dialog.

Write Your First Macro in Excel

Select the macro you have recorded and click on Run

Write Your First Macro in Excel

You will notice that macro has performed most of the actions; however it has missed dragging the formula till the end.

Write Your First Macro in Excel

The issue is caused due to few steps got recorded with absolute cell reference. Like in below code where we selected cell I11 and pressed Ctrl + Shift + Up key. Because we selected cell I11, the code will never select entire range (on second sheet it was suppose to be till row 16).

Write Your First Macro in Excel

To fix it, we need to change the code and make it dynamic. So instead of selecting I11 cell, we will select the same row which got selected from earlier step (Selection.End(xlDown).Select). In the highlighted code 11 is changed with Selection.Row.

Write Your First Macro in Excel

Now let’s re-run the code after deleting column I in the second sheet.

Write Your First Macro in Excel
Write Your First Macro in Excel

Now the output is as per our expectation and the Average formula is dragged till the end.

Write Your First Macro in Excel

Patch New Actions in Existing Macro

In case you want to add some patch code in the main code. You can record it separately and copy the code at the relevant place in main code.

For example, let’s try to record another macro just to change the format Average column from 3 decimal places to 2 decimal places.

Write Your First Macro in Excel

The recorded code generated for changing the cell format to 2 decimal places look like below.

Write Your First Macro in Excel

Let’s patch this code as last step in the main code “Format_Sheet”

Write Your First Macro in Excel

With this, we got our final code that can be used to format the data in the desirable format.

Conclusion

Congratulations! You’ve successfully written your first macro in Excel. By automating repetitive tasks, macros enhance efficiency and productivity. Remember, practice and experimentation are key to mastering VBA macros. Explore further possibilities and keep refining your skills. Happy macro coding!

Download Practice File

You can also practice this through our practice files. Click on the below link to download the practice file.

Similar Posts

Leave a Reply

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