How to record a macro and then how to modify the code in Excel VBA?
Tags: torecordamacro, vbamacros
Published: 2021-09-17 03:36:25

---
title: How to record a macro and then how to modify the code in Excel VBA?
published: true
date: 2021-09-16 11:26:05 UTC
tags: ToRecordAMacro,VBAMacros
canonical_url: https://geekexcel.com/how-to-record-a-macro-and-then-how-to-modify-the-code-in-excel-vba/
---

In this tutorial, we will guide you on how to **record a macro** and then how to **modify the code** in Excel VBA. Let’s get them below!! Get an official version of MS Excel from the following link: **[https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)**

## Write macro for Sum formula in VBE

- Firstly, you have to  **create a sample data** with data in **range A1:C15** , in which column A contains **names** , Column B having the **marks of subject 1** and column C having the **marks of subject 2** and in column D, you have to **return the total marks**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Sample-data-13.png)<figcaption>Sample data</figcaption>

- Here, you need to **record the macro** for returning **total marks in cell D2**.
- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Record Macro**  option from the group of  **Code**  section.

![](https://geekexcel.com/wp-content/uploads/2021/09/Record-Macro.png)<figcaption>Record Macro</figcaption>

- Now, in the **Macro Name box** , you have to write the **name of macro**  **without using any space** and **special character**.
- After that, you need to **write the description about the macro** in description box and the **recording will start now**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Description.png)<figcaption>write the description</figcaption>

- You have to **write the sum formula** in **cell D2**.

```
=SUM(B2:C2)
```

![](https://geekexcel.com/wp-content/uploads/2021/09/Sum-Formula.png)<figcaption>SUM Formula</figcaption>

- Then, you need to **paste the formula** in **range D3:D15**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Paste-the-Formula.png)<figcaption>Paste the Formula</figcaption>

- Now, you have to click on **Stop Recording**.
- To see the **recorded macros** , you need to press key **ALT+F11**.
- Finally, the **recorded code** will be like this, as given below.

```
Sub SUM()
'
'SUM Macro
'Total marks obtain
'
' Keyboard Shortcut: Ctrl+Shift+S
'
Range("D2").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-2]:RC[-1])"
Selection.Copy
Range("C2").Select
Selection.End(xlDown).Select
Range("D14").Select
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Range("D2").Select
End Sub

```

![](https://geekexcel.com/wp-content/uploads/2021/09/Recorded-Code-1.png)<figcaption>Recorded Code</figcaption>

## Write macro for Average formula in VBE

- Firstly, you have to  **create a sample data** with data in **range A1:C15** , in which column A contains **names** , Column B having the **marks of subject 1** and column C having the **marks of subject 2** and in column D, you have to **return the average marks**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Sample-data-14.png)<figcaption>Sample data</figcaption>

- Here, you need to **record the macro** for returning **total marks in cell D2**.
- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Record Macro**  option from the group of  **Code**  section.

![Record Macro](https://geekexcel.com/wp-content/uploads/2021/09/Record-Macro.png)<figcaption>Record Macro</figcaption>

- Now, in the **Macro Name box** , you have to write the **name of macro**  **without using any space** and **special character**.
- After that, you need to **write the description about the macro** in description box and the **recording will start now**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Average-Macro-description.png)<figcaption>Description</figcaption>

- You have to **write the AVERAGE**  **formula** in **cell D2**.

```
=AVERAGE(B2:C2)
```

![](https://geekexcel.com/wp-content/uploads/2021/09/Average-Formula.png)<figcaption>Average Formula</figcaption>

- Then, you need to **paste the formula** in **range D3:D15**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Paste-the-Formula-1.png)<figcaption>Paste the Formula</figcaption>

- Now, you have to click on **Stop Recording**.
- To see the **recorded macros** , you need to press key **ALT+F11**.
- Finally, the **recorded code** will be like this, as given below.

![](https://geekexcel.com/wp-content/uploads/2021/09/Average-Macro.png)<figcaption>Recorded Code</figcaption>

## Conclusion

We hope that this short tutorial gives you guidelines on how to **record a macro** and then how to **modify the code** in Excel VBA. Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Continue Reading:**

- **[Excel Formulas to Find the Same Date in Next Year ~ Simple Guide!!](https://geekexcel.com/excel-formulas-to-find-the-same-date-in-next-year/)**
- **[Enter the Date in Excel Sheet ~ Step-by-Step Procedure!!](https://geekexcel.com/enter-the-date-excel/)**
- **[Find the Nth Largest Value in Excel Office 365 ~ Step-By-Step Procedure!!](https://geekexcel.com/find-the-nth-largest-value-in-excel/)**
- **[How to Extract Text Before Nth Specific Character in Excel?](https://geekexcel.com/extract-text-between-nth-specific-character-excel/)**
- **[Excel Formulas to Extract the Time from Date and Time ~ Quick Method!!](https://geekexcel.com/excel-formulas-to-extract-the-time-from-date-and-time/)**