Home » excel » excel – Create hyperlink using information from two columns

excel – Create hyperlink using information from two columns

Posted by: admin May 14, 2020 Leave a comment

Questions:

Using Excel 2010, in each row I would like to create a hyperlink in column G.

This column already holds information which shall be used as the displayed text (or the friendly name) of the hyperlink.

To make the hyperlink point to the correct place, it needs the number that column M holds. The matching information is always in the same row.

So basically, this could just be a merge of the two bits of information in the two cells of the specific row.

This is what I started with:

    Sub Macro2()
'
' Macro2 Macro
'
Dim Name As String
Dim Branch As String

' Combination of two rows
Dim CombineRow As Range

Dim cell As Range
Dim row As Range
Dim Branch_ID As Range

Set Branch_ID = Worksheets("Default").Range("M2")

Set CombineRow = Range("G2:M2")

For Each row In CombineRow.Rows
     For Each cell In row.Cells
        Branch = Branch_ID.Cells.Value
        Name = Name_ID.Cells.Value
        ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
            "https://www.example.com/", SubAddress:= _
            "something:", _
            TextToDisplay:=Name
        Next cell
        ' relative one row down
        Set Branch_ID = Branch_ID.Offset(1, 0)
        Set Name_ID = Branch_ID.Offset(1, 0)
    Next row
End Sub

What I don’t know is how to attach the value of column M (e.g. 600021610) to the end of the hyperlink?

Am I on the correct path here?

EDIT:

What I have now is this:

Sub Macro2()
'
' Macro2 Macro
'
Dim Branch As String
Dim Name As String

' combine two rows
Dim CombineRow As Range

Dim cell As Range
Dim row As Range
Dim Branch_ID As Range
Dim Name_ID As Range

Set Branch_ID = Worksheets("Default").Range("M2")
Set Name_ID = Worksheets("Default").Range("G2")

Set CombineRow = Range("G2:M2")

For Each row In CombineRow.Rows
    For Each cell In row.Cells
        Branch = Branch_ID.Cells.Value
        Name = Name_ID.Cells.Value
        ActiveSheet.Hyperlinks.Add Anchor:=Name_ID, Address:= _
            "https://www.example.com/", SubAddress:= _
            "something" & Branch, _
            TextToDisplay:=Name
        Next cell
        Set Branch_ID = Branch_ID.Offset(1, 0)
        Set Name_ID = Name_ID.Offset(1, 0)
    Next row
End Sub

This works, but only for the first row. Why doesn’t it do the loops? Is the CombineRow really correct like this, or must it be this: G:G;M:M or something similar?

I could also imagine using a Do ... While loop, because putting in an end_tag wouldn’t be a problem.

How to&Answers:

Try making the argument to the hyperlinks.add use a variable.

dim addressStr as string
dim targetRng as Range

For Each row In CombineRow.Rows
     For Each cell In row.Cells
        Branch = Branch_ID.Cells.Value
        Name = Name_ID.Cells.Value

        set targetRng = range("A1")   'change this to whatever you want it to be each loop
        addressStr = "https://www.example.com/" & cells(cell.row,15).value

        ActiveSheet.Hyperlinks.Add Anchor:=targetRng , Address:= _
            addressStr, SubAddress:= _
            "something:", _
            TextToDisplay:=Name
        Next cell
        ' relative one row down
        Set Branch_ID = Branch_ID.Offset(1, 0)
        Set Name_ID = Branch_ID.Offset(1, 0)
    Next row<BR>
End Sub<BR>

Also, you are setting Name_ID and Branch_ID to the same value in your offsets.


In your loop, you will only loop through 1 row (because your range is all within row 2). So your only loop is the cells – you do NOT ever set the branch/name ID’s differently because they occur after your loop:

For Each cell In row.Cells
    'stuff
Next cell

Set Branch_ID = Branch_ID.Offset(1, 0)
Set Name_ID = Name_ID.Offset(1, 0)

So each time you call the hyperlink function you are using the same Name_ID range. You may also want to make those Offset(0,1) because Offset is Offset(row,column) and it seems you are iterating over a horizontal range.

Answer:

You could simply use & operator to concatenate string and create the URL.

addressStr = "https://www.example.com/" & <<Specify Cell for M Column and Row>>

Answer:

It works!

Thank all of you for your help.

The soltuion was not to put (G2:M2) because that results in just creating seven hyperlinks.

Instead, just put (G:M) and and IF around it to prevent it from creating nonsense hyperlinks when cells are empty already.

    Sub Macro2()
'
' Macro2 Macro
'
Dim Branch As String
Dim Name As String

' combine two rows
Dim CombineRow As Range

Dim cell As Range
Dim row As Range
Dim Branch_ID As Range
Dim Name_ID As Range

Set Branch_ID = Worksheets("Default").Range("M2")
Set Name_ID = Worksheets("Default").Range("G2")

Set CombineRow = Range("G:M")

For Each row In CombineRow.Rows
For Each cell In row.Cells

    Branch = Branch_ID.Cells.Value
    Name = Name_ID.Cells.Value

    If Name <> "" Then

        ActiveSheet.Hyperlinks.Add Anchor:=Name_ID, Address:= _
            "https://www.example.com", SubAddress:= _
            "something" & Branch, _
            TextToDisplay:=Name

    Set Branch_ID = Branch_ID.Offset(1, 0)
    Set Name_ID = Name_ID.Offset(1, 0)

    End If

    Next cell

    Next row

End Sub

I am DEAD sure there is lots of room for improvement, especially in the IF construction.
But so far, it works. This doesn’t mean that anybody should hold back their comments on improveing this ;). These were basically my first steps in VBA! 🙂