How To Import a module from a file using VBA in Microsoft Excel?
Tags: toimportamodulefroma, vbamacros
Published: 2021-10-11 04:14:12

---
title: How To Import a module from a file using VBA in Microsoft Excel?
published: true
date: 2021-10-08 10:40:12 UTC
tags: ToImportAModuleFromA,VBAMacros
canonical_url: https://geekexcel.com/how-to-import-a-module-from-a-file-using-vba-in-microsoft-excel/
---

In this blog post, we are going to see how to  **import a module from a file using VBA**  in Microsoft Excel. Let’s get into this article!! 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 import a module to the active workbook**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Sample-Macro.png)<figcaption>Create a macro</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://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to click on the  **Insert ** and select  **Module** and name it as **MainModule**.

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

- After that, you need to  **copy and paste the code**  given below.

```

Option Explicit
Sub InsertVBComponent(ByVal wb As Workbook, ByVal CompFileName As String)
' Inserts the content of CompFileName as a new component in workbook
' CompFileName must be a valid VBA component suited for
' import (an exported VBA component)
'Checking whether CompFileName file exists
If Dir(CompFileName) <> "" Then
    
    'Ignore errors
    On Error Resume Next
    
    'Inserts component from file
    wb.VBProject.VBComponents.Import CompFileName
    
    On Error GoTo 0
End If
Set wb = Nothing
End Sub
Sub Calling_Procedure()
    'Calling InsertVBComponent procedure
    InsertVBComponent ActiveWorkbook, "C:\Users\Ramandeep\Desktop\Filename.bas"
    
End Sub

```

- You have to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Save-the-Code-28-1024x747.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://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

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

![](https://geekexcel.com/wp-content/uploads/2021/10/Run-the-Code-25.png)<figcaption>Run the Code</figcaption>

- Finally, after **running the macro** , a **new module** will be imported from **Filename.bas** in VBA.

## Verdict

In the above post, you can learn the simple steps ** ** on how to  **import a module from a file using VBA**  in Microsoft Excel. Kindly, share your  **feedback**  in the below comment section. Thanks for visiting **[Geek Excel.](https://geekexcel.com/)** Keep Learning!

**Read Ahead:**

- **[Import Data from Another Worksheet or Text Files in Excel Office 365!!](https://geekexcel.com/import-data-from-another-worksheet-or-text-files-in-excel-office-365/)**
- **[Excel Formulas to Find the First Row Number ~ Useful Tutorial!](https://geekexcel.com/excel-formulas-to-find-first-row-number/)**
- **[Insert Multiple Pictures into Cells Based on Cell Value in Excel Office 365!!](https://geekexcel.com/insert-multiple-pictures-into-cells-based-on-cell-value-in-excel-office-365/)**