Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

How to hyperlink to a file using VBA

0

I have a several spreadsheets that contain the names and network addresses of hundreds of files. Because I eventually want to merge the various spreadsheets in one worksheet with a Form as a coverpage.
How can I used VBA to sequence through the list of addresses on Sheet2 and create hyperlinks using the document name in column F, so that the formula for the hyperlink resides in the VBA Macro and not on the Spreadsheet.

I am using a formula using HYPERLINK & VLOOKUP. Sheet1 Contains the data and Document names. Sheet2 (Hidden), contains link addresses within the workbook so I use VLOOKUP there to build the hyperlinks. My formula also uses the IF statement to leave an empty cell if nothing was found:

=IF(ISTEXT(G2),HYPERLINK(VLOOKUP(G2,Sheet2!C:D,2,0),G2),"")

Can I do the same thing using VBA?

Answer
Discuss

Discussion

Hi again G1zmos.

Do you expect the user to know the exact file names or should they be presented with options based on the part nane they type in? Is the file to be opened or the hyperlink simply written to a cell?
John_Ru (rep: 6142) Feb 3, '23 at 1:11 pm
The exact file names are used as the 'FriendlyName' in the hyperlink. I didn't want the formulas in the spreadsheets, just hyperlinks using the 'FileName'.
I don't want the file opened using the Macro, just the hyperlinks written to the cell. I hope this answers your question.
Thanks
G1zmos (rep: 2) Feb 3, '23 at 1:15 pm
Okay but do you want a liat of friendly names presented to the user? Let me know please and I'll try to find time over the weekend 
John_Ru (rep: 6142) Feb 3, '23 at 1:57 pm
In my example file, the file name is in column G. Once I can insert the hyperlinks, I will not require the first column. Basically the format on Sheet 1 is the format for all of the spreadsheets that I want to end up with. there is no requirement for two columns with the document file name. But Yes I want a list of the FriendlyName as per my updated example file.
G1zmos (rep: 2) Feb 3, '23 at 2:15 pm
Okay, will look at new file and try over the weekend hopefully. 
John_Ru (rep: 6142) Feb 3, '23 at 2:35 pm
Hi again G1zmos. Did you see my Answer (below) from Sunday?
John_Ru (rep: 6142) Feb 8, '23 at 3:18 am
Still no comment?
John_Ru (rep: 6142) Feb 22, '23 at 9:39 am
Add to Discussion

Answers

0

G1zmos

This answer is based in part on Don's tutorial Filter Data as you Type in Excel

In the revised file attached, this sheet macro launches a UserForm if a single cell in column G of Sheet1 is selected:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'do nothing if more than one cell selected or not in column G
If Target.CountLarge <> 1 Or Intersect(Target, Range("G:G")) Is Nothing Then Exit Sub

' otherwise open the form
UserForm1.Show

End Sub

This code sets up the form with a list of files from Sheet2 column C:

Private Sub UserForm_Initialize()

ListBox1.Clear
' add a caption about the selected cell
Label1.Caption = "Select a file to record in cell " & Selection.Address(0, 0)
' put cursor in the search textbox
TextBox1.SetFocus
' create a list of filenames...
With Sheet2
    Set filterRange = .Range("C2:C" & .Cells(.Rows.Count, 3).End(xlUp).Row)
End With
' .. by looping through cells
For Each Cl In filterRange.Cells
    ListBox1.AddItem CStr(Cl.Value)
Next Cl

End Sub

A textbox is enabled with a wildcard search like *<<entry>>* so you can type any part of the file name  (e.g. just 4) and this code filters that list as you type:

Private Sub TextBox1_Change()
Dim filterInput As String, filterRange As Range

filterInput = TextBox1.Text

With Sheet2
    Set filterRange = .Range("C2:C" & .Cells(.Rows.Count, 3).End(xlUp).Row)
End With

' filter the range
filterRange.AutoFilter Field:=1, Criteria1:="*" & filterInput & "*", VisibleDropDown:=False

' clear the list and repopulate but stop if nothing is found
ListBox1.Clear
On Error Resume Next
If filterRange.SpecialCells(xlCellTypeVisible) Is Nothing Then
    filterRange.AutoFilter
    Exit Sub
End If

For Each Cl In filterRange.SpecialCells(xlCellTypeVisible)
    ListBox1.AddItem CStr(Cl.Value)
Next Cl

End Sub

If you pick a file from the ListBox, the Submit button turns green and, if you click that, this macro runs:

Private Sub CB_Submit_Click()

'Find the chosen file
For Each Cl In filterRange.SpecialCells(xlCellTypeVisible)
    If Cl.Text = ListBox1.Text Then
        'if found, write file choice to selected cell (column G -6)
        Selection.Value = ListBox1.Text
        ' add hyperlink to column A
        Sheet2.Hyperlinks.Add Anchor:=Selection.Offset(0, -6), _
            Address:=Cl.Offset(0, -1).Value & "\" & ListBox1.Text, _
            TextToDisplay:=ListBox1.Text
        Exit For
    End If
Next Cl
' clear the filter and close UF
filterRange.AutoFilter
Unload Me

End Sub

which puts the friendly file name in column G and a hyperlink in column A. A few clicks do it all!

The code to add the hyperlink is in bold above.

Hope this makes sense and works as a solution for you. If so, please remember to mark this Answer as Selected.

Discuss

Discussion

Did you look at this, G1zmos?
John_Ru (rep: 6142) Feb 28, '23 at 2:43 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login