halcyon27
halcyon27

Reputation: 169

Click CommandButton to display UserForm with URL link

I am trying to code CommandButton1 to display UserForm1. In Userform1 I would like a hyperlink that uses values from cells in the worksheet. I would like UserForm1 to unload when the link is clicked.

I will put down what I (think) I know. But I am missing most of the code. Specifically:

How do I actually display the link?

How do I code to dismiss UserForm1 when link is clicked?

Private Sub CommandButton_Click()
    UserForm1.Show
End Sub

Private Sub UserForm1_Click()
    Dim URL As Hyperlink
        URL = "http://www.example.com/" & Cells.Range("Q5") & "/index"
    ...
End Sub

Upvotes: 2

Views: 4356

Answers (1)

funk
funk

Reputation: 500

I would put a label in your UserForm.

Private Sub Label1_Click()
    Dim URL As String
    Dim cellvalue As Variant

    cellvalue = ActiveWorkbook.Sheets(1).Range("Q5").Value
    URL = "http://www.example.com/" & cellvalue & "/index"

    On Error GoTo urlnotopen
    ActiveWorkbook.FollowHyperlink Address:=URL, NewWindow:=True
    Unload UserForm
    Exit Sub
urlnotopen:
    MsgBox "Cannot open " & URL
End Sub

Regards

Upvotes: 2

Related Questions