+ Reply to Thread
Results 1 to 16 of 16

Loop thru files in folder and copy things

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

    Loop thru files in folder and copy things

    I am looking for a simple piece of code that loops thru all files in a folder (so opening them and close when done).

    Then after opening perform my main code.
    When main code is done, close workbook and go to the next file in the folder.

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Loop thru files in folder and copy things

    Are all your source files in the same structure?

    This is straightforward with Power Query (Get & Transform Data). Post sample workbooks.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

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

    Re: Loop thru files in folder and copy things

    Yes they are but why would it matter?

    It just needs to cycle thru the excel files in a folder, open them, run the main code (that copies fields from the file to my main book) and then close the file again.

    Need to fix it with normal VBA for Excel (no power query)

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Loop thru files in folder and copy things

    Why not use Power Query? It's perfect for this type of requirement. You have Excel 2016, so the functionality is built in. It's much quicker than VBA opening every file...

    Something like:

    Please Login or Register  to view this content.

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

    Re: Loop thru files in folder and copy things

    Could be I do not know power query and not sure if I and my colleagues (who I am making this for have it).

    Well is was playing around with a simple code from the net trying to amend it for my needs:
    Please Login or Register  to view this content.
    The StrFile is adepted to my needs opening every thing that begins with that text up to the weeknumber.
    But I do not know where to put my main code to get it to work.

  6. #6
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Loop thru files in folder and copy things

    Quote Originally Posted by rpinxt View Post
    Could be I do not know power query....
    With respect, you don't know the VBA to do what you need, either...


    Quote Originally Posted by rpinxt View Post
    ... and not sure if I and my colleagues (who I am making this for have it).
    As I said, you have Excel 2016 which has Power Query built in. Which Excel version are your colleagues using?

    Happy to write the precise query you need, if you upload a sample source workbook, and indicate which data you're trying to collate.

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

    Re: Loop thru files in folder and copy things

    Well I'm building vba models for year now.
    No I am no expert but our company has lots of benfits from it.

    This is my main code as VBA has to lookin different tabs with different length to pull out the numbers (so maybe it is not as simple as you think)
    Please Login or Register  to view this content.
    It is where it opens the workbook to copy from I need a loop that opens all the files in the folder one by one.
    Main code should do the rest.

    ps:normally I only open 1 data book, but this problem needed to have multiple books in a row openend and that why i was asking for help.

  8. #8
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Loop thru files in folder and copy things

    This combines data from all worksheets in all files (with name beginning `02. Daily Week`) in your specified folder.

    Please Login or Register  to view this content.
    Give it a try. You might like it.

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

    Re: Loop thru files in folder and copy things

    Well yes if it is better and faster I am sure I will like it.

    But it is no flat data source file.
    I will attach 1 weekday from 1 of the files.
    Be aware all these tabs are not the same length.
    And I only need 2 amounts per tab from each book.
    So not sure of combining all that data (it is for 1 year so 52 books times 5 tabs...) is usefull.
    Attached Files Attached Files

  10. #10
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Loop thru files in folder and copy things

    So you want the Actual and Planned values from Columns C and D, for the last row in Column C containing data, for each worksheet in each file?

    Try this:

    Please Login or Register  to view this content.

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

    Re: Loop thru files in folder and copy things

    Where would I put that code? And how do I activate it.

    Further I would only want the last amounts in columns C and D for Actual and Plannend.
    For the 5 weekdays in the file (nothing of the other sheets)

  12. #12
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Loop thru files in folder and copy things

    New information: workbooks contain more sheets then required. Okay. Here's an amended query to filter only worksheets Mon - Fri:

    Please Login or Register  to view this content.
    For how to use Power Query code, see link in my signature (https://excel.solutions/2017/11/powe...te-code-video/)

    (edited to make code a little easier to read)
    Last edited by Olly; 10-16-2019 at 06:43 AM.

  13. #13
    Registered User
    Join Date
    07-04-2017
    Location
    Belgium
    MS-Off Ver
    365 ProPlus
    Posts
    81

    Re: Loop thru files in folder and copy things

    Hey rpinxt,

    If you're still looking for a VBA solution, you might want to have a look at this:
    https://www.computergaga.com/blog/lo...esystemobject/
    https://exceloffthegrid.com/vba-code...r-sub-folders/

    Please Login or Register  to view this content.
    No offence, Olly :p
    You're probably much more of an expert than I am.

    Bram
    Last edited by dunnobe; 10-16-2019 at 08:53 AM.

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

    Re: Loop thru files in folder and copy things

    Thanks Olly and Bram.
    I indeed found a solution in the classic VBA code
    Please Login or Register  to view this content.
    I will look into the power query solution, maybe I can unravel how to use it

  15. #15
    Registered User
    Join Date
    07-04-2017
    Location
    Belgium
    MS-Off Ver
    365 ProPlus
    Posts
    81

    Re: Loop thru files in folder and copy things

    Hey rpinxt,

    Write down your entire code.
    No doubt someone can give you some advice on making your code more efficient.

    https://analysistabs.com/vba/optimiz...macros-faster/

    Speed is everything.

    Bram
    Last edited by dunnobe; 10-18-2019 at 09:22 AM.

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

    Re: Loop thru files in folder and copy things

    I agree dunnobe.

    But I am ok with the speed of my macro

    And yes the real wizards can most likely make it much more fast.
    But then the code that they are using I do not understand anymore .
    I like to also understand what is going on. Would have to study on their code and unfortunately I do not have that time at the moment .

    ps: forgot to close the thread. Sorry.

+ 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. Loop through files in folder, copy range, Paste range to original workbook
    By knevil in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-09-2016, 05:33 PM
  2. Choose a Folder and Loop a Sub for all files in the chosen folder
    By smartbuyer in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-11-2016, 10:57 PM
  3. [SOLVED] Choose a folder and loop through all files in the chosen folder
    By smartbuyer in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-10-2016, 11:19 PM
  4. [SOLVED] Loop Through Folder, Create Emails with Sub Folder Names in Subject, Attach files in sub
    By Rschwar23 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2015, 10:06 AM
  5. Replies: 12
    Last Post: 03-09-2015, 05:52 PM
  6. VBA Loop for a folder of all the files, Loop all the worksheet in each workbook
    By nanjingwoodworking in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-07-2013, 07:20 PM
  7. [SOLVED] VBA Code open files in folder, copy text to workbook-Next time folder opened copy only new
    By Bikeman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2013, 07:59 PM

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