Results 1 to 11 of 11

Excel VBA: Search folder and sub-directories for files matching partial string

Threaded View

  1. #1
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Excel VBA: Search folder and sub-directories for files matching partial string

    I have a sub that's part of a larger routine to search folders and subfolders. I want to integrate a script that once a subfolder is found, to look within the subfolder and find files that contain the letters *dq* within it. I've attempted to try to use a couple approaches and have come up with this as my best option, although when I run it, no errors are thrown and no output occurs even though there are numerous files within the main directory meeting the criteria. My objective is when a file is found within the subfolder to open and search for a string and if true then list the file name and location in the sheet, then proceed to the next file within the subfolder.
    Sub ListFiles(ByRef Folder As Object)
        Dim fName as String
            If Not Folder Like "*dq*" Then Exit Sub
    
            Set sht = ThisWorkbook.Worksheets("Sheet1")
            r = sht.Cells(sht.Rows.count, "A").End(xlUp).Row + 1
            
            With ActiveSheet
            
                On Error Resume Next
                    For Each File In Folder.Files
                        fName = File.Name
                        With File
                             If fName Like "*dq*" Then
                                'open the file and call the string search
                                'if string search then
                                    .Cells(r, 1).Value = File.ParentFolder
                                    .Cells(r, 2).Value = File.ShortName
                                    .Cells(r, 3).Value = File.ShortPath
                                    .Cells(r, 4).Value = File.Type
                                    
                                    r = r + 1
                            End If
                        End With
                    Next File
            
            End With
    
    End Sub
    EDIT: after looking at the code, I realized I had a redundant search. I revised to look at each File within the loop, however it still does not find any existing files within the directory
    Last edited by terriertrip; 10-16-2020 at 01:43 PM. Reason: incorrect code; revised code

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Search for a string on all excel files inside a ZIp folder
    By Moreno20 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-02-2014, 03:45 PM
  2. How To Use a ComboBox to Search Matching Files Name & Open it From A Folder
    By DKTRL in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-28-2013, 08:34 AM
  3. [SOLVED] Search for a partial string match and cycle through matching results
    By kamelkid2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-08-2013, 08:27 AM
  4. copy files located in multiple directories that match a partial value
    By msheen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-18-2012, 04:55 PM
  5. Want to search for string in list of files in a folder
    By DeepthiReddy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-27-2012, 03:21 AM
  6. Case insensitive search for string within folder of excel files
    By vba_novice in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-22-2011, 11:21 PM
  7. Search files in folder based on search string
    By djvice in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-22-2008, 01:12 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