Solved

How can I insert a macro from an excel sheet to other excel sheets in a specific folder

Posted on 2016-07-27
13
35 Views
Last Modified: 2016-09-26
How can I insert a macro from an excel sheet to other excel sheets in a specific folder (c:\test). Please find the main excel sheet which has the macro (Main.xlsm) and the other excel sheets (Well1.xlsx , Well2.xlsx and Well3.xlsx ) but it could be more. Please I need also to run the macro in each sheet after inserting the macro. Please find attached the main excel file and the other files.

Regards,

Dallag
Main.xlsm
Well1.xlsx
Well2.xlsx
Well3.xlsx
0
Comment
Question by:Mohammed Dallag
  • 6
  • 4
13 Comments
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41731576
You would need to export the code then import it into the other files. These files would also need to be saved as a macro enabled workbook.

I would suggest that the code be saved as an Excel addin which can then be run in any workbook.

The code would need to editing for this purpose but I would be pleased to help you with this.
0
 

Author Comment

by:Mohammed Dallag
ID: 41731603
I just want a code to be run from the first sheet to copy the macro from the same excel file to the other excel files in the directory.

Regards,

Dallag
0
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 41731848
Why not just run the macro over those other files?

' This looks for files that start with "Well" in the file name
    Dim file As Variant
    Dim wb as Workbook
    file = Dir("c:\test\well*")
    While (file <> "")
        Set wb = Workbooks.Open("c:\test\" & file, 0)

      ' run your code here to process wb

     file = Dir
   Wend

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41731852
I've already pointed out a page by one of the leading Excel experts( Chip Pearson) to export and import the code. There is no simple way and I have suggested that you study that article.

The alternative that I have suggested here is much simpler than trying to export & import code. It's actually what an addin is designed for.
0
 

Author Comment

by:Mohammed Dallag
ID: 41732227
ThomasMcA2

I was able to insert the macro on all my files and I need also to run the macro after inserting it in each file. What is the code to run the macro in module11
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41732366
How did you insert the code? To run a macro from another workbook would require something like

 Application.Run ("FileName.xlsm!MacroName")

Open in new window


Add your file name and macro name.

It's a pity that you don't listen to advice from the Experts that you are asking. You are really making hard work of this.
0
 

Author Comment

by:Mohammed Dallag
ID: 41736465
ThomasMcA2

Please check the below macro. If I run it nothing happend first time but if i debug it and stop the code before the close and save as the file manually as XLSM and delete it and run the macro again it will work. Could you please check. All the files are attached.

Regards,

Dallag

Private Sub CommandButton1_Click()

Dim directory As String, fileName As String, fileName2 As String, sheet As Worksheet, total As Integer

Application.ScreenUpdating = False
Application.DisplayAlerts = False

directory = "c:\test\"
fileName = Dir(directory & "*.xlsx??")


Do While fileName <> ""
    Workbooks.Open (directory & fileName)
    Workbooks(fileName).VBProject.VBComponents.Import "c:\test\Module1.bas"
    fileName2 = Left(fileName, 11) & ".xlsm"
    Workbooks(fileName).SaveAs fileName2, FileFormat:=52
    Workbooks(fileName2).Close
    fileName = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Open in new window

import-export4.xlsm
Module1.bas
XXXX_1_1NO1.xlsx
0
 
LVL 18

Accepted Solution

by:
Roy_Cox earned 500 total points
ID: 41745962
You haven't set a reference to the VBA Extensibility library in the import-export file as explained in Chip pearson's article.

set-reference.pngimport-export4.xlsm
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41764118
This question cannot be resolved. I have answered versions of this question probably 3 times .  The OP has been given advice about simpler, more effective methods, links, sample code. The OP will not listen to that advice.
0
 

Author Comment

by:Mohammed Dallag
ID: 41765556
Dear Roy_Cox,

I am still trying to test your last solution without luck. I will try again and let you know. Thank you for your help.

Regards,

Dallag
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41765622
What problems are you having?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question