Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9211411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:17:21+00:00 2026-06-18T01:17:21+00:00

Is there any method to add macro to Excel file programmatically ? I have

  • 0

Is there any method to add macro to Excel file programmatically ?
I have too many Excel files I want to add a macro to them.
Adding manually (by hand) seems impossible.
I need to create a tool to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:17:23+00:00Added an answer on June 18, 2026 at 1:17 am

    Yes, you can do this programatically, you can access the VB Integrated Development Environment through code. The following website are excellent for learning about the VBIDE, I’ve used them to put together this code. If you run WorkbookModuleImporttwo open dialog boxes will pop up, the first asking for workbooks to import the modules into, the second to select the modules for importing.

    Sub WorkbookModuleImport()
        Dim ii As Integer, vFileNames As Variant, vModules As Variant
    
        'We'll use the Application.GetOpenFilename to get a list of all the excel     workbooks we want to import into
        vFileNames = Application.GetOpenFilename(",*.xls;*.xlsx;*.xlsm", , "Select Workbooks to Import Modules To", , True)
        'If the result is not an array it means the cancel button has been pressed
        If Not IsArray(vFileNames) Then Exit Sub
    
        'Use the same method to get all the modules/classes/forms to input
        vModules = Application.GetOpenFilename(",*.cls, *.bas, *.frm", , "Select Modules/Forms/Class Modules to Import", , True)
        If Not IsArray(vModules) Then Exit Sub
    
        'Now loop through all the workbooks to import the modules
        For ii = LBound(vFileNames) To UBound(vFileNames)
            Call ImportModules(VBA.CStr(vFileNames(ii)), vModules)
        Next ii
    
    End Sub
    
    
    
    Public Sub ImportModules(sWorkbookName As String, vModules As Variant)
        Dim cmpComponents As VBIDE.VBComponents, ii As Integer
        Dim wkbTarget As Excel.Workbook
    
        'We need to open the workbook in order to be able to import the code module
         Set wkbTarget = Workbooks.Open(sWorkbookName)
    
        'If the project is protected with a password we can't import so just set tell us in the immediate window
        If wkbTarget.VBProject.Protection = 1 Then
            'Give a message
            Debug.Print wkbTarget.Name & " has a protected project, cannot import module"
            GoTo Cancelline
        End If
    
        'This is where we set the reference to the components of the Visual Basic project
        Set cmpComponents = wkbTarget.VBProject.VBComponents
    
       'Loop through all the modules to import
        For ii = LBound(vModules) To UBound(vModules)
            cmpComponents.Import vModules(ii)
        Next ii
    
    
    Cancelline:
       'If it's in Excel 2007+ format but doesn't already have macros, we'll have to save it as a macro workbook
       If wkbTarget.FileFormat = xlOpenXMLWorkbook Then
           wkbTarget.SaveAs wkbTarget.Name, xlOpenXMLWorkbookMacroEnabled
           wkbTarget.Close SaveChanges:=False
       Else
           'Otherwise, just save the workbook and close it
            wkbTarget.Close SaveChanges:=True
       End If
    
       'I don't trust excel, so set the workbook object to nothing
       Set wkbTarget = Nothing
    End Sub
    

    These webpages are great references:
    http://www.cpearson.com/excel/vbe.aspx and
    http://www.rondebruin.nl/vbaimportexport.htm. I used Ron’s as a starting point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any method available to add IDs like there is for adding a
Is there any method to preview Excel files on XP besides WebBrowser control? How
In my application, I have to add the choicegroup on Canvas.Is there any method
Is there any method to add intellisense to designer-generated classes and properties in LINQ
Is there any way I can add a static extension method to a class.
Is there any method to rename a div element. I have a div with
Is there any method like the array_unique for objects? I have a bunch of
Is there any method in R to find out what data-sets have been attached.
I want to ask a question that, Is there any method to design a
in OpenCV 2 and later there is method Mat::resize that let's you add any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.