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
36 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 19

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

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 19

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 19

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 19

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 19

Expert Comment

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

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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