+ Reply to Thread
Results 1 to 15 of 15

Macro to open up a .csv file inside each subfolder in a master folder

  1. #1
    Registered User
    Join Date
    07-28-2014
    Location
    Richmond, VA
    MS-Off Ver
    excel 2010
    Posts
    12

    Macro to open up a .csv file inside each subfolder in a master folder

    I have a piece of code that opens up .csv files within a master folder.... But, I found out that they will each be in separate subfolders (inside master folder). So I need to adjust this code to help me go about searching through the subfolders:

    Please Login or Register  to view this content.
    Can anyone guide me in the right direciton? Any help would be appreciated greatly.

  2. #2
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    This would do it, but you need to call in from another sub to pass in the Extension, Directory path, and use TRUE for incSub (include Subfolders)

    Please Login or Register  to view this content.
    Last edited by GeneralDisarray; 08-05-2014 at 11:30 AM.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  3. #3
    Registered User
    Join Date
    07-28-2014
    Location
    Richmond, VA
    MS-Off Ver
    excel 2010
    Posts
    12

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    Thanks, I will give it a shot!

    Could you briefly explain what the portion of code in red above does?

    Also, in my prior code, I set mainwb as the workbook that is active at the start (the one I run the macro from, and the one that I paste stuff that I get from these .csv files). Would I need to outline mainwb in this sub routine, or in the main macro?

  4. #4
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    The portion in red just calls the sub routine again, for each subfolder (it's iterative, it will snake through sub folders within sub folders... within sub folders....)

    The workbook could be passed into either routine - this structure just loops through and doesn't reference any workbooks yet. Be careful how you do that in the "DO YOUR THING TO THE FILE HERE" section.

  5. #5
    Registered User
    Join Date
    07-28-2014
    Location
    Richmond, VA
    MS-Off Ver
    excel 2010
    Posts
    12

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    So just to clarify:

    I know that my subfolders will not contain any more subfolders... A 1-1 ratio, each subfolder has one .csv file, the file that im looking for.

    Since this is the case, and nothing is buried too deep, this incSub will only iterate ONCE (because its only one layer deep)?

    Lastly, say I call mainwb = ActiveWorkbook at the very start of the main routine, will that definition change based on what is currently active? In other words, I need mainwb to stay constant throughout this entire code because I continue to reference it (and I will reference it in this sub routine many many times).

  6. #6
    Registered User
    Join Date
    07-28-2014
    Location
    Richmond, VA
    MS-Off Ver
    excel 2010
    Posts
    12

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    So just to clarify:

    I know that my subfolders will not contain any more subfolders... A 1-1 ratio, each subfolder has one .csv file, the file that im looking for.

    Since this is the case, and nothing is buried too deep, this incSub will only iterate ONCE (because its only one layer deep)?

    Lastly, say I call mainwb = ActiveWorkbook at the very start of the main routine, will that definition change based on what is currently active? In other words, I need mainwb to stay constant throughout this entire code because I continue to reference it (and I will reference it in this sub routine many many times).

  7. #7
    Registered User
    Join Date
    07-28-2014
    Location
    Richmond, VA
    MS-Off Ver
    excel 2010
    Posts
    12

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    *SORRY FOR DUPLICATE POST* computer was lagging
    Last edited by hutch94; 08-05-2014 at 01:51 PM.

  8. #8
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    Here's what I would suggest. Add another parameter to the loop routine to allow you to pass in a workbook variable (byRef).

    Then, in the main routine that will call the loop routine, SET the workbook as needed. After that, you can refer to that workbook by variable name SomeWorkbook in the Loop routine - no matter which iteration.

    Having only one subfolder won't be any problem - the code can just handle as many as needed. The "For Each" style of loop is designed to loop over a "collection" (in this case, a collection of subfolders for any folder) and is by design flexible enough to handle 0,1,2,3.... members in the collection.

    BTW, I didn't make these edits within the IDE so watch for typos (always use "Option Explicit" IMO)


    Please Login or Register  to view this content.
    Last edited by GeneralDisarray; 08-05-2014 at 02:05 PM.

  9. #9
    Registered User
    Join Date
    07-28-2014
    Location
    Richmond, VA
    MS-Off Ver
    excel 2010
    Posts
    12

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    I get an Expected:expression bug when I try to compile.

    It happens when I CALL the sub routine into play, specifically it highlights byRef

  10. #10
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    AHHHH! i should have typed it into the IDE.

    That shouldn't be there (it only needs to appear below, in the LoopDirectoryExample definition of parameters)

    Take that "ByRef" out!
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    07-28-2014
    Location
    Richmond, VA
    MS-Off Ver
    excel 2010
    Posts
    12

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    Awesome it compiled! Many thanks.

    The mechanisim is working perfectly.

    However one quick problem:
    The main goal is to open the csv file and paste contents into the target (main) WB on separate sheets. I did this by tracking the sheet index. I made a variable called sheet_index which I added +1 to everytime through the loop, this told the macro to paste the data onto the next sheet, or create a new sheet and paste depending on how many there were in the work book. NOW I can't use that method it seems. Below I have posted a shortened version of the main code. Currently, it just copies everything onto the same sheet...

    Please Login or Register  to view this content.
    Last edited by hutch94; 08-05-2014 at 04:06 PM.

  12. #12
    Registered User
    Join Date
    07-28-2014
    Location
    Richmond, VA
    MS-Off Ver
    excel 2010
    Posts
    12

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    To more concisely phrase it:

    Where should I define sheet_index as Integer, initialize it as 1, and then tell it to sheet_index++ each time it gets to a new csv file?

    The current locations are not working because this new method iterates in a different fashion than before.

  13. #13
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    Try it without the index - just add a new sheet every time. It's a good habit to use Option Explicit and always introduce variables used in a sub at the top with a DIM statement (like your WB).

    Also, instead of just referencing "sheets()" or "range()" it's a good idea to qualify everything. So, instead of a sheets.add (which you then refer to later) I would use "Set NewSheet = WorkbookVariable.add"

    When you fully qualify, you also don't have to worry about what's active or not -- all those .select or .activate statements can be trimmed out

    Also, your code to copy then paste can be altered to "equate values of same-sized ranges".

    Please Login or Register  to view this content.
    Last edited by GeneralDisarray; 08-05-2014 at 04:22 PM.

  14. #14
    Registered User
    Join Date
    07-28-2014
    Location
    Richmond, VA
    MS-Off Ver
    excel 2010
    Posts
    12

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    Thanks! This does the job, and works well! And way more efficient!

    If you can't tell haha, I am super new to VBA... I know C, so I was able to figure the logic out, but the new syntax and object oriented stuff was killing me.

    Thanks for the quick help

    I will let you know if I run into any other problems

  15. #15
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Macro to open up a .csv file inside each subfolder in a master folder

    No problem, we've all been there. My only programing experience prior to working in excel was Turbo Pascal, and Python / C also

    Please mark your original post as "Solved"

+ 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] Code to create folder and inside subfolder
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 03-07-2014, 03:03 PM
  2. [SOLVED] How to Copy cell value of all files which inside of folder and append to Master file
    By snsuvarna in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-06-2013, 03:38 PM
  3. Macro that searches in folder and subfolder - please help
    By anaconte1010 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2013, 10:11 AM
  4. Macro to Create Folder\Sub Folder\SubFolder\
    By coolhit in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-30-2012, 12:43 PM
  5. [SOLVED] Macro to copy file names and data from each file in a folder into master spreadsheet
    By dee1989 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-10-2012, 05:52 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