Insert Hyperlink

tobyhutton1234

New Member
Joined
Dec 21, 2010
Messages
24
Hi

I'm trying to work out how to insert a hyperlink using vba.

I have a form where a user will enter theor username.

Once they enter their username and clicked ok it will apper in the cell where the box appers.

Heres my code sao far.

Code:
Private Sub CommandButton1_Click()
    If Len(Me.CamUser & "") = 0 Then
        MsgBox "You enter your username before you continue.", vbInformation + vbOnly, "Username"
            Cancel = True
        Exit Sub
    Else
        ActiveCell.Value = ("http://website.com/default.asp?ID=" & CamUser)
        Unload Me
    End If
End Sub
CamUser is the name of the textbox. I need the text to display in the cell to say (whatever their username) and a link to take them to a website.

Any ideas thanks.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try something like:

Rich (BB code):
Private Sub CommandButton1_Click()
    If Len(Me.CamUser.value & "") = 0 Then
        MsgBox "You enter your username before you continue.", _
            vbInformation + vbOKOnly
        Exit Sub
    End If
 
    With ActiveSheet
        .Hyperlinks.Add Anchor:=.Range("D4"), Address:= _
            "http://website.com/default.asp?ID=" & Me.CamUser.value, _
             TextToDisplay:=  Me.CamUser.value
        Unload Me
    End With
End Sub

You might want to consider using a Cell Address and possibly an explicit SheetName reference. Unless you are controlling ActiveCell in your code, the link will be placed in whatever cell happened to be active when the userform was opened. Might possibly overwrite data.

Good luck!
 
Upvote 0
Thank you that codxe works fine but it will only place into the cell range thats specified in Ancor:= .Range("D4"). It need it to go into the cell that clicked. This could be any cell in column D.

Tnanks
 
Upvote 0
You can modify this line:

Rich (BB code):
.Hyperlinks.Add Anchor:=.Range("D4"), Address:= _

To this, for the ActiveCell
Rich (BB code):
.Hyperlinks.Add Anchor:=ActiveCell, Address:= _

Or this, for the Intersection of Column D and the ActiveCell's Row.
Rich (BB code):
.Hyperlinks.Add Anchor:=.Cells(ActiveCell.row, 4), Address:= _

Hope this helps.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top