+ Reply to Thread
Results 1 to 8 of 8

Macro - Collect data - Select folder and loop through all xls files

  1. #1
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118

    [Solved] Macro - Collect data - Select folder and loop through all xls files

    *see end of this message for the solution*

    Hi all,

    I am trying to to create a macro that pops up the "open file" messagebox, you select a folder and it will go on to loop through all the .xls files in that folder and perform some data collecting function.

    I have managed to form the data collecting part but can't figure out how to create the loop that will open and read all the xls files. Therefore I have resorted to hardcoding the filename of each file into my macro but this is not convenient as I have to use the same technique for other parts of my workbook and I can't update the filename every time I want to use the workbook with another set of files.

    Attached is my example and I also copy it here:


    Please Login or Register  to view this content.
    Summary:

    All the files I want to open are excel files
    ** They are all in one folder which I want to select every time I run the macro
    What I want to do after I open every file is to copy/paste-values-only specific columns (2 in number right now) to specific worksheet/columns in my other workbook where the macro is called from.

    If you could show me a way to grab the filename and print it in a specific cell that would be a bonus.

    Thank you in advance for any advice or help.

    Kostas

    ** I have found the vba code that allows me to open into a specific folder and then I can navigate through it here:
    Please Login or Register  to view this content.
    . Here it is:
    Please Login or Register  to view this content.
    and it's being called through another routine
    Please Login or Register  to view this content.

    --Solution by Leith--
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by kostas; 04-04-2008 at 06:01 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello kostas,

    Here is the revised macro to copy the data from the workbooks you select onto the two worksheets in the main workbook. It uses the Open file dialog to allow you to choose the directory and files. Only workbook files (.xls) are listed.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  3. #3
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    macro

    Hi
    try the macro codes in the text file attached. Change file path if necessary It will do the rest for you
    ravi
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118
    Thank you both very much for responding so quickly. I will try them out and report back here with how they work.

    Again thank you,

    Kostas

  5. #5
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118
    Ok,

    I have some feedback,

    Leith,

    I run the macro, selected a file and then it broke says "subscript out of range" at
    Please Login or Register  to view this content.
    I believe it has something to do with the way it locates the row/column but I don't clearly understand what goes wrong.

    Ravishankar,

    I run your macro but it breaks at many different points. The issue is that I have to rename the sheet names (I think) with the ones I use and I'm not sure which worksheet is the one opened and which worksheet is the one the data goes to in the code you have given me, so I can replace them correctly.

    sorry for not understanding much but im quite an amateur in VBA.

    Any further assistance will be greatly appreciated.

    Regards,

    Kostas

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Kostas,

    Referencing can get a little tricky when using multiple workbooks. I fixed the code and ran a few tests. The results were correct. Give a try on your end. Here is the revised code.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  7. #7
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    macro

    HI
    I have used Sheet1 for both source and destination file. If they are different let me know.
    Ravi

  8. #8
    Forum Contributor
    Join Date
    03-21-2007
    Posts
    118
    Leith,

    Your solution worked amazingly. It's just excellent. Thank you very-very much for all the time you put into this.

    The only amendments I had to make to the code were the following:

    I added a
    Please Login or Register  to view this content.
    right after the
    Please Login or Register  to view this content.
    to avoid being asked about updating external links etc from the macro.

    I also added a
    Please Login or Register  to view this content.
    at the end of the macro after the
    Please Login or Register  to view this content.
    to avoid being asked whether I want to save the file or not when it's being closed (since it is opened as read only it asks for save/save as in the end).

    Finally I had to make a small correction on
    Please Login or Register  to view this content.
    , instead of CG it had to be CB as it was rightly coded above (otherwise it was copying the wrong column from the source file).

    Overall excellent and thank you once again.


    Ravinshankar,

    The source workbook has a sheet called "data" and I want to copy the columns BG and CB.
    Column BG goes to the destination workbook's sheet called "formdata" and column CB goes to the destination workbook's sheet called "formdata2".

    I can't ask you to spend more time on this request as Leith's answer succesfully solved it. However if you are interested academically to provide a leaner and quicker solution I'll be happy to receive it!

    Many thanks for your efforts and time everyone. I will update the initial post to include the final solution in the bottom so every can see it directly.

    Regards,

    Kostas

+ 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