Hyperlinks in MS Excel

Good day Experts,

I'm working on a project now where I create many Excel spreadsheets from database exports. One of the columns in the data that I get from the database is a UNC path, for example:  \\servername\share name\folder 1\folder 2\.

I'm sending these Excel sheets to users.  Is there any way that I can easily convert the UNC path column in Excel so that all the fields (with UNC paths) in this column are hyperlinks that point to the UNC path in the field value?

Who is Participating?
redmondbConnect With a Mentor Commented:
Hi. PantoffelSlippers.

Please see attached. It checks column A of Sheet1 (both easily changed) for any cells starting with "\\". These cells are then converted to Hyperlink formulas. (Let me know if you'd prefer not to use formulas.)

The code is...
Option Explicit

Sub UNC_to_Hyperlink()
Dim i         As Long
Dim xLast_Row As Long
Dim xColumn   As String
Dim xArray    As Variant ' Array holding the input data.

xColumn = "A"

xLast_Row = ActiveSheet.UsedRange.Cells(1, 1).Row + ActiveSheet.UsedRange.Rows.Count - 1
If xLast_Row < 2 Then
    MsgBox ("No data found in " & ActiveSheet.Parent.Name & ". Run cancelled.")
    Exit Sub
End If

xArray = Range(xColumn & "1:" & xColumn & xLast_Row)

For i = 1 To xLast_Row                              ' Work down column B.
    If Mid(xArray(i, 1), 1, 2) = "\\" Then
        xArray(i, 1) = "=HYPERLINK(""" & xArray(i, 1) & """)"
    End If

Range(xColumn & "1:" & xColumn & xLast_Row) = xArray

End Sub

Open in new window

PantoffelSlippersAuthor Commented:
Thanks Brian - I'll check it out now.
PantoffelSlippersAuthor Commented:
Seems like it's working!

I can also just use the "=Hyperlink" formula straight in my worksheet without a Macro.  I was looking for something like that - not sure why I couldn't find it.

Thanks Brian!
PantoffelSlippersAuthor Commented:
Excellent answer - with a sample even!
Naresh PatelTraderCommented:
Where are you Sir redmondb,

Not seen you on EE For long.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.