Autolink

pnasz

New Member
Joined
Apr 16, 2012
Messages
19
i have image code 02-71234
which i want to autolink to the folder on the network.
Folder contain lot of file but i want to open the file related to the image code, for example for code 02-71234 i want to open the file 02-71234 (20.4 grams) which is present in a directory
 
yes thats what i want. can you help me to search for the file in the sub directories in the parent folder.

thanx in advance
 
Upvote 0
yes thats what i want. can you help me to search for the file in the sub directories in the parent folder.

thanx in advance

Below is some revised code that you could try.

Paste all this code into the Sheet Code Module of the Sheet where you will enter the Image Codes.
(Right-Click on the Sheet's Tab > View Code... to get to the Sheet Code Module.)

Edit the Path of the Folder to be searched and the Column you'll enter the Image Codes, if other than Column "A"

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Const sPath = "C:\TEST\"
    Dim sFilename As String, sCode As String
    
    sCode = Target.Text
    
    If Intersect(Target, Columns("A")) Is Nothing Or _
        Target.Count > 1 Or _
        sCode = vbNullString Then Exit Sub
        
    On Error GoTo CleanUp
    Application.EnableEvents = False
            
    sFilename = FindFileMatch(sPath, sCode & "*")
    If sFilename = vbNullString Then
        MsgBox "No matching files found for code " & sCode
    Else
        Me.Hyperlinks.Add _
            Anchor:=Target, Address:=sFilename, _
            TextToDisplay:=sFilename
    End If
CleanUp:
    Application.EnableEvents = True
End Sub

Private Function FindFileMatch(ByVal sPath As String, _ 
    ByVal sPattern As String) As String
[COLOR="Teal"]'---Searches sPath and its Subfolders for file matching sPattern
'---   returns first match[/COLOR]
    Dim sSubFolders() As String
    Dim sFile As String, sMatch As String
    Dim lCnt As Long, i As Long

    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    On Error Resume Next
    sFile = Dir(sPath & "*.*", vbDirectory)

    Do While Len(sFile) > 0
        If Left(sFile, 1) <> "." Then
            If (GetAttr(sPath & sFile) And vbDirectory) = vbDirectory Then
                lCnt = lCnt + 1
                ReDim Preserve sSubFolders(1 To lCnt)
                sSubFolders(lCnt) = sPath & sFile
            End If
            If sFile Like sPattern Then
                FindFileMatch = sPath & sFile
                Exit Function
            End If
        End If
        sFile = Dir
    Loop
    
    For i = 1 To lCnt
        sMatch = FindFileMatch(sSubFolders(i), sPattern)
        If sMatch <> vbNullString Then
            FindFileMatch = sMatch
            Exit Function
        End If
    Next i
    FindFileMatch = vbNullString
End Function
 
Upvote 0
i am not able to see the macro when i try to run f8.

also is it possible to search the file in subfolders.
 
Upvote 0
i am not able to see the macro when i try to run f8.

also is it possible to search the file in subfolders.

The code needs to be copied into a Sheet Code Module (not a Standard Module where macros are often placed). If you place this in a Standard Module, you'll get the error message you described in your earlier post.

The macro is triggered automatically when the User enters a value into any Cell in Column A. You can't (and shouldn't) run it using F8.

Here are instructions on where to paste the code...

Paste all this code into the Sheet Code Module of the Sheet where you will enter the Image Codes.
(Right-Click on the Sheet's Tab > View Code... to get to the Sheet Code Module.)
 
Upvote 0
getting error exit function not allowed in sub or property.

Did you make any changes to the code other than perhaps changing the Target folder from "C:\TEST\"?

It sounds like the Function FindFileMatch might have been changed to a Sub.
That won't work correctly since it needs to return a value.
 
Upvote 0
its working thanx a lot.

In the excel sheet in the cell its showing the link with the path of file.
Is it possible to show only file name with link.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top