Hyperlink Search Macro

Markus250

New Member
Joined
Aug 30, 2006
Messages
48
Is it possible to make a macro that can search for a file with the same name as a cell's text and then link that cell to the file?

Example, if I had cells that said "One" "Two" "Three" "Four" and "Five" and had a folder in my C drive with 5 files names "One.jpg"... etc could I make the macro search a folder for that file and link to it?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
My knowledge on macro's is not very broad, can you explain what it is each line of your code does, it doesn't seem to work for me.
 
Upvote 0
Well, to follow this one, you might need to broaden your experience. It's not that straight-forward, and this isn't a Macro, to be sure.

Here's an overview of recursion:

http://www.microsoft.com/technet/scriptcenter/guide/sas_vbs_kove.mspx?mfr=true

Still, you have the power of Google-searching, and the help files and MSDN (e.g., lookup Dir).

It works as advertised. So, instead of me trudging through this thing, line-by-line, which I have already done, several times, perhaps you could ask a detailed question as to which line you are not following?
 
Upvote 0
Okay, maybe this will be helpful to yourself and others...

I have commented every single line of code to varying extents, in the first routine I linked to in this thread:

Code:
'Name of Sub Routine
Sub foobar()
'Late Bind Variable for the File Scripting Object
Dim fso As Object
'String Variable for working with the Dir() function
Dim strName As String
'Array of Strings Variable - it's 2d, and Long Integer Variable
Dim strArr(1 To 65536, 1 To 1) As String, i As Long

'String Constant for root Search Folder
Const strDir As String = "C:\temp"
'This is our search term constant
Const searchTerm As String = "production"

'Locate first file with our search term and .xls extension
'Within our root folder
Let strName = Dir$(strDir & "\*" & searchTerm & "*.xls")
'Start looking for the next files
Do While strName <> vbNullString
    'Use a counter, to populate our 1-based, 2d Array of Strings
    Let i = i + 1
    'Popupulate the appropriate element within the Array
    Let strArr(i, 1) = strDir & "\" & strName
    'Find the next matching file
    Let strName = Dir$()
'Keep going through root folder
Loop
'Late bind with FSO, we're using this to 'walk through' subfolders
Set fso = CreateObject("Scripting.FileSystemObject")
'Call our recursive procedure
'We Marshall our variables byRef as we're going to manipulate them
'This allows us to avoid using Public Variables
Call recurseSubFolders(fso.GetFolder(strDir), strArr(), i, searchTerm)
'We're done with FSO, terminate it
Set fso = Nothing
'Lets make sure we actually found at least one match
If i > 0 Then
    'Pass our 2d Array of Strings to the Worksheet
    Range("A1").Resize(i).Value = strArr
'End of our conditional
End If
'End of our routine
End Sub

'Here's our recursive Sub Routine, we use this to walk Sub Directories
Private Sub recurseSubFolders(ByRef Folder As Object, _
    ByRef strArr() As String, _
    ByRef i As Long, _
    ByRef searchTerm As String)
'Late bind to Work with FSO's SubFolder Object
Dim SubFolder As Object
'String Variable for working with the Dir() function
Dim strName As String
'Loop for iterating through each SubFolder Object in the Root Folder
For Each SubFolder In Folder.SubFolders
    'Use Dir() to return a potential match
    Let strName = Dir$(SubFolder.Path & "\*" & searchTerm & "*.xls")
    'Start looking for the next files
    Do While strName <> vbNullString
        'Use a counter, to populate our 1-based, 2d Array of Strings
        Let i = i + 1
        'Popupulate the appropriate element within the Array
        Let strArr(i, 1) = SubFolder.Path & "\" & strName
        'Find the next matching file
        Let strName = Dir$()
    'Keep going through the Subfolder folder
    Loop
    'Call the recursive routine, we want to walk through Subfolders
    'Of each Subfolder, and so forth, capturing the entire tree of Folders
    Call recurseSubFolders(SubFolder, strArr(), i, searchTerm)
'Next SubFolder Object
Next
'End of our routine
End Sub
It'll be easier to read in the VBA IDE. ;)
 
Upvote 0
Would it be possible to have it so the code opens a folder rather than a file?

Also, It seems you misunderstood me earlier in the topic now that I reread it. The code never needs to open multiple files at once, just a single file that matches the selected cell. (Well, now a single FOLDER that matches the selected cell.)
 
Upvote 0
Also, the open function you posted earlier is for opening spread sheets. Is there one that opens folders or files?
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,212
Members
449,214
Latest member
mr_ordinaryboy

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