+ Reply to Thread
Results 1 to 4 of 4

Help make my code work: Loop through folder of workbooks with multiple sheets and combine

  1. #1
    Forum Contributor
    Join Date
    10-03-2015
    Location
    North Carolina, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Help make my code work: Loop through folder of workbooks with multiple sheets and combine

    Hi,

    I don't know enough vba to write all (or much at all) of my own code, so I hunt and peck around the internet to find code that works for my needs. I found two pieces of code that work as desired when used individually, but not when put together. The first piece of code is written to loop through all files in a folder and "do something" to each of them. The second piece of code is written to combine multiple workbooks into a single workbook. When combined, the code almost works perfectly, but it does throw an error at the very end. Actually, I think the end result is (combined file) good. I just want to know why there is an error, how to get rid of it, and if anyone can help me fix the problem.

    Below, I will provide the sample code that I found for each task, and then I will include my attempt at combining them to work together.

    Code to loop through all files in a folder and do something:

    Please Login or Register  to view this content.

    Code to combine multiple workbooks into a single workbook:

    Please Login or Register  to view this content.

    Here is my attempt at combining the two pieces of code:

    Please Login or Register  to view this content.

    When I run the combined code, the end result a combined file that looks good. I think it is doing what I want. BUT, at the end, the following error is thrown:

    Run-time error '5':
    Invalid procedure call or argument

    ... and when I debug, the following line (directly above the final Loop, and directly below DoEvents) is highlighted:

    myFile = Dir

    How can I fix this problem?

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Help make my code work: Loop through folder of workbooks with multiple sheets and comb

    Hello,

    Problem is that you can't use 2 DIR functions simultaneously. One overwrites the other.
    Suggested solution here :https://stackoverflow.com/questions/...stances-of-dir

    Regards.
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Forum Contributor
    Join Date
    10-03-2015
    Location
    North Carolina, USA
    MS-Off Ver
    Office 365
    Posts
    167

    Re: Help make my code work: Loop through folder of workbooks with multiple sheets and comb

    Hi GC, and thank you for your reply. You clearly pointed me at a resource that tackles my particular problem, but unfortunately, I just don't possess the knowledge necessary to put it to use. I enrolled myself in a vba class, but I fear it will be some time before I am able to tackle these more advanced obstacles. Could you please provide additional assistance?

    Thank you!
    Carlos

  4. #4
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Help make my code work: Loop through folder of workbooks with multiple sheets and comb

    Hello again,

    Sorry I looked to fast the first time. The modification was not too hard to do.
    Here the revised code.

    Please Login or Register  to view this content.

+ 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. Replies: 3
    Last Post: 02-06-2018, 12:53 PM
  2. Code to loop through workbooks in folder with specific names
    By stephme55 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-31-2016, 07:31 PM
  3. Combine same sheet from multiple workbooks in the same folder
    By a1b2c3d4e5f6g7 in forum Excel General
    Replies: 2
    Last Post: 05-29-2015, 03:20 PM
  4. Replies: 0
    Last Post: 02-13-2015, 04:26 AM
  5. Loop Through All Workbooks In Folder, Then All Sheets EXCEPT Sheet1 and Sheet2 InEachSheet
    By EnigmaMatter in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-09-2015, 07:48 PM
  6. Need Code to Combine All Workbooks in Folder
    By jcranst in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-01-2012, 02:18 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