Excel Developer Tip


Return to The Spreadsheet Page

Excel page

Tip archives

Adding a Hyperlink to a UserForm

If you use Excel 97, you probably know that the Insert Hyperlink command (or Ctrl+K) lets you insert a hyperlink into any cell. But if you've tried to add a hyperlink to a UserForm, you may have come up empty-handed.

There is no direct way to add a hyperlink to a UserForm, but you can simulate one by using the techniques described here. The figure below shows an example.

hyperlink.gif (3105 bytes)

  1. Add a Label object and enter some text for its Caption
  2. Make the Label blue and underlined so it resembles a typical hyperlink. You might also want to set its font to Courier New.
  3. None of the standard mouse pointers resembles a pointing hand, so set the Label's MousePointer property to: 99 - fmMousePointerCustom
  4. Specify the cursor file for the Label's MouseIcon image. If you don't have a cursor file that resembles a pointing hand, click here to download a file named hand.cur.
  5. Double-click the Label and enter an event-handler subroutine for its Click event. The FollowHyperlink method is what makes it work. Here's an example of such a subroutine:
Private Sub Label1_Click()
    Link = "http://www.whitehouse.gov"
    On Error GoTo NoCanDo
    ActiveWorkbook.FollowHyperlink Address:=Link, NewWindow:=True
    Unload Me
    Exit Sub
NoCanDo:
    MsgBox "Cannot open " & Link
End Sub

To create a "mail to" hyperlink, use a statement like this:

    Link = "mailto:president@whitehouse.gov"

Download an Example

I prepared a simple example that contains a UserForm that has two hyperlinks.