简体   繁体   中英

Hyperlink Cell based on Value of another cell

I Have trawled the forums looking for a solution.

I have a code for creating a hyperlink based on the column B cell value. It works but only if the I run the sub whilst selecting the cell.

What I need is for the hyperlink to get automatically added if the cell in column H's value is "ok"

Sub Hyperlinks()

Dim r As Range
Dim FilePath As String

If Intersect(Columns("B"), Selection) Is Nothing Then Exit Sub

For Each r In Intersect(Selection, Range("B2:B" & _
Cells(Rows.Count, "B").End(xlUp).Row))
If r <> vbNullString Then

FilePath = "T:\BLUEMAC\Search Paths\PDF MASTER FOLDER\"

ActiveSheet.Hyperlinks.Add Anchor:=r, _
Address:=FilePath & r.Value & ".pdf", TextToDisplay:=r.Value
End If
Next r

End Sub

Any help would be greatly appreciated.

Change

If Intersect(Columns("B"), Selection) Is Nothing Then Exit Sub

For Each r In Intersect(Selection, Range("B2:B" & _
Cells(Rows.Count, "B").End(xlUp).Row))

To

For Each r In Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row)

And

ActiveSheet.Hyperlinks.Add Anchor:=r, _
Address:=FilePath & r.Value & ".pdf", TextToDisplay:=r.Value

To

If r.offset(0,6).value = "ok" then ActiveSheet.Hyperlinks.Add Anchor:=r, _
Address:=FilePath & r.Value & ".pdf", TextToDisplay:=r.Value

Like this?

Sub Hyperlinks()

Dim r As Range
Dim FilePath As String

If Intersect(Columns("B"), Selection) Is Nothing Then Exit Sub

For Each r In Intersect(Selection, Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row))
    If r <> vbNullString And LCase$(r.Offset(0, 6).value) = "ok" Then
        FilePath = "T:\BLUEMAC\Search Paths\PDF MASTER FOLDER\"
        ActiveSheet.Hyperlinks.Add Anchor:=r, _
             Address:=FilePath & r.Value & ".pdf", TextToDisplay:=r.Value
    End If
Next r

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM