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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 PatelFinancial AdviserCommented:
Where are you Sir redmondb,

Not seen you on EE For long.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.