+ Reply to Thread
Results 1 to 17 of 17

Copy Paste data from Files within Subfolders

  1. #1
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    Copy Paste data from Files within Subfolders

    Hello,

    I have the following code pulling data from files a specified folder, into a master file. I need to amend the code to pull files from any subfolders in the main folder, if present.

    How do I amend what I have to achieve this?

    Thanks.

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Copy Paste data from Files within Subfolders

    @lyla22

    You can adapt from here:

    Please Login or Register  to view this content.
    If you include your MoveData code, remember to put only the process of the file, not the search.
    meaning only check if the file is excel workbook, and transfer the data.
    you don't need to loop again
    Barriers are there for those who don't want to dream

  3. #3
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Copy Paste data from Files within Subfolders

    @lyla22

    You can adapt from here:

    Please Login or Register  to view this content.
    If you include your MoveData code, remember to put only the process of the file, not the search.
    meaning only check if the file is excel workbook, and transfer the data.
    you don't need to loop again

    I adapted this code from ExelTotal.com
    sorry it's in spanish.... but is easy to understand

    Vicho

  4. #4
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    Re: Copy Paste data from Files within Subfolders

    Hi Vicho,

    I've made an attempt but not really sure what I'm doing! Can you give me some advice on what needs to be changed?

    Thanks



    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Copy Paste data from Files within Subfolders

    Hi lyla22

    I'm guessing about your work, as I didn't see your data
    I hope it goes OK for you

    Please give it a try over some sample data, until you are sure it is OK
    sorry for the double posting before.

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    Re: Copy Paste data from Files within Subfolders

    Hi Vicho,

    Thank you for your help. I tried your code but it's not working, I understand it's difficult to do without actual data.

    I''m attaching the file along with some import data. If you caould take a look, I'd be really grateful.

    Thanks.
    Attached Files Attached Files

  7. #7
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Copy Paste data from Files within Subfolders

    OK lyla22
    Let's go step by step.

    Fill your own path in cell B1,
    In the attached book I've placed two buttons on sheet Parameters.
    first click TEST1
    it should show you ALL files in your path, including subfolders.

    If this test is OK, then clean the data showed and click TEST2
    now it should show you all EXCEL files: xls, xlsx, xlsm... etc.

    I'm waiting for your comments.
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    Re: Copy Paste data from Files within Subfolders

    Hi Vicho,

    I appreciate your time with this.

    I've looked at your file and yes, it's correctly listing the Excel files in all subfolders...

  9. #9
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Copy Paste data from Files within Subfolders

    In your original post, you placed as the first action:

    Please Login or Register  to view this content.
    I guess that it is acting over your "Master" sheet, in order to clean all the data from a previous run.
    But using the "UsedRange" property to find the last row, is not a good idea, while you have a named range "chtData" defined to row 1000
    so the code is writing the imported values in row 1001 and so .

    Just to test please eliminate the named range to see if the code can perform what you need.
    click on TEST

  10. #10
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Copy Paste data from Files within Subfolders

    Sorry
    the book was not attached
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    Re: Copy Paste data from Files within Subfolders

    So I've removed the table range name chtdata and when I click TEST:

    1. Ruta inexistente msgbox appears, click OK then;
    2. Master sheet appears with a single row of data from the first import file and in front I see the Ruta inexistente msgbox again.
    3. I click ok to the Ruta inexistente msgbox and the line of data disappears. Nothing else happens.
    4. The file from which the data was copied from is still open in the background.

  12. #12
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Copy Paste data from Files within Subfolders

    That means we are having problems to "Close and Save" the opened book.
    Could you please post the book with out the named range (I can not delete because it is password protected), and one sample book from where you import data.
    How many books are involved in the process (Aprox.) ...?, Are all of them exactly the same estructure??
    From your code, there is no change on the data source book...do you realy need to Save it when closing?
    On your last TEST, that single row of data from the first book, was correct? Complete? On the first row under the title?

  13. #13
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    Re: Copy Paste data from Files within Subfolders

    Quote Originally Posted by vichopalacios View Post
    That means we are having problems to "Close and Save" the opened book.
    Could you please post the book with out the named range (I can not delete because it is password protected), and one sample book from where you import data.
    How many books are involved in the process (Aprox.) ...?, Are all of them exactly the same estructure??
    From your code, there is no change on the data source book...do you realy need to Save it when closing?
    On your last TEST, that single row of data from the first book, was correct? Complete? On the first row under the title?

    OK, files attached.

    How many workbooks?
    No fixed number, could be as little as 20 and as many as 500...

    No need to save the import files on close.

    The row of data did import correctly, on the first row under the title...
    Attached Files Attached Files

  14. #14
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Copy Paste data from Files within Subfolders

    Good news @lyla22
    Let me play around during weekend
    I'll come back on monday.

    Vicho

  15. #15
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Copy Paste data from Files within Subfolders

    Hi @lyla22

    I think I found the bug.
    please try this book with your own path
    Attached Files Attached Files

  16. #16
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Cork, Ireland
    MS-Off Ver
    Excel 365
    Posts
    149

    Re: Copy Paste data from Files within Subfolders

    Hi Vicho,

    Sorry, I didnt see your last post 'til now. It seems to be working! Thank you! Can you give me a quick overview as to what the different
    parts of the macro do? I have the idea but the language is throwing me off a little!

    Once the data is pulled in, there are calculations I want the macro to perform, where should I put that code in your file?

    Many thanks again for your time...

  17. #17
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Copy Paste data from Files within Subfolders

    I'm pleased to be able to help.

    First I suggest you to read about the "moduldes" in VBA ( the web is plenty of information).
    This is a good place to start reading:
    HTML Code: 
    The codes we are writing here should go into regular modules, not in a sheet module.

    Second, whenever you write a code, and it gets long enought to be difficult to follow, then it's time to brake it into smaller pieces.

    Third, trying to clear, add, delete data or rows/columns in a filtered range, can produce unexpected results, so it's a good practice to unfilter, do the job, and filter again.

    With this in mind, let's talk about the codes:
    Sub MiRuta3() takes the path (ruta in spanish) from sheet Parameters, to search the files in that folder and sub-folders.
    then unfilter sheet Master, and clean all the previous data.
    then it calls the sub Mostrar_Archivos3(rUta), passing the variable ruta; at the end of this sub, Master sheet will be filled with all the data imported from all the files in ruta and its subfolders.
    then the new table is auto-filtered again, letting it ready to be explored.

    Call Mostrar_Archivos3(rUta) is a generic routine that you can use whenever you need to search files inside a folder (which path is ruta), and its subfolders, using the FileSystemObject.. this is a recursive sub as you can see, it calls itself passing this time the subfolder path....
    here we used an IF statement to filter only .xls files.
    So every time we find a file we proceed with an action, in our case the action is defined in another sub: MoveData3(aRchivo), to wich we are passing the path to that specific workbook (archivo). This sub will produce one new line at the bottom of the Mastersheet, for each file found.
    After all the files are processed, we are calling a final sub Arrenge (by now it is commented with an apostrophe). this sub will be on charge to do all the calculations on the data created until now.

    In the sheet2 Master Module I left the arrenge code....(Note that this is not the place where it has to be, I used it as a momentary storage, you shoul cut it from here and paste in the regular Module 2). So you can uncomment the call Arrenge line. Do this, and try to see how it goes.

    I can guess that you recorded a macro from your actions.....
    Do you really need to write formulas? or you just want to have the results the formula calculations.?

+ 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] list of subfolders in folder - without files and sub-subfolders
    By MartyZ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-11-2022, 10:56 AM
  2. [SOLVED] Macro to Copy and paste folders and subfolders without files in it
    By Madhut in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-07-2016, 01:47 PM
  3. VBA macro that copy files from multiple subfolders
    By R12345 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-14-2016, 03:22 AM
  4. Needs macro to copy excel files from folders, subfolders to new folder
    By genetist in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-08-2014, 08:53 AM
  5. [SOLVED] Files within Multiple SubFolders and SubFolders Within It
    By codeslizer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-18-2013, 04:18 AM
  6. copy files from subfolders
    By tryer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-08-2012, 05:53 PM
  7. [SOLVED] copy subfolders, replace text in files and save files in copied subfolders
    By pieros in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-01-2005, 09:05 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