How To Add a procedure to a module using VBA in Microsoft Excel?
Tags: toaddaproceduretoamo, vbamacros
Published: 2021-10-13 02:15:56

---
title: How To Add a procedure to a module using VBA in Microsoft Excel?
published: true
date: 2021-10-12 09:04:22 UTC
tags: ToAddAProcedureToAMo,VBAMacros
canonical_url: https://geekexcel.com/how-to-add-a-procedure-to-a-module-using-vba-in-microsoft-excel/
---

In this tutorial, we will guide you on how to  **add a procedure to a module using VBA ** in Microsoft Excel. 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)**

## Example

- Firstly, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```
Sub InsertProcedureCode(ByVal wb As Workbook, ByVal InsertToModuleName As String)
' inserts new code in module named InsertModuleName in wb
' needs customizing depending on the code to insert
Dim VBCM As CodeModule
Dim InsertLineIndex As Long
    On Error Resume Next
    Set VBCM = wb.VBProject.VBComponents(InsertToModuleName).CodeModule
    If Not VBCM Is Nothing Then
        With VBCM
            InsertLineIndex = .CountOfLines + 1
            ' customize the next lines depending on the code you want to insert
            .InsertLines InsertLineIndex, "Sub NewSubName()" & Chr(13)
            InsertLineIndex = InsertLineIndex + 1
            .InsertLines InsertLineIndex, _
                " Msgbox ""Hello World!"",vbInformation,""Message Box Title""" & Chr(13)
            InsertLineIndex = InsertLineIndex + 1
            .InsertLines InsertLineIndex, "End Sub" & Chr(13)
            ' no need for more customizing
        End With
        Set VBCM = Nothing
    End If
    On Error GoTo 0
End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/10/Save-the-Code-36-1024x517.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- Now, you need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Finally, after **running the macro** , you will receive **output** as **added a procedure to a module** using VBA.

## Closure

We hope that this tutorial gives you guidelines on how to  **add a procedure to a module using VBA**  in Microsoft Excel. 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/) **!!**

**Read Next:**

- **[How To Delete a procedure from a module using VBA in Excel?](https://geekexcel.com/how-to-delete-a-procedure-from-a-module-using-vba-in-excel/)**
- **[Delete a module using VBA in Microsoft Excel](https://geekexcel.com/how-to-delete-a-module-using-vba-in-microsoft-excel/)**
- **[How to Split Column through Excel office VBA? – Easy tricks!!](https://geekexcel.com/how-to-split-column-through-excel-office-vba-easy-tricks/)**
- **[How To Delete module content using VBA in Microsoft Excel?](https://geekexcel.com/how-to-delete-module-content-using-vba-in-microsoft-excel/)**