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
31 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:dallagmm
  • 6
  • 4
13 Comments
 
LVL 17

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:dallagmm
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
 
LVL 17

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:dallagmm
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 17

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:dallagmm
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 17

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 17

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:dallagmm
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 17

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now