---
description: A Macro is a piece of programming code that runs in Excel environment and helps automate routine tasks. Learn step by step example of recording Macros.
title: How to Write Macros in Excel: Full Tutorial
image: https://www.guru99.com/images/how-to-write-macros-in-excel.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Macros in Excel record a sequence of steps and replay them with a single click to automate a repetitive task. This page explains what a macro and VBA are, the macro security basics, and records a working macro that imports and formats a bank receipts CSV file.

* ⏺️ **Core Definition:** An Excel macro records your steps and plays them back as many times as needed, saving time on repetitive work.
* 🧑‍💻 **VBA:** Visual Basic for Applications is the language Excel writes automatically as the macro records, so no coding is required to start.
* 🛠️ **Developer Tab:** Macros live on the Developer tab, which is hidden by default and enabled through Options, Customize Ribbon.
* 🔒 **Security:** Macros are disabled by default because they can be misused, so only run macros from a trusted source.
* 💾 **File Format:** A workbook with macros must be saved in the macro enabled .xlsm format.
* ▶️ **Record and Run:** Record Macro captures the steps, Stop Recording ends it, and the Macros button replays it.

[ Read More ](javascript:void%280%29;) 

![How to Write Macros in Excel](https://www.guru99.com/images/how-to-write-macros-in-excel.png)

## What is an Excel Macro?

**Excel Macro** is a record and playback tool that simply records your Excel steps and the macro will play it back as many times as you want. VBA Macros save time as they automate repetitive tasks. It is a piece of programming code that runs in an Excel environment but you don’t need to be a coder to program macros. Though, you need basic knowledge of VBA to make advanced modifications in the macro.

## Why are Excel Macros Used in Excel?

As humans, we are creatures of habit. There are certain things that we do on a daily basis, every working day. Wouldn’t it be better if there were some magical way of pressing a single button and all of our routine tasks are done? I can hear you say yes. Macro in Excel helps you to achieve that. In a layman’s language, a macro is defined as a recording of your routine steps in Excel that you can replay using a single button.

For example, you are working as a cashier for a water utility company. Some of the customers pay through the bank and at the end of the day, you are required to download the data from the bank and format it in a manner that meets your business requirements.

You can import the data into [Excel](https://www.guru99.com/excel-tutorials.html) and format. The following day you will be required to perform the same ritual. It will soon become boring and tedious. **Macros solve such problems by automating such routine tasks**. You can use a macro to record the steps of

* Importing the data
* Formatting it to meet your business reporting requirements.

## What is VBA in a Layman’s Language?

**VBA is the acronym for Visual Basic for Applications.** It is a programming language that Excel uses to record your steps as you perform routine tasks. You do not need to be a programmer or a very technical person to enjoy the benefits of macros in Excel. Excel has features that automatically generated the source code for you. Read the article on [VBA](https://www.guru99.com/vba-tutorial.html) for more details.

## Excel Macro Basics

**Macros are one of the developer features. By default, the tab for developers is not displayed in Excel. You will need to display it via customize report**

Excel Macros can be used to compromise your system by attackers. By default, they are disabled in Excel. If you need to run macros, you will need to enable running macros and only run macros that you know come from a trusted source

If you want to save Excel macros, then you must save your workbook in a macro-enabled format \*.xlsm

The macro name should not contain any spaces.

Always fill in the description of the macro when creating one. This will help you and others to understand what the macro is doing.

## Step by Step Example of Recording Macros in Excel

Now in this Excel macros tutorial, we will learn how to create a macro in Excel:

We will work with the scenario described in the importance of macros Excel. For this Excel macro tutorial, we will work with the following CSV file to write macros in Excel.

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio111.png)

You can download the above file here

[Download the above CSV File & Macros](https://drive.google.com/uc?export=download&id=1Hfdw4V0SV%5F1dj4su4WDsGgQ2iU2%5F3GA-)

We will create a macro enabled template that will import the above data and format it to meet our business reporting requirements.

### RELATED ARTICLES

* [VBA Controls in Excel: Form & ActiveX ](https://www.guru99.com/vba-operators.html "VBA Controls in Excel: Form & ActiveX")
* [Excel VBA Subroutine: How to Call Sub in VBA with Example ](https://www.guru99.com/vba-functions-subroutine.html "Excel VBA Subroutine: How to Call Sub in VBA with Example")
* [VBA Logical Operators: AND, OR, NOT, IF NOT in Excel VBA ](https://www.guru99.com/vba-logical-operators.html "VBA Logical Operators: AND, OR, NOT, IF NOT in Excel VBA")
* [Excel VBA Function Tutorial: Return, Call, Examples ](https://www.guru99.com/vba-function.html "Excel VBA Function Tutorial: Return, Call, Examples")

## Enable Developer Option

To execute VBA program, you have to have access to developer option in Excel. Enable the developer option as shown in the below Excel macro example and pin it into your main ribbon in Excel.

**Step 1)**Go to main menu “FILE”

Select option “Options.”

[](https://www.guru99.com/images/vba/062416%5F1048%5FWhatisVBA1.png)

[](https://www.guru99.com/images/vba/062416%5F1048%5FWhatisVBA2.png)

**Step 2)** Now another window will open, in that window do following things

* Click on Customize Ribbon
* Mark the checker box for Developer option
* Click on OK button

[](https://www.guru99.com/images/vba/062416%5F1048%5FWhatisVBA3.png)

**Step 3)** Developer Tab

You will now be able to see the DEVELOPER tab in the ribbon

[](https://www.guru99.com/images/vba/062416%5F1048%5FWhatisVBA4.jpg)

**Step 4)** Download CSV

First, we will see how we can create a command button on the spreadsheet and execute the program.

* Create a folder in drive C named Bank Receipts
* Paste the receipts.csv file that you downloaded

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio3.png)

**Step 5)** Record Macro

1. Click on the DEVELOPER tab
2. Click on Record Macro as shown in the image below

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio4.png)

You will get the following dialogue window

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio5.png)

1. Enter ImportBankReceipts as the macro name.
2. Step two will be there by default
3. Enter the description as shown in the above diagram
4. Click on “OK” tab

**Step 6)** Perform Macro Operations/Steps you want to record

* Put the cursor in cell A1
* Click on the DATA tab
* Click on From Text button on the Get External data ribbon bar

You will get the following dialogue window

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio6.png)

1. Go to the local drive where you have stored the CSV file
2. Select the CSV file
3. Click on Import button

You will get the following wizard

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio7.png)

Click on Next button after following the above steps

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio8.png)

Follow the above steps and click on next button

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio9.png)

* Click on Finish button
* Your workbook should now look as follows

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio10.png)

**Step 7)** Format the Data

Make the columns bold, add the grand total and use the SUM function to get the total amount.

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio11.png)

**Step 8)** Stop Recording Macro

Now that we have finished our routine work, we can click on stop recording macro button as shown in the image below

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio12.png)

**Step 9)** Replay the Macro

Before we save our work book, we will need to delete the imported data. We will do this to create a template that we will be copying every time we have new receipts and want to run the ImportBankReceipts macro.

* Highlight all the imported data
* Right click on the highlighted data
* Click on Delete
* Click on save as button
* Save the workbook in a macro enabled format as shown below

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio13.png)

* Make a copy of the newly saved template
* Open it
* Click on DEVELOPER tab
* Click on Macros button

You will get the following dialogue window

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio14.png)

1. Select ImportBankReceipts
2. Highlights the description of your macro
3. Click on Run button

You will get the following data

[](https://www.guru99.com/images/5-2015/050215%5F1033%5FIntroductio15.png)

Congratulations, you just created your first macro in Excel.

## Absolute vs Relative Macro Recording

Before recording, the Use Relative References button on the Developer tab decides how the macro remembers the cells you click. Choosing the right mode is the single most common reason a recorded macro works or fails on new data.

| Mode               | How it records                                                  | Best for                                                           |
| ------------------ | --------------------------------------------------------------- | ------------------------------------------------------------------ |
| Absolute (default) | Records the exact cell address, such as A1, every time          | A task that always starts in the same cell, like importing into A1 |
| Relative           | Records the movement from the current cell, not a fixed address | A task repeated down a list, wherever the cursor happens to be     |

**💡 Tip:** The ImportBankReceipts macro above uses absolute recording, because it always fills the sheet from cell A1\. Switch on Use Relative References before recording when the same steps must run on a different row each time.

## FAQs

⚡ Why can’t I save a macro in a normal .xlsx file?

A standard .xlsx file cannot store macro code. Save the workbook in the macro enabled .xlsm format instead. If you save as .xlsx, Excel warns that the macros will be removed from the file.

🔒 Why are macros disabled by default?

Macro code can run harmful actions, so Excel disables macros by default and shows a security warning. Only enable macros in a file that comes from a source you trust, and check the code if you are unsure.

✏️ How do I edit a recorded macro?

Open the Developer tab, click Macros, select the macro, and choose Edit. This opens the Visual Basic Editor where the recorded steps appear as VBA code that can be changed and saved.

🤖 Can AI write an Excel macro without recording?

Yes. AI features such as Copilot in Excel and code assistants generate VBA from a plain description of the task. The user pastes the code into the Visual Basic Editor and tests it on a copy of the data.

🧠 Can AI explain what a recorded macro does?

Yes. AI assistants read the generated VBA and describe each step in plain language, and suggest ways to make it shorter or safer. The user reviews the explanation and tests any change before using it.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/how-to-write-macros-in-excel.png","url":"https://www.guru99.com/images/how-to-write-macros-in-excel.png","width":"700","height":"250","caption":"How to Write Macros in Excel","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/introduction-to-macros-in-excel.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/vba","name":"VBA"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/introduction-to-macros-in-excel.html","name":"How to Write Macros in Excel: Full Tutorial"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/introduction-to-macros-in-excel.html#webpage","url":"https://www.guru99.com/introduction-to-macros-in-excel.html","name":"How to Write Macros in Excel: Full Tutorial","dateModified":"2026-07-15T13:40:07+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/how-to-write-macros-in-excel.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/introduction-to-macros-in-excel.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/charlotte","name":"Charlotte Miller","description":"I am Charlotte Miller, an Excel VBA Developer with over a decade of first-hand experience.","url":"https://www.guru99.com/author/charlotte","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/charlotte-miller-author.png","url":"https://www.guru99.com/images/charlotte-miller-author.png","caption":"Charlotte Miller","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"VBA","headline":"How to Write Macros in Excel: Full Tutorial","description":"A Macro is a piece of programming code that runs in Excel environment and helps automate routine tasks. Learn step by step example of recording Macros.","keywords":"vba","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/charlotte","name":"Charlotte Miller"},"dateModified":"2026-07-15T13:40:07+05:30","image":{"@id":"https://www.guru99.com/images/how-to-write-macros-in-excel.png"},"copyrightYear":"2026","name":"How to Write Macros in Excel: Full Tutorial","subjectOf":[{"@type":"HowTo","name":"Enable Developer Option","description":"To execute VBA program, you have to have access to developer option in Excel. Enable the developer option as shown below and pin it into your main ribbon in Excel.","step":[{"@type":"HowToStep","name":"Step 1) Go to main menu 'FILE'","text":"Select option 'Options.'","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/vba/062416_1048_WhatisVBA1.png"},"url":"https://www.guru99.com/introduction-to-macros-in-excel.html#step1"},{"@type":"HowToStep","name":"Step 2) Now another window will open, in that window do following things","text":"Click on Customize Ribbon","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/vba/062416_1048_WhatisVBA3.png"},"url":"https://www.guru99.com/introduction-to-macros-in-excel.html#step2"},{"@type":"HowToStep","name":"Step 3) Developer Tab","text":"You will now be able to see the DEVELOPER tab in the ribbon","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/vba/062416_1048_WhatisVBA4.jpg"},"url":"https://www.guru99.com/introduction-to-macros-in-excel.html#step3"},{"@type":"HowToStep","name":"Step 4) Download CSV","text":"First, we will see how we can create a command button on the spreadsheet and execute the program.","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/5-2015/050215_1033_Introductio3.png"},"url":"https://www.guru99.com/introduction-to-macros-in-excel.html#step4"},{"@type":"HowToStep","name":"Step 5) Record Macro","text":"Click on the DEVELOPER tab","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/5-2015/050215_1033_Introductio4.png"},"url":"https://www.guru99.com/introduction-to-macros-in-excel.html#step5"},{"@type":"HowToStep","name":"Step 6) Perform Macro Operations/Steps you want to record","text":"Put the cursor in cell A1","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/5-2015/050215_1033_Introductio6.png"},"url":"https://www.guru99.com/introduction-to-macros-in-excel.html#step6"},{"@type":"HowToStep","name":"Step 7) Format the Data","text":"Make the columns bold, add the grand total and use the SUM function to get the total amount.","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/5-2015/050215_1033_Introductio11.png"},"url":"https://www.guru99.com/introduction-to-macros-in-excel.html#step7"},{"@type":"HowToStep","name":"Step 8) Stop Recording Macro","text":"Now that we have finished our routine work, we can click on stop recording macro button as shown in the image below","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/5-2015/050215_1033_Introductio12.png"},"url":"https://www.guru99.com/introduction-to-macros-in-excel.html#step8"},{"@type":"HowToStep","name":"Step 9) Replay the Macro","text":"Before we save our work book, we will need to delete the imported data. We will do this to create a template that we will be copying every time we have new receipts and want to run the ImportBankReceipts macro.","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/5-2015/050215_1033_Introductio13.png"},"url":"https://www.guru99.com/introduction-to-macros-in-excel.html#step9"}]},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Why can't I save a macro in a normal .xlsx file?","acceptedAnswer":{"@type":"Answer","text":"A standard .xlsx file cannot store macro code. Save the workbook in the macro enabled .xlsm format instead. If you save as .xlsx, Excel warns that the macros will be removed from the file."}},{"@type":"Question","name":"Why are macros disabled by default?","acceptedAnswer":{"@type":"Answer","text":"Macro code can run harmful actions, so Excel disables macros by default and shows a security warning. Only enable macros in a file that comes from a source you trust, and check the code if you are unsure."}},{"@type":"Question","name":"How do I edit a recorded macro?","acceptedAnswer":{"@type":"Answer","text":"Open the Developer tab, click Macros, select the macro, and choose Edit. This opens the Visual Basic Editor where the recorded steps appear as VBA code that can be changed and saved."}},{"@type":"Question","name":"Can AI write an Excel macro without recording?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI features such as Copilot in Excel and code assistants generate VBA from a plain description of the task. The user pastes the code into the Visual Basic Editor and tests it on a copy of the data."}},{"@type":"Question","name":"Can AI explain what a recorded macro does?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI assistants read the generated VBA and describe each step in plain language, and suggest ways to make it shorter or safer. The user reviews the explanation and tests any change before using it."}}]}],"@id":"https://www.guru99.com/introduction-to-macros-in-excel.html#schema-21170","isPartOf":{"@id":"https://www.guru99.com/introduction-to-macros-in-excel.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/introduction-to-macros-in-excel.html#webpage"}}]}
```
