How to Link Cells to Files/Folders using Excel VBA?

There are many cases where we want to have a Hyperlink on a cell that opens a document / image etc. In the following snippet we can see how that works

Sub Create_HyperLinks()

Dim i1 As Integer
Dim sA, sB As String

For i1 = 2 To Cells.SpecialCells(xlCellTypeLastCell).Row
    If LenB(Trim$(Cells(i1, 3).Value)) ;; 0 Then
        sA = Trim$(Cells(i1, 1).Value)
        sB = Trim$(Cells(i1, 2).Value)
        sA = "Compared_" ; sA ; "_" ; sB ; ".xls"
        Sheets(1).Range("C" ; i1).Hyperlinks.Add Cells(i1, 3), "CompareReports\" ; sA
    End If
Next i1


End Sub

Now, learn to write simple yet effective VB coding with our comprehensive and specialized Course on Excel Automation here.

Happy Excelling
Team Excelgoodies
www.Excelgoodies.com

Leave a Reply

Your email address will not be published. Required fields are marked *