+ Reply to Thread
Results 1 to 15 of 15

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

Hybrid View

  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:

    Sub loopthroughfiles()
    
    Dim mainwb As Workbook
    Dim WB As Workbook
    Dim i As Integer
    
    'set a variable as the active open work book that the macro is running from, clear contents
    Set mainwb = ActiveWorkbook
    
    'set file path
    Set FileSYstemObj = CreateObject("Scripting.FileSystemObject")
    Set FolderObj = FileSYstemObj.GetFolder("C:\Users\E1TCH02\Desktop\Input")
    
    For Each fileobj In FolderObj.Files 'loop through the files
        If FileSYstemObj.GetExtensionName(fileobj.Path) = "csv" Then     
        Application.DisplayAlerts = False
        Set WB = Workbooks.Open(fileobj.Path)
        
        'I do a bunch of copying, adjusting, and pasting between the opened file and the mainwb
    
    End If
    Next fileobj
    
    mainwb.Activate
    mainwb.Save
    
    End Sub
    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)

    Sub LoopDirectoryExample(myPath As String, fileExtension As String,incSub As Boolean)
    
    'FILE SYSTEM OBJECTS REQUIRE:
    ' While you are in the Visual Basic IDE, go to the Tools Menu > References
    ' Add a reference to the Microsoft Scripting Runtime library
    '
        Dim fso As FileSystemObject
        Dim fld As Folder, subfld As Folder
        Dim fil As File
        Dim N As Long
        
         'initialize'
            Set fso = New FileSystemObject
            Set fld = fso.GetFolder(myPath)
            
        'place each file name in the array'
            For Each fil In fld.Files
                If UCase(fil.Name) Like "*." & UCase(fileExtension) Then
    
                '
                'DO YOUR THING TO THE FILES HERE
                '
    
                End If
            Next fil
            
        'pass in the subfolders if incSub is passed  in as "True"
        If incSub Then
                For Each subfld In fld.SubFolders
                    Call LoopDirectoryExample(myPath & "\" & subfld.Name, fileExtension, True)
                Next subfld
        End If
    End Sub
    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)


    Sub Driver()
      DIM targetBook as workbook
      Set target = thisWorkbook 'if you are calling the code from the workbook you would like to add data to.
      CALL listDirectoryExample(byRef targetbook,"MyPath","csv",TRUE)
    End Sub
    
    Sub LoopDirectoryExample(byRef SomeWorkbook as workbook,myPath As String, fileExtension As String,incSub As Boolean)
    
    'FILE SYSTEM OBJECTS REQUIRE:
    ' While you are in the Visual Basic IDE, go to the Tools Menu > References
    ' Add a reference to the Microsoft Scripting Runtime library
    '
        Dim fso As FileSystemObject
        Dim fld As Folder, subfld As Folder
        Dim fil As File
        Dim N As Long
        
         'initialize'
            Set fso = New FileSystemObject
            Set fld = fso.GetFolder(myPath)
            
        'place each file name in the array'
            For Each fil In fld.Files
                If UCase(fil.Name) Like "*." & UCase(fileExtension) Then
    
                '
                'DO YOUR THING TO THE FILES HERE
                'Use SomeWorkbook to point to the right file :)
    
                End If
            Next fil
            
        'pass in the subfolders if incSub is passed  in as "True"
        If incSub Then
                For Each subfld In fld.SubFolders
                    Call LoopDirectoryExample(someWorkbook, myPath & "\" & subfld.Name, fileExtension, True)
                Next subfld
        End If
    End Sub
    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!
    Sub Driver()
      DIM targetBook as workbook
      Set target = thisWorkbook 'if you are calling the code from the workbook you would like to add data to.
      CALL listDirectoryExample(targetbook,"MyPath","csv",TRUE)
    End Sub

  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...

    Sub WLDO_Driver()
    
    Dim targetBook As Workbook
    Set targetBook = ActiveWorkbook
    Dim i As Integer
    
    'creates summary sheet in target WB
    targetBook.Activate
    Sheets.Add.Name = "SUMMARY"
    
    Call LoopThroughDirectory(targetBook, "C:\Users\E1TCH02\Desktop\Input", "csv", True)
    
    'creates chart
    targetBook.Activate
    Sheets("SUMMARY").Select
     Range("D2").Select
        ActiveSheet.Shapes.AddChart.Select
        With ActiveChart
          .ChartType = xlLine
          .SeriesCollection.NewSeries
          .SeriesCollection(1).Values = Sheets("SUMMARY").Range("a1", ActiveSheet.Range("a1").End(xlDown))
          .Axes(xlCategory).Select
        End With
        Selection.Delete
    
    targetBook.Save
    
    End Sub
    
    Sub LoopThroughDirectory(ByRef SomeWorkbook As Workbook, myPath As String, fileExtension As String, incSub As Boolean)
        Dim fso As FileSystemObject
        Dim fld As Folder, subfld As Folder
        Dim fil As File
        Dim N As Long
        Dim sheet_index As Integer
        sheet_index = 1
        
         'initialize'
            Set fso = New FileSystemObject
            Set fld = fso.GetFolder(myPath)
            
        'place each file name in the array'
            For Each fil In fld.Files
                If UCase(fil.Name) Like "*." & UCase(fileExtension) Then
                    Application.DisplayAlerts = False
                    Set WB = Workbooks.Open(fil.Path)
        
                    'copy results from the just opened wb
                    WB.Worksheets(1).Select
                    Range("a1:s2200").Select
                    Selection.Copy
                    WB.Close
                    
                    'go to main wb and paste data
                    SomeWorkbook.Activate
                    If sheet_index <= 4 Then
                        Sheets(sheet_index).Select
                        ActiveSheet.Paste
                    Else
                        Sheets.Add
                        ActiveSheet.Paste
                    End If                
                    
        'I adjust the newly pasted data
            
        
    '    'take wanted information from each sheet and paste onto summary sheet
        Range("G11:H12").Select
        Selection.Copy
        Sheets("SUMMARY").Select
    
        If Range("a1").Value = "" Or Range("a2").Value = 0 Then
           Range("a1").Select
        Else
           Range("a1").End(xlDown).Select
           ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Select
        End If
    
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Columns("A:A").EntireColumn.AutoFit
    '
        'save main workbook
        SomeWorkbook.Activate
        SomeWorkbook.Save
    
        End If
        
        sheet_index = sheet_index + 1    
      Next fil
            
        'pass in the subfolders if incSub is passed  in as "True"
        If incSub Then
                For Each subfld In fld.SubFolders
                    Call LoopThroughDirectory(SomeWorkbook, myPath & "\" & subfld.Name, fileExtension, True)
                Next subfld
        End If
    End Sub
    Last edited by hutch94; 08-05-2014 at 04:06 PM.

  12. #12
    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".

    Option Explicit
    
    Sub WLDO_Driver()
    
    Dim targetBook As Workbook
    Dim i As Integer
    Dim summarySheet As Worksheet
    
    'creates summary sheet in target WB
    Set targetBook = ActiveWorkbook
    Set summarySheet = targetBook.Worksheets.Add
        summarySheet.Name = "SUMMARY"
    
    Call LoopThroughDirectory(targetBook, "C:\Users\E1TCH02\Desktop\Input", "csv", True)
    
    'creates chart
     targetBook.Activate
     Sheets("SUMMARY").Select
     Range("D2").Select
        ActiveSheet.Shapes.AddChart.Select
        With ActiveChart
          .ChartType = xlLine
          .SeriesCollection.NewSeries
          .SeriesCollection(1).Values = Sheets("SUMMARY").Range("a1", ActiveSheet.Range("a1").End(xlDown))
          .Axes(xlCategory).Select
        End With
        Selection.Delete
    
    targetBook.Save
    
    End Sub
    
    Sub LoopThroughDirectory(ByRef SomeWorkbook As Workbook, myPath As String, fileExtension As String, incSub As Boolean)
        Dim fso As FileSystemObject
        Dim fld As Folder, subfld As Folder
        Dim fil As File
        Dim N As Long
        Dim sheet_index As Integer
        Dim WB As Workbook
        Dim newSheet As Worksheet
        
        sheet_index = 1
        
         'initialize'
            Set fso = New FileSystemObject
            Set fld = fso.GetFolder(myPath)
            
        'place each file name in the array'
            For Each fil In fld.Files
                If UCase(fil.Name) Like "*." & UCase(fileExtension) Then
                    Application.DisplayAlerts = False
                    Set WB = Workbooks.Open(fil.Path)
        
                    'copy results from the just opened wb
                        Set newSheet = SomeWorkbook.Worksheets.Add
                        newSheet.Range("A1:S2200").Value = _
                            WB.Worksheets(1).Range("a1:s2200").Value
                        
                        targetBook.Worksheets("SUMMARY").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(2, 2).Value = _
                            newSheet.Range("G11:H12").Value
                  
                'save main workbook
                        SomeWorkbook.Save
    
                End If 'UCase(fil.Name) Like "*." & UCase(fileExtension)
        
            Next fil
            
        'pass in the subfolders if incSub is passed  in as "True"
        If incSub Then
                For Each subfld In fld.SubFolders
                    Call LoopThroughDirectory(SomeWorkbook, myPath & "\" & subfld.Name, fileExtension, True)
                Next subfld
        End If
    End Sub
    Last edited by GeneralDisarray; 08-05-2014 at 04:22 PM.

  13. #13
    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.

  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