Excel VBA: How to autocreate hyperlink from cell value?

怎甘沉沦 提交于 2019-12-10 21:43:44

问题


I have a table called Table1

In Column B, I have the ticket number. e.g: 76537434

Requirement: when any change happens in any cell in column B, that cell (Target cell) to be changed into a hyperlink such that the hyperlink address would be example.com/id=76537434

Cell value i.e. 76537434 must remain the same


回答1:


Add this event handler to your worksheet's code module:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column <> 2 Then Exit Sub
    Target.Hyperlinks.Delete ' or Target.ClearHyperlinks to conserve the formatting
    Me.Hyperlinks.Add Target, "http://example.com/id=" & Target.value
End Sub



回答2:


The following Worksheet_Change event should be able to solve your problem:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

Dim cell As Range
Dim tmp As String
If Intersect(Range("B:B"), Target) Is Nothing Then Exit Sub

For Each cell In Target
    If cell.Column = 2 Then
        Application.EnableEvents = False
        tmp = cell.Value2
        cell.Parent.Hyperlinks.Add _
            Anchor:=Cells(cell.Row, 2), _
            Address:="http://example.com/id=" & tmp, _
            TextToDisplay:=tmp
        Application.EnableEvents = True
    End If
Next cell

End Sub

Note, that you must copy it to the sheet and not into a separate module.




回答3:


=HYPERLINK(E14&F14,"Name")

where cell E14 contains "http://www.example.com/id=" and cell F14 contains "76537434". This soultions doesn't need VBA macros.



来源:https://stackoverflow.com/questions/43092315/excel-vba-how-to-autocreate-hyperlink-from-cell-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!