+ Reply to Thread
Results 1 to 10 of 10

VBA to Search for files in a Folder and Opens it

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-01-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    136

    VBA to Search for files in a Folder and Opens it

    I have a spreadsheet and the content has different file names that are arranged in a single Column. For example, in Column A, I have the following file names.

    A1: Math2.xlsx
    A2: Math1.xlsx
    A3: Eng1.xlsx

    These files are located in the directory C:/Projects/Sample

    But the problem is this: Under the Sample folder, I have so many other folders created there (about 10). Hence I don’t know exactly the location of the files listed above in the Sub Folders.

    I need an excel macro that can search for the files listed from A1 to A (whatever) and show me where it is exactly located under the Sample Sub-Folders and open it when I click on it. For example in A1 if I click on Math2.xlsx, it will show me the sub folder (Under Sample Folder) where it is located and opens it up.
    Is this possible?

    Thanks in advance so much for your time.
    Last edited by vnzerem; 02-04-2014 at 12:37 PM.

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

    Re: VBA to Search for files in a Folder and Opens it

    Hello vnzerem,

    Add a new VBA module to your workbook. Copy and paste the code below into it. Select the sheet with the file names and run the macro "LinkFiles". This will search the parent folder and all sub folders and their folders for the file name. When found, a hyperlink will be added to the cell. Hover the mouse over a file name on the worksheet and you will see the full path to file. Click the cell and it will open the file.

    Function FindMyFile(ByVal ParentFolder As String, ByVal FileName As String, Optional FoundFile As String)
    
        Dim colFolders As New Collection
        Dim FolderItem As Variant
        
            ParentFolder = IIf(Right(ParentFolder, 1) <> "\", ParentFolder & "\", ParentFolder)
            
            FolderItem = Dir(ParentFolder, vbDirectory + vbHidden)
            
                Do
                    If (GetAttr(ParentFolder & FolderItem) And vbDirectory) <> 0 Then
                       If FolderItem <> "." And FolderItem <> ".." Then
                           colFolders.Add ParentFolder & FolderItem
                       End If
                    End If
                   
                    FolderItem = Dir()
                    
                    If FolderItem = "" Then Exit Do
                    
                    If LCase(FolderItem) = LCase(FileName) Then
                        FoundFile = ParentFolder & FolderItem
                    End If
                Loop
                
                If Not colFolders Is Nothing Then
                    For Each FolderItem In colFolders
                        Call FindMyFile(FolderItem, FileName, FoundFile)
                    Next FolderItem
                End If
            
            FindMyFile = FoundFile
            
    End Function
    
    Sub LinkFiles()
    
        Dim Cell         As Range
        Dim FilePath     As String
        Dim ParentFolder As String
        Dim Rng          As Range
        Dim RngEnd       As Range
        Dim Wks          As Worksheet
        
            ParentFolder = "C:\Projects\Sample"
            
            Set Wks = ActiveSheet
            
            Set Rng = Wks.Range("A1")
            
                Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
                If RngEnd.Row < Rng.Row Then Exit Sub
                
                For Each Cell In Wks.Range(Rng, RngEnd)
                    FilePath = FindMyFile(ParentFolder, Cell)
                    
                    If Cell.Hyperlinks.Count <> 0 Then Cell.Hyperlinks(1).Delete
                    
                    If FilePath <> "" Then
                        Cell.Hyperlinks.Add Anchor:=Cell, Address:=FilePath, TextToDisplay:=Cell.Text
                    End If
                Next Cell
            
    End Sub
    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!)

  3. #3
    Forum Contributor
    Join Date
    10-01-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    136

    Re: VBA to Search for files in a Folder and Opens it

    All I can say is WOW !!!
    You are simply Amazing !!!
    Thanks a bunch !!

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

    Re: VBA to Search for files in a Folder and Opens it

    Hello vnzerem,

    You're welcome. That was interesting and fun project to do.

  5. #5
    Forum Contributor
    Join Date
    10-01-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    136

    Re: VBA to Search for files in a Folder and Opens it

    It is really nice watching how you combined the function with the sub-routine to achieve the results.

  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,259

    Re: VBA to Search for files in a Folder and Opens it

    Hello vnzerem,

    Recursive functions like FindMyFile are powerful programming tools. The draw backs are they can difficult to implement and understand. Glad you like it.

  7. #7
    Forum Contributor
    Join Date
    10-01-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    136

    Re: VBA to Search for files in a Folder and Opens it

    @Leith Ross: I need to study more on that function....It is awesome!!

  8. #8
    Forum Contributor
    Join Date
    10-01-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    136

    Re: VBA to Search for files in a Folder and Opens it

    One quick question, how can I modify the code to display "found" on Column B for found files and "not found" on Column B for files not found.

  9. #9
    Forum Contributor
    Join Date
    10-01-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    136

    Re: VBA to Search for files in a Folder and Opens it

    Don't worry Leith, I was able to add it into the code. Thanks!

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

    Re: VBA to Search for files in a Folder and Opens it

    Hello vnzerem,

    Glad to see you were able to solve your problem. If you have any other questions, let me know.

+ 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] Excel VBA code for selecting a folder then search & open specified files in that folder
    By Excelrookie_1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-27-2021, 03:09 AM
  2. Replies: 4
    Last Post: 05-20-2013, 08:31 AM
  3. Search Folder containing XLS files
    By sini in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-02-2011, 04:29 PM
  4. 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
  5. Excel opens all files in My Documents folder
    By Dave B in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-04-2005, 11:06 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