Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
41 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 22

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
Technology Partners: 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 22

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 22

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 22

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 22

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 22

Expert Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

618 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