How To Create a new module using VBA in Microsoft Excel?
Tags: tocreateanewmodule, vbamacros
Published: 2021-10-19 04:44:01

---
title: How To Create a new module using VBA in Microsoft Excel?
published: true
date: 2021-10-15 11:06:06 UTC
tags: ToCreateANewModule,VBAMacros
canonical_url: https://geekexcel.com/how-to-create-a-new-module-using-vba-in-microsoft-excel/
---

In this tutorial, we will guide you on how to ** create a new 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, you need to  **create a macro to insert a new module** in an Excel workbook.

![](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/10/Sample-data-21.png)<figcaption>Sample data</figcaption>

- 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 need to  **copy and paste the code**  given below.

```
Option Explicit
Sub CreateNewModule(ByVal ModuleTypeIndex As Integer, ByVal NewModuleName As String)
'Declaring variables
Dim ModuleComponent As VBComponent
Dim WBook As Workbook
'Creating object of active workbook
Set WBook = ActiveWorkbook
Set ModuleComponent = Nothing 
On Error Resume Next
'Adding new module component
Set ModuleComponent = WBook.VBProject.VBComponents.Add(ModuleTypeIndex)
If Not ModuleComponent Is Nothing Then
    'Renaming the new module
    ModuleComponent.Name = NewModuleName
End If
On Error GoTo 0
Set ModuleComponent = Nothing
End Sub
Sub CallingProcedure()
'Declare variables
Dim ModuleTypeConst As Integer
Dim ModuleName As String
'Getting value of module name and type of module
ModuleTypeConst = CInt(Range("D12").Value)
ModuleName = Sheet1.TextBox2.Value
'Calling CreateNewModule
CreateNewModule ModuleTypeConst, ModuleName
End Sub

```

- You have 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-35-1024x953.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- Then, 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>

- Before **running the macro** , you need to specify the **type and name of the module**.

![](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/10/Enter-the-type-1.png)<figcaption>Enter the type and name of the module</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

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

- Finally, after  **running the macro** , you will receive **output** as **class module is added to the workbook**.

## Wrap-Up

We hope that this tutorial gives you guidelines on how to  **create a new 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/) **!!**

**Keep Reading:**

- **[How to Modify and Manage Multiple Cell Comments in Excel Office 365?](https://geekexcel.com/how-to-modify-and-manage-multiple-cell-comments-in-excel-office-365/)**
- **[Remove/Add Author Name in Comments in Excel 365](https://geekexcel.com/how-to-remove-add-author-name-in-comments-in-excel-365/)**
- **[How to Create User Defined Function through Excel VBA?](https://geekexcel.com/how-to-create-user-defined-function-through-excel-vba/)**
- **[Extract the Missing Values through Excel VBA](https://geekexcel.com/how-to-extract-the-missing-values-through-excel-vba/)**
- **[How to use Loan Calculator through Excel Office VBA?](https://geekexcel.com/how-to-use-loan-calculator-through-excel-office-vba/)**