+ Reply to Thread
Results 1 to 10 of 10

Looping thru files in folder

  1. #1
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    826

    Looping thru files in folder

    What would be the easiest code to just loop thru all excel books in a folder.

    Open them, do stuff, copy the data, into another book, close without saving and go to the next file in the folder.

    Be aware the files are not named in such a way that I can control the sequence.
    Just if there 10 files, open the first in the row, then the second etc.

    And the do stuff, copy, paste is already in different macro.
    Just need to cycle thru the files call that macro and close them again without saving.

    ps: the do stuff needs to take place in the openend file.

  2. #2
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Looping thru files in folder

    You should take a look at the dir() function in VBA, Its likely the simplest way to loop files in a folder. It can be done recursively as well. Otherwise using a file system object (fso) to loop the files in a folder or sub folder.
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,277

    Re: Looping thru files in folder

    This should get you started.
    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Looping thru files in folder

    Alternative to VB Script using VBA
    If the workbook hosting the code is in the same folder the code below would open each file for potential action. If the host workbook is not in the same folder, you would need to substitute the actual path for 'ThisWorkbook.Path' as a string, eg. "C:\User\Document\xl"
    Please Login or Register  to view this content.
    This is very generic, but you can add code to spice it up if needed.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  5. #5
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: Looping thru files in folder

    Good using create object

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,760

    Re: Looping thru files in folder

    Quote Originally Posted by rpinxt View Post
    Be aware the files are not named in such a way that I can control the sequence.
    You've got some pretty good answers here so I won't muddy the waters there. I will add, however, that you can't control the sequence anyway. The order in which files are in a Windows directory are undefined. If you loop through all the files you may detect a pattern but Windows does not guarantee for any given path that you can predict what the sequence will be.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  7. #7
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Looping thru files in folder

    Quote Originally Posted by 6StringJazzer View Post
    You've got some pretty good answers here so I won't muddy the waters there. I will add, however, that you can't control the sequence anyway. The order in which files are in a Windows directory are undefined. If you loop through all the files you may detect a pattern but Windows does not guarantee for any given path that you can predict what the sequence will be.
    I believe the windows default is numeric-alphabetically sorted in the directory. The problem in most cases is the inconsistency of the user entries in formatting the names.
    Last edited by JLGWhiz; 02-21-2018 at 02:27 PM.

  8. #8
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: Looping thru files in folder

    If you did need to open them in order, you would store the file names into an array, collection or dictionary. Sort the construct and then loop the construct to open the files. As mentioned naturally looping the files could give you an unexpected order of the files, which may be fine for most uses.

  9. #9
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    826

    Re: Looping thru files in folder

    Quote Originally Posted by 6StringJazzer View Post
    You've got some pretty good answers here so I won't muddy the waters there. I will add, however, that you can't control the sequence anyway. The order in which files are in a Windows directory are undefined. If you loop through all the files you may detect a pattern but Windows does not guarantee for any given path that you can predict what the sequence will be.
    Well with controlling the sequence I ment that is is not like File1, File2, File3...
    Then I could use a for i = 1 to 10 and just let it open path.File & i & .xlsx etc.

    But I indeed got enough good suggestions here.
    Thanks everybody!

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,277

    Re: Looping thru files in folder

    Glad to help. thanks for rep+.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Looping through specific files from a Folder
    By Saarang84 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-18-2015, 11:20 AM
  2. looping macro for all files in a specified folder
    By Robo25 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-12-2013, 05:08 AM
  3. Looping through a folder of files in VBA
    By lizzo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2013, 05:29 PM
  4. looping through files in a folder
    By bored in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2012, 02:20 AM
  5. Looping through files in a folder
    By pdb78 in forum Excel General
    Replies: 4
    Last Post: 01-27-2012, 12:16 PM
  6. Help with VBS code for looping files in a folder
    By johncassell in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-14-2011, 05:19 AM
  7. Looping through files in a folder
    By prepotency in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-13-2005, 11:23 AM

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