+ Reply to Thread
Results 1 to 10 of 10

Run Macro on all files in folder

  1. #1
    Registered User
    Join Date
    02-02-2010
    Location
    Brisbane
    MS-Off Ver
    Excel 2003
    Posts
    7

    Run Macro on all files in folder

    Hello guys,

    The macro below that I have adapted from the net was used to run a set of macro on all files located in a specified folder.

    It used to work beautifully when I was still using Windows XP and Excel 2003. However, I have recently upgraded to Windows 7 64 bits and Excel 2007 and the macro no longer works.

    It simply does not run the macro on all the files as specified in the .LookIn field.

    I also read somewhere that the .FileSearch function no longer works in Excel 2007 too.

    I would be greatly appreciated if someone could modify the code below to make it work on Windows 7 and Excel 2007 again.

    Preferably, I would like to add another feature that allows me to specific which folder to run the macro on.

    Many thanks all!

    Temjin

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Run Macro on all files in folder

    Temjin,

    Application.FileSearch is not available in Excel 2007


    The following may help you get started in the right direction:

    Sub fList()
    By Peter_SSs
    http://www.mrexcel.com/forum/showthr...ht=fList%28%29

    By RichardSchollar
    http://www.ozgrid.com/forum/showpost...73&postcount=4

    John Walkenbach has some code here:
    http://www.thecodecage.com/forumz/ex...directory.html

    In earlier versions Application.FileSearch could be used to loop through all the files contained in a Directory. This does not appear to work in 2007 versions. I have managed to use Dir to achieve this instead
    ' DateTime : 09/05/2007 08:43
    ' Author : Roy Cox (royUK)'
    http://excel-it.com/Excel%202007/2007_filesearch.htm


    Sub getLatest()
    By Weaver
    http://www.mrexcel.com/forum/showthread.php?t=462360
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Registered User
    Join Date
    02-02-2010
    Location
    Brisbane
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Run Macro on all files in folder

    Thanks a lot Stan, however, I am still having trouble executing the code. It doesn't do anything.

    ' DateTime : 09/05/2007 08:43
    ' Author : Roy Cox (royUK)'
    ' Purpose : Open all worksheets in a specific folder' Disclaimer; This code is offered as is with no guarantees. You may use it in your' projects but please leave this header intact.
    '---------------------------------------------------------------------------------------
    Option Explicit
    Sub Open_All_Files()
    Dim oWbk As Workbook
    Dim sFil As String
    Dim sPath As String

    sPath = "C:\Documents and Settings\Roy Cox\My Documents\" 'location of files
    ChDir sPath
    sFil = Dir("*.xlsx") 'change or add formats
    Do While sFil <> "" 'will start LOOP until all files in folder sPath have been looped through
    Set oWbk = Workbooks.Open(sPath & "\" & sFil) 'opens the file

    ' do something
    oWbk.Close True 'close the workbook, saving changes
    sFil = Dir
    Loop ' End of LOOP
    End Sub
    I'm pretty sure I've entered the path correctly, and the extension of the excel files there matches the format specified in the code.

    I've done a bit of googling online as well and I'm trying to play with some of the codes in this thread.

    http://social.msdn.microsoft.com/for...-03f7994369d6/

    My preference would be for the ability to execute the "line of codes" through subfolders as well. But my first priority is to get it to work initially.

    And I still don't know why it doesn't. More help would be appreciated for a newbie like me.

    Many thanks!

    Temjin

  4. #4
    Registered User
    Join Date
    02-02-2010
    Location
    Brisbane
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Run Macro on all files in folder

    Would be great if someone could help me out on this. Really need to get this working.

    Many thanks

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Run Macro on all files in folder

    Hi Temjin,

    See how the this goes - just change the MyMacro code to suit your needs (it currently just displays which file has the focus):

    Please Login or Register  to view this content.
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Run Macro on all files in folder

    Hi,

    I had a similar issue with XL 2007 under Vista. You might want to check out the solution at http://www.excelforum.com/excel-prog...not-found.html. It might help if you run into a similar problem looping through the files.

    abousetta

  7. #7
    Registered User
    Join Date
    06-28-2011
    Location
    Spain
    MS-Off Ver
    Excel 2003
    Posts
    29

    Re: Run Macro on all files in folder

    Hello,

    I just tried Robert's code in 2007 and it works perfectly. Thanks Robert!

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Run Macro on all files in folder

    or

    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    Last edited by snb; 08-26-2011 at 06:41 AM.



  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Run Macro on all files in folder

    Hi vzc8,

    Thanks for the feedback and for adding to my reputation level.

    Kind regards,

    Robert

  10. #10
    Registered User
    Join Date
    09-21-2014
    Location
    Stockholm, Sweden
    MS-Off Ver
    2010
    Posts
    10

    Re: Run Macro on all files in folder

    Hi

    I'm having the same problem, I have a similar code to the one you wrote but my problem is that am not getting the code to copy all ranges to the workbook.
    It goes through all files with a specifik name, but for each file i want to copy a specifik range from the closed workbook to my active workbook. How can I do it?

+ 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.6.0 RC 1