Variable Hyperlinks
Excel provides an easy way to create a table of hyperlinks and even save the workbook as a web page. The built-in method for creating a hyperlink is Insert > Hyperlink (Ctrl+K). The problem with this method is mass changes, like renaming your server. Editing each one (if you have a lot) would be out of the question, but you could certainly write a macro to do it.
One of the engineers in my office asked why we couldn't just define an environment variable somewhere and reference that. Well, we can, in a way.
=HYPERLINK(VLOOKUP("%MainServer%",EnvVars,2,FALSE)& VLOOKUP("%docs%",EnvVars,2,FALSE)&"UserGuide.pdf")
By using the HYPERLINK worksheet function rather than Insert > Hyperlink, I can put some variables in a table and use VLOOKUPs to build the link.
Before

After

That's pretty good. I can put my environment variables into their own workbook so I can refer to them from many different sheets and workbooks. One downside is that I can't specify a tooltip when using the HYPERLINK worksheet function. Another downside is that the formula can get a bit long and hard to manage.
I'd like to create my own version of HYPERLINK, but I don't know how. I'm just going to go right ahead and say it's impossible to return a hyperlink from function called from a cell. There. Now one of you can prove me wrong. Here's my best attempt so far - but it has problems.
ToolTip As String, _
ParamArray Paths() As Variant) As String
Application.Volatile
Dim i As Long
Dim sLink As String
Dim oLink As Hyperlink
For i = LBound(Paths) To UBound(Paths)
sLink = sLink & ConvertVar(Paths(i))
Next i
If Len(FriendlyName) = 0 Then
FriendlyName = sLink
End If
If Len(ToolTip) = 0 Then
ToolTip = sLink
End If
Application.Caller.Hyperlinks.Add Application.Caller, _
sLink, , ToolTip, FriendlyName
VIPERLINK = FriendlyName
End Function
Private Function ConvertVar(ByVal sVar As String) As String
Dim rFound As Range
Set rFound = Sheet1.Range("EnvVars").Find(sVar, , xlValues, xlWhole)
If Not rFound Is Nothing Then
ConvertVar = rFound.Offset(0, 1).Value
Else
ConvertVar = sVar
End If
End Function
Called like =VIPERLINK("","Click here","%MainServer%","%docs%","UserGuide.pdf").
ConvertVar is just a placeholder function right now to prove the concept. It definitely needs to be refactored. Problem #1 is that I'm changing a property of the cell that isn't the Value property. That's generally a no-no, but we're all about pushing the envelope here at DDoE. Note that my UDF doesn't return a Hyperlink object, but rather a String.
Problem #2 is that deleting the contents of the cell doesn't clear the hyperlink-like formatting (blue with underline). Similarly, (problem #3 if you're counting) replacing the formula with simple text doesn't delete the hyperlink, it just changes the TextToDisplay.
Maybe those shouldn't be deal-breakers, but I don't think I'm getting that much from this function. I get a Tooltip - big whoop. I get a little-easier-to-write function - nice, but necessary? Maybe I just forget about the Tooltip and make a function that returns the link string.
Dim i As Long
Dim sLink As String
For i = LBound(Paths) To UBound(Paths)
sLink = sLink & ConvertVar(Paths(i))
Next i
ENVIROLINK = sLink
End Function
Used like =HYPERLINK(envirolink("%MainServer%","%docs%","UserGuide.pdf"))
Then I don't have to worry about orphaned formatting and I get a slightly more manageable worksheet formula. What do you think? Worth it?


