我需要一个解决方案的超链接在Excel中。如果我更改超链接的文本,我想使我的整个超链接地址自动更新。
例如,超链接的TextToDisplay
是:
D:/attachment/1000.jpg
然后将超链接的TextToDisplay
更改为:
D:/attachment/1001.jpg
如果我更改超链接的文本,超链接地址将与显示文本相同。
发布于 2019-07-16 05:45:19
公式解
最简单的方法是使用助手列。例如,在A列中写入URL,并在B列中使用以下公式创建超链接。每次您更改A列中的URL时,它都会相应地更改超链接。
=HYPERLINK(A:A)
VBA溶液
或者,您可以在VBA中使用Worksheet_Change
事件。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
For Each Cell In Target
If Cell.Hyperlinks.Count = 1 Then
Cell.Hyperlinks(1).Address = Cell.Value
End If
Next Cell
End Sub
请注意,此代码适用于工作表上的所有超链接。如果要将其限制在特定范围内,请执行以下更改:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Set AffectedRange = Intersect(Target, Me.Range("A:A"))
'limits the code to hyperlinks in column A.
'hyperlinks in other cells than column a are not changed
If Not AffectedRange Is Nothing Then
Dim Cell As Range
For Each Cell In AffectedRange
If Cell.Hyperlinks.Count = 1 Then
Cell.Hyperlinks(1).Address = Cell.Value
End If
Next Cell
End If
End Sub
https://stackoverflow.com/questions/57050432
复制相似问题