+ Reply to Thread
Results 1 to 3 of 3

VBA Look Through Folder and Open Files Not Listed in Master File

Hybrid View

  1. #1
    Registered User
    Join Date
    05-13-2013
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    9

    VBA Look Through Folder and Open Files Not Listed in Master File

    So I have a master document and several hundred files with different date suffixes in the same folder. On the master document I want to list the date code and some various information in the file itself. The folder will be updated regularly so I want to be able to run the report periodically and add any new dates to the list.

    So how do I do the following:
    1. Search each file name in a folder
    2. If the file is on my list, skip it. Else open the file.
    3. Preform a function to the open file.
    4. Close the file and proceed to continue checking the folder.

    I cobbled together the below from various sources but it seems to have the problem of opening every file after a new file is found (eg if I have 100 files in the folder and only 97 is new, it will open file 97 to 100).

    Sub ListFilesInFolder(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)
    
        Dim FSO, SourceFolder, SubFilder, FileItem As Object
        Dim r As Long
    
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set SourceFolder = FSO.GetFolder(SourceFolderName)
        OrgWb = ThisWorkbook.Name
        OrgWs = ActiveSheet.Name
        Directory = "K:\Production\Robots\"
        
        r = Range("A65536").End(xlUp).Row + 1
    
            
            For Each FileItem In SourceFolder.Files
              FileDate = Left(Right(FileItem.Name, 12), 8)
                Set Result = Cells.Find(What:=FileDate, After:=Cells(1, 1), _
                LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
                    If (Not Result Is Nothing) Then
                    Else
                        FilePath = Directory & FileItem.Name
                        Workbooks(OrgWb).Worksheets(OrgWs).Cells(r, 1) = FileDate
                            Workbooks.Open (FilePath)
                            'Do Suff with Open Book
                            Workbooks.Close (FilePath)
                        r = r + 1
                    End If
              X = SourceFolder.Path
            Next FileItem
            If IncludeSubfolders Then
              For Each SubFolder In SourceFolder.SubFolders
                ListFilesInFolder SubFolder.Path, True
              Next SubFolder
            
            End If
            
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
        
    End Sub

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: VBA Look Through Folder and Open Files Not Listed in Master File

    Try adding this line of code just before looping for Next FileItem:
    Set Result = Nothing
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  3. #3
    Registered User
    Join Date
    05-13-2013
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: VBA Look Through Folder and Open Files Not Listed in Master File

    Thanks for the help p24leclerc but unfortunately that didn't help. However, I played around with it and it seems to be working after I made a couple of changes. Now I'm curious if someone can explain why the first doesn't work but the second does in case this comes up again in the future.

    Previously, once it found an item not on my list it would just stop and loop through every file after the new file at Workbooks.Open (FilePath) even if I just pressed F8 once.

    FilePath = Directory & FileItem.Name
                        Workbooks(OrgWb).Worksheets(OrgWs).Cells(r, 1) = FileDate
                            Workbooks.Open (FilePath)
                            'Do Suff with Open Book
    Filename = FileItem.Name
                        Workbooks(OrgWb).Worksheets(OrgWs).Cells(r, 1) = FileDate
                            Workbooks.Open Filename:=Directory & Filename
                            'Do Stuff
    Last edited by CGriffiths1214; 10-07-2015 at 12:19 PM. Reason: Added More Detail

+ 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] Macro to open up a .csv file inside each subfolder in a master folder
    By hutch94 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 08-05-2014, 07:24 PM
  2. Macro to copy some cells in some excel files in a folder to a master file
    By phemocheee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-17-2013, 04:40 AM
  3. merge csv files within same folder into one master file
    By dvb_24 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-07-2012, 07:11 PM
  4. Copying data from multiple files in a folder to a master file
    By kjshep in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-22-2012, 12:31 PM
  5. Noob 4 Help - Macro to LIST ALL FILES IN FOLDER and then IMPORT ALL LISTED FILES
    By StlSmiln in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-25-2012, 04:02 AM
  6. Get txt files from a folder to master file in different sheets
    By aravindhan_31 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-31-2009, 05:29 AM
  7. Replies: 3
    Last Post: 05-06-2006, 11:50 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