+ Reply to Thread
Results 1 to 11 of 11

Search for file names and list them in column A

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Search for file names and list them in column A

    Hello everyone
    The following masterpiece of code is Mr. Leith Ross's code. It searches for specific file names that contain specific string
    Private FileFilter  As String
    
    Function ListFiles(ByVal FolderPath As Variant, ByRef OutputCell As Range, Optional ByVal SearchDepth As Long)
    ' Written:  November 07, 2015
    ' Author:   Leith Ross
    ' SearchDepth = Maximum level (depth) of Subfolders to search.
        Dim N           As Long
        Dim oFile       As Object
        Dim oFiles      As Object
        Dim oFolder     As Variant
        Dim oShell      As Object
        
        If oShell Is Nothing Then
            Set oShell = CreateObject("Shell.Application")
        End If
        
        Set oFolder = oShell.Namespace(FolderPath)
        If oFolder Is Nothing Then
            MsgBox "The Folder '" & FolderPath & "' Does Not Exist.", vbCritical
            SearchDepth = 0
            Exit Function
        End If
        
        Set oFiles = oFolder.Items
        
        N = 0
        
        oFiles.Filter 64, FileFilter
        For Each oFile In oFiles
            OutputCell.Offset(N, 0) = oFolder.self.Name
            OutputCell.Parent.Hyperlinks.Add OutputCell.Offset(N, 1), oFile.Path, , , oFile.Name
            N = N + 1
        Next oFile
        
        Set OutputCell = OutputCell.Offset(N, 0)
        
        oFiles.Filter 32, "*"
        If SearchDepth <> 0 Then
            For Each oFolder In oFiles
                Call ListFiles(oFolder, OutputCell, SearchDepth - 1)
            Next oFolder
        End If
    End Function
    
    Sub ListFilesTest()
        Dim lastRow As Long
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
        Set Wks = Worksheets("Sheet1")
        Set Rng = Wks.Range("A1:B1")
        lastRow = Wks.Cells(Rows.Count, "A").End(xlUp).Row
        
        FileFilter = InputBox("Enter the Full or Partial name of the Files to Find.")
        If FileFilter = "" Then Exit Sub
        
        FileFilter = "*" & FileFilter & "*"
        
        Rng.Resize(lastRow - Rng.Row + 1, 2).Clear
        
        ListFiles "G:\Test", Worksheets("Sheet1").Range("A1"), 1
    End Sub
    It works fine but I need to add flexible way to be able to search for more than a string ..
    for example if I have a file name "Copy Non-Contiguous Columns To Another Sheet In Different Order"
    If I searched the string 'non' it is ok and the file name appears in results
    If I searched the string 'order' it is ok and the file name appears in results
    But if I searched 'non order' the file name doesn't appear in results ...!!and this is the problem for me

    I mean it is ok if I used one string
    I need it to be flexible that I need sometimes to type two ot three or more strings ..
    So as in example I need to the file name "Copy Non-Contiguous Columns To Another Sheet In Different Order" to appear in results if I typed in inputbox the string 'order non copy' .. the order of strings may be from right to left or from left to right or scrambled ..and the case of letters is not sensitive

    Thanks advanced for help
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Search for file names and list them in column A

    Does putting the string like this help any?

    "non?order"
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Search for file names and list them in column A

    Thanks Dave for quick reply
    This method doesn't work .. or you mean you can edit the code so as to be able to search for file names using this method ..if so ok no problem using this method

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Search for file names and list them in column A

    Thank you very much for Mr. Leith Ross for this masterpiece... I appreciate his help in this code as this code is his masterpiece

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Search for file names and list them in column A

    Are you there Mr. Lewis?
    May you have a look at the code ..and tell me if my request is possible or not?

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Search for file names and list them in column A

    Hello Yasser,

    Yes, Yasser it can be done. To use multiple words in the title, I would need to create a UserForm to accept multiple search words and options. The UserForm inputs would then be used to create the FileFilter.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Search for file names and list them in column A

    Thanks a lot for reply Mr. Leith
    But I don't need results to appear on userform .. I need the same results in column with hyperlinks to the results ..this saves my time a lot
    Or you mean to use userform just for inputs instead of inputbox
    Regards

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Search for file names and list them in column A

    Hello Yasser,

    I did mean to use the UserForm just for inputs instead of the input box. It will allow more inputs to be combined in different ways for filtering.

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Search for file names and list them in column A

    Thanks Mr. Leith for reply
    I still wait for your reply
    Here's a code that search for all instances in column A and show the cells for results and hide other rows ..
    This may help to reach the goal
    Sub SearchStrings()
        Dim SearchStr As String
        Dim Str() As String
        Dim Matched As Boolean
        Dim Cel As Range, Rng As Range
        Dim LastRow As Long, I As Long
    
        SearchStr = InputBox("Please Insert Search String(s)", , "Please help")
        Str() = Split(LCase(SearchStr), " ")
        LastRow = Application.WorksheetFunction.Max(Range("A" & Rows.Count).End(3).Row, ActiveCell.Row + 1)
        
        Range("A1:A" & LastRow).EntireRow.Hidden = False
        For Each Cel In Range("A1:A" & LastRow)
            Matched = True
    
            For I = 0 To UBound(Str())
                If InStr(LCase(Cel.Value), Str(I)) = 0 Then
                    Matched = False
                    Exit For
                End If
            Next I
    
            If Matched Then
                If Rng Is Nothing Then
                    Set Rng = Cel
                Else
                    Set Rng = Union(Rng, Cel)
                End If
            End If
        Next Cel
    
        If Rng Is Nothing Then
            MsgBox "No Matching Cell Found."
        Else
            Range("A1:A" & LastRow).EntireRow.Hidden = True
            Rng.EntireRow.Hidden = False
        End If
    End Sub

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Search for file names and list them in column A

    Mr. Leith Ross
    Do you remember my thread and working on it ..?
    Thank advanced for your help

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Search for file names and list them in column A

    Bump ... Any help

+ 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. Search a range Last Names for age & return the value to a List of FULL NAMES
    By LunarLights in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-16-2014, 05:02 PM
  2. Replies: 3
    Last Post: 04-16-2013, 01:44 PM
  3. [SOLVED] Search a list of names and automatically return any names not already included in table
    By bishbash89 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-21-2013, 09:06 AM
  4. Replies: 2
    Last Post: 10-08-2012, 12:49 PM
  5. Search and Copy files from a column of file names excel vba
    By net4earning in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-18-2011, 07:58 AM
  6. Search and Copy files from a list of file names excel vba programming
    By aashishnawal in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-21-2010, 09:27 AM
  7. how do I search a word or pdf for a list of names in a CSV file?
    By Martin Crane in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-30-2008, 05:16 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