?
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
Medium Priority
?
38 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
13 Comments
 
LVL 21

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 21

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 21

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 21

Accepted Solution

by:
Roy Cox earned 2000 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 21

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 21

Expert Comment

by:Roy Cox
ID: 41765622
What problems are you having?
0

Featured Post

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.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

777 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