+ Reply to Thread
Results 1 to 5 of 5

Thread: Macro to run macros in all files in a specific folder

  1. #1
    Registered User
    Join Date
    01-29-2007
    Posts
    50

    Macro to run macros in all files in a specific folder

    Can someone help me create a macro that i can run in a file that will run all macros in all files in a folder?
    I have a different macro in each file in a folder that is slightly different than the next that i would like to open one sheet and have it to run all macros for each book.

    Thanks

  2. #2
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: Macro to run macros in all files in a specific folder

    You can call a macro in a different workbook with code like the following - this will run the macro called runSort in the file C:\Pilot\test.xlsm. You could also place this code in a new workbook and put it into the workbook_open event that way you only have to open one workbook to run a series of macros.
    Sub runMacros()
    Application.Run "'C:\Pilot\test.xlsm'!runSort"
    End Sub
    You may need to change the macro code in the workbook you refer to in the above code - you may need to make that workbook the active workbook - if you run into problems try adjusting the code in the workbook you are calling to make it the active workbook
    Windows(ThisWorkbook.Name).Activate
    This could get you into a heap of trouble if you try calling a group of workbooks and running the embedded macros - it depends on what the macros you are calling are doing - perhaps placing a timer (Application.OnTime method) between calling each macro would be a good idea - it really depends on what the macros are doing
    Last edited by smuzoen; 02-10-2012 at 07:11 AM.
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated

  3. #3
    Registered User
    Join Date
    01-29-2007
    Posts
    50

    Re: Macro to run macros in all files in a specific folder

    Is this correct?
    All of the files i need to run macros for are in
    c:\documents and settings\rstevens1\Desktop\2012 D R Horton Bidding\Option Pricing\

    the blank workbook i just created and dumped the code into is book1.xls

    I get a runtime error so i must have something wrong.




    Private Sub Workbook_Open()
    Application.Run "'C:\Documents and Settings\rstevens1\Desktop\2012 D R Horton Bidding\Option Pricing\book1.xls'!Macro1Datsmart2" 'You need to change this to the directory and filename plus the macro name
    End Sub
    Thanks
    Last edited by JESSIER4025; 02-10-2012 at 07:22 AM. Reason: wrong filename

  4. #4
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,386

    Re: Macro to run macros in all files in a specific folder

    You could do another thing - Excel has a concept called Personal workbook. You could store all your macros in that book and have a small procedure/macro that will call the rest. Like -
     sub macros_run()
    call xxx
    call yyy
    call zzz 
    end sub
    But you have to ensure that each of the macros that you need to run will call their respective workbooks to do the activities.
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: Macro to run macros in all files in a specific folder

    Try
    Application.Run ("'C:\Documents and Settings\rstevens1\Desktop\2012 D R Horton Bidding\Option Pricing\book1.xls'!Macro1Datsmart2") 'book1.xls must be the name of the file containing the macro Macro1Datsmart2
    otherwise you may need to open the workbook
    Workbooks.Open Filename:="C:\Documents and Settings\rstevens1\Desktop\2012 D R Horton Bidding\Option Pricing\book1.xls" 'The file containing the macro you want to run via book1.xls you created
    Application.Run "book1.xls!Macro1Datsmart2"
    the blank workbook i just created and dumped the code into is book1.xls
    The Application.Run needs to refer to the workbook with the macro NOT the workbook calling the macro. You said you placed the code into book1.xls. The book1.xls part of book1.xls'!Macro1Datsmart2 needs to be the name of the file containing the macro Macro1Datsmart2

    Are you using Excel 2007 or 2003 (your extension xls suggests 2003)?

    As an example in book1.xls the code should be to e.g. run the macro ABC in the file getResults.xlsx
    Application.Run ("'C:\Documents and Settings\rstevens1\Desktop\2012 D R Horton Bidding\Option Pricing\getResults.xlsx'!ABC")
    I hope this makes sense. Make sure the file you create to run the macros in the workbooks in the directory is named differently from any of the workbooks in the directory.
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0