+ Reply to Thread
Results 1 to 7 of 7

Search file and hyperlink

Hybrid View

matrix_xrs Search file and hyperlink 07-04-2012, 12:29 AM
smuzoen Re: Search file and hyperlink 07-04-2012, 01:50 AM
matrix_xrs Re: Search file and hyperlink 07-05-2012, 11:58 PM
smuzoen Re: Search file and hyperlink 07-06-2012, 12:31 AM
matrix_xrs Re: Search file and hyperlink 07-06-2012, 05:37 PM
smuzoen Re: Search file and hyperlink 07-07-2012, 02:25 AM
matrix_xrs Re: Search file and hyperlink 07-09-2012, 08:40 AM
  1. #1
    Registered User
    Join Date
    03-18-2012
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    42

    Search file and hyperlink

    Hi,

    I'm trying to write a macro that will search for a file in a directory and then hyperlink the file.

    example:

    in cell A1: i have a file name.

    Like it to search for the file name in My doc folder and then place the hyperlink in cell A2


    I know there is examples of filesearch but this is no longer available for 2007. could someone help me as i can't find anything that works for excel 2007.


    Thanks for the help.

  2. #2
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: Search file and hyperlink

    This is a versatile macro that allows you to nominate a directory (&/or subdirectory as well) and list all files and hyperlink them - in your case when inputbox asks for filename (which accepts wildcards * & #) simply click cell A1 - browse to your my docs directory and it will search - you will need to modify to your specific needs - if you need help modifying it let me know.
    
    Sub test()
        Dim myDir As String, temp(), myList, myExtension As String
        Dim SearchSubFolders As Boolean, Rtn As Integer, msg As String
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show Then
                myDir = .SelectedItems(1)
            End If
        End With
        msg = "Enter File name and Extension" & vbLf & "following wild" & _
        " cards can be used" & vbLf & "* # ?"
        myExtension = Application.InputBox(msg)
        If (myExtension = "False") + (myExtension = "") Then Exit Sub
        Rtn = MsgBox("Include Sub Folders ?", vbYesNo)
        SearchSubFolders = Rtn = 6
        myList = SearchFiles(myDir, myExtension, 0, temp(), SearchSubFolders)
        If Not IsError(myList) Then
        For k = LBound(myList, 2) To UBound(myList, 2)
        Sheets(1).Cells(k, 1) = myList(1, k) & "\" & myList(2, k)
        ActiveSheet.Hyperlinks.Add Anchor:=Cells(k, 1), Address:= _
             CStr(myList(1, k)) & "\" & CStr(myList(2, k)), TextToDisplay:=CStr(myList(1, k)) & "\" & CStr(myList(2, k))
        Next
            'Sheets(1).Cells(1).Resize(UBound(myList, 2), 2).Value = myList
            'Application.Transpose (myList)
        Else
            MsgBox "No file found"
        End If
    End Sub
     
     
    Private Function SearchFiles(myDir As String _
        , myFileName As String, n As Long, myList() _
        , Optional SearchSub As Boolean = False) As Variant
        Dim fso As Object, myFolder As Object, myFile As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        For Each myFile In fso.getfolder(myDir).Files
            Select Case myFile.Attributes
            Case 2, 4, 6, 34
            Case Else
                If (Not myFile.Name Like "~$*") _
                * (myFile.Path & "\" & myFile.Name <> ThisWorkbook.FullName) _
                * (UCase(myFile.Name) Like UCase(myFileName)) Then
                    n = n + 1
                    ReDim Preserve myList(1 To 2, 1 To n)
                    myList(1, n) = myDir
                    myList(2, n) = myFile.Name
                End If
            End Select
        Next
        If SearchSub Then
            For Each myFolder In fso.getfolder(myDir).subFolders
                SearchFiles = SearchFiles(myFolder.Path, myFileName, _
                n, myList, SearchSub)
            Next
        End If
        SearchFiles = IIf(n > 0, myList, CVErr(xlErrRef))
    End Function
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated?

  3. #3
    Registered User
    Join Date
    03-18-2012
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Search file and hyperlink

    wow thanks Anthony this is great.

    would you be able to tweak the code so that it can loop through all of cells in collumn A and do a search in a folder and if there is file place the hyperlink in collumn B.

    sorry i'm not that good at tweaking codes.

    thank you very much.

  4. #4
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: Search file and hyperlink

    Do you mean that there will be directory names in Column A and the code is to search the directory name in Column A cells and then place all the files found in Column B and the Column B files are hyperlinked.
    Could you perhaps just upload a sample worksheet just showing how you want the sheet to appear - I think I know what you mean however a sample worksheet would make it quick and easier if I knew exactly what you wanted. Just select Go Advanced, Manage Attachments then select the sample workbook to upload it to your post.

  5. #5
    Registered User
    Join Date
    03-18-2012
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Search file and hyperlink

    Hi Anthony,

    Thanks very much for your help. I have uploaded a sample workbook of what I would like to achieve. please let me know if you have any questions.

    thanks
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: Search file and hyperlink

    The code will first ask you to browse to a directory to search - the code will always search any subdirectories as well - if a file is found that matches like the files you have listed in Column C it will place a hyperlink to the file - if the file cannot be matched to any file in directory or subdirectories then "Not Found" is placed next to the file name. I do not change the names of the file in column C - I just leave it as is - this could be change so it displays the actual filename - see code in workbook for instructions how to make the files in column C to be changed to match the the exact filename. Let me know if you have any problems. There is a button on sheet to start the process.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    03-18-2012
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Search file and hyperlink

    that is exactly what I needed and it works really well. thanks for taking the time to help me out.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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