+ Reply to Thread
Results 1 to 12 of 12

Loop to open excel files in a folder

  1. #1
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Loop to open excel files in a folder

    Hiya,

    I just wanted to find out if it would be possible to get a macro to loop through an entire folders files (excel) open them, copy some data over, then close them. (100's) of them

    This is on the assumption you dont know the exact string name of these files.

    The data is all in same format.

    What i kinda need is it to list the filenames into a colum, then i can get my macro to go down the list one by one...

    I have the code to open a workbook & i know how to use counters to loop through the operation, just cant think of how to get all the workbook names.

    Any help would be greatly appriciated.

    Cheers,

  2. #2
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Loop to open excel files in a folder

    Has anybody had a similar issue?

  3. #3
    Registered User
    Join Date
    05-20-2010
    Location
    Fresno, CA
    MS-Off Ver
    2003, 2010
    Posts
    58

    Re: Loop to open excel files in a folder

    Use the GetOpenFilename method and allow the user to select the folder and workbooks on their own. The filenames that the user selects will be passed to an array variable, and you can then use your loop to cycle through each each element and perform whatever action you want.

    See:
    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
    http://www.dailydoseofexcel.com/arch...topenfilename/
    Thanks,
    Ryan



    A word to the wise is infuriating. - Hunter S Thompson

  4. #4
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Loop to open excel files in a folder

    Quote Originally Posted by Ryan Murtagh View Post
    Use the GetOpenFilename method and allow the user to select the folder and workbooks on their own. The filenames that the user selects will be passed to an array variable, and you can then use your loop to cycle through each each element and perform whatever action you want.

    See:
    http://msdn.microsoft.com/en-us/libr...ffice.11).aspx
    http://www.dailydoseofexcel.com/arch...topenfilename/
    Ok,

    Well ive got it to "get" the filenames by making it select multiple also.

    Please Login or Register  to view this content.
    But im unsure how to reference the "filenames" to store them into an array.

    as its a singular refererence with multiple datapoints in it, im unsure as of how to proceed.

  5. #5
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Loop to open excel files in a folder

    ok, got it,.

    Thanks for your help!

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-20-2010
    Location
    Fresno, CA
    MS-Off Ver
    2003, 2010
    Posts
    58

    Re: Loop to open excel files in a folder

    elmarko,

    That looks good! I just have one suggestion...


    Instead of:
    Please Login or Register  to view this content.

    Consider using:
    Please Login or Register  to view this content.

    That way you may avoid a 'Subscript Out of Range' error if the user hits 'OK' without selecting any files.

    So the code would look like this...
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-20-2010
    Location
    Fresno, CA
    MS-Off Ver
    2003, 2010
    Posts
    58

    Re: Loop to open excel files in a folder

    You can also replace
    Please Login or Register  to view this content.

    With
    Please Login or Register  to view this content.

    ...but I'm not sure if there's a real functional advantage to that. I think that's just me trying to impose my own personal preference on you.

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Loop to open excel files in a folder

    If you know the folder path you could do it without user intervention assuming all the files in the folder meet your requirements - just an idea


    Please Login or Register  to view this content.

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

    Re: Loop to open excel files in a folder

    Stay away from the FileSearch function because it is no longer available after excel 2003. If you write you macro using FileSearch and later upgrade, it won't work (happened to me).

    abousetta

  10. #10
    Registered User
    Join Date
    04-14-2010
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Loop to open excel files in a folder

    Quote Originally Posted by Ryan Murtagh View Post
    You can also replace
    Please Login or Register  to view this content.

    With
    Please Login or Register  to view this content.

    ...but I'm not sure if there's a real functional advantage to that. I think that's just me trying to impose my own personal preference on you.

    Actually it really does help alot that little change, not particually for this marco, but for if i want to use two moving references based off variables i can move up/down/left & right easily (using my own style of coding)

    eg (totally useless bit of code, but before i went a long way round to reference horizontal compared to the easy vertical references)

    Please Login or Register  to view this content.

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop to open excel files in a folder

    I know I'm late to the party on this thread, but I have several Consolidation and Parse macros on my site that may be of use to you.

    In particular, the macro on this page may already be suited to your original question:On that page I've highlighted the lines of code that would need attention. Give it a try.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop to open excel files in a folder

    If your question is covered, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ 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