Using HYPERLINK formula in VBA

Gauraw

New Member
Joined
Nov 20, 2015
Messages
15
Hi All,

I am having two columns - First column has Link (Url) and 2nd Column has Friendly Name.Using HYPERLINK formula,
I am able to create the Hyperlink in excel but to make it dynamic, I need to use VBA Code.

Kindly let me know how I can use the HyperLink formula - "HYPERLINK(I9,H9) " in VBA Code to achieve below Output.
UrlNameOutput
www.google.comGoogleGoogle
www.yahoo.comYahooYahoo
www.mrexcel.comMrExcelMrExcel

<tbody>
</tbody>

Please note the above table list is dynamic. Based on the filters selection, the urls and no . of urls can change.

With Regards,
Gauraw
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
What do you mean Dynamic?

I have URLs in column A. friendly names in column B and in C1 I type =HYPERLINK(A1,B1) and drag it down....

What result are you looking for?
 
Upvote 0
Thanks tygrrboi for ur response.

The Dynamic means these urls are getting populated based on Combo box selection for one entity. When different entity is selected from the combo box different set of Urls (in terms of names & Count) will be populated. Thus, I need to put the Hyperlink task into VBA Code and run whenever change event happens in a combo box.

Hope this makes things clear.


What do you mean Dynamic?

I have URLs in column A. friendly names in column B and in C1 I type =HYPERLINK(A1,B1) and drag it down....

What result are you looking for?
 
Upvote 0
Sub urldo()
Dim urltext As String


Range("g2").Select

Do Until ActiveCell.Value = ""

urltext = ActiveCell.Offset(0, 0).Value

urltext = Right(urltext, Len(urltext) - 4)


urltext = Left(urltext, Len(urltext) - 4)

ActiveCell.Offset(0, 2).Value = urltext
ActiveCell.Offset(1, 0).Select
Loop
End Sub

Give me a bit to see if I can get that to change the 3rd row to hyperlink.
 
Last edited:
Upvote 0
Thanks HockeyDiablo for the response.

Just for more clarity and making things simple. The URL and Name column will be always there.
Its just we need to reference these cells and come up the Hyperlink using VBA.



Sub urldo()
Dim urltext As String


Range("g2").Select

Do Until ActiveCell.Value = ""

urltext = ActiveCell.Offset(0, 0).Value

urltext = Right(urltext, Len(urltext) - 4)


urltext = Left(urltext, Len(urltext) - 4)

ActiveCell.Offset(0, 2).Value = urltext
ActiveCell.Offset(1, 0).Select
Loop
End Sub

Give me a bit to see if I can get that to change the 3rd row to hyperlink.
 
Upvote 0
Sub urldo()
Dim urltext As String
Dim newlink As String


Range("a2").Select

Do Until ActiveCell.Value = ""

urltext = ActiveCell.Offset(0, 0).Value

urltext = Right(urltext, Len(urltext) - 4)


urltext = Left(urltext, Len(urltext) - 4)

ActiveCell.Offset(0, 2).Value = urltext
If ActiveCell.Hyperlinks.Count = 1 Then
newlink = ActiveCell.Hyperlinks(1).Address
ActiveCell.Offset(0, 2).Hyperlinks.Add anchor:=ActiveCell.Offset(0, 2), Address:=ActiveCell.Offset(0, 2)
ActiveCell.Offset(0, 2).Hyperlinks(1).Address = newlink
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub

I changed the 3 columns to A,B,C for testing. The first row works, now I need to figure out how to get the others.
 
Upvote 0
Sub urldo()
Dim urltext As String
Dim newlink As String
Dim r As Range
Dim x As Range


Set r = Range("A2").End(xlDown)
For Each x In Range("A2", r)


urltext = x

If x.Hyperlinks.Count = 1 Then
newlink = x.Hyperlinks(1).Address
x.Offset(0, 2).Hyperlinks.Add anchor:=x.Offset(0, 2), Address:=x.Offset(0, 2)
x.Offset(0, 2).Hyperlinks(1).Address = newlink
End If

urltext = Right(urltext, Len(urltext) - 4)


urltext = Left(urltext, Len(urltext) - 4)

x.Offset(0, 2).Value = urltext
x.Offset(1, 0).Select

Next x



End Sub

Finished....
 
Upvote 0
Thank you HockeyDiablo for suggesting this piece of code. I used it as reference and created one as per my need.

Here is the code which I used for my tool :

Code:
[/COLOR]Sub AddHyperlink()


Dim i As Long
Dim Url As Long
Dim ws As Worksheet
Set ws = ActiveSheet


Url = Range("B" & Rows.Count).End(xlUp).Row


For i = 9 To Url 'set 9 to starting row


    ws.Hyperlinks.Add Range("E" & i), Cells(i, 9).Value, _
      , "Go to Requested site.", Cells(i, 8).Value
Next i


End Sub

Thanks Again !
 
Upvote 0
Hi All,

I am having two columns - First column has Link (Url) and 2nd Column has Friendly Name.Using HYPERLINK formula,
I am able to create the Hyperlink in excel but to make it dynamic, I need to use VBA Code.

Kindly let me know how I can use the HyperLink formula - "HYPERLINK(I9,H9) " in VBA Code to achieve below Output.
UrlNameOutput
www.google.comGoogleGoogle
www.yahoo.comYahooYahoo
www.mrexcel.comMrExcelMrExcel

<tbody>
</tbody>

Please note the above table list is dynamic. Based on the filters selection, the urls and no . of urls can change.

With Regards,
Gauraw



Before I applied the Hyperlinks.Add function which works well, I had naïvely tried the following code to insert a HYPERLINK formula in a cell from VBA but it does not work.
Sub testHL()
'Not working thus useless routine

myUrl = "www.google.com"
myName = "Google"
ActiveCell.Formula = "=HYPERLINK(""" & myUrl & """;""" & myName & """)"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,839
Messages
6,127,199
Members
449,368
Latest member
JayHo

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