如何基于2个单元格值在单元格中自动插入超链接

时间:2016-10-07 09:42:09

标签: excel vba excel-vba hyperlink

我想知道是否有人可以提供帮助。

我有一个宏,我用它作为更改记录如下:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If ActiveSheet.Name = "ChangeRecord" Then Exit Sub

Application.EnableEvents = False

UserName = Environ("USERNAME")

NewVal = Target.Value

Application.Undo

oldVal = Target.Value

lr = Sheets("ChangeRecord").Range("A" & Rows.Count).End(xlUp).Row + 1

Sheets("ChangeRecord").Range("A" & lr) = Now
Sheets("ChangeRecord").Range("B" & lr) = ActiveSheet.Name
Sheets("ChangeRecord").Range("C" & lr) = Target.Address
Sheets("ChangeRecord").Range("D" & lr) = oldVal
Sheets("ChangeRecord").Range("E" & lr) = NewVal
Sheets("ChangeRecord").Range("F" & lr) = UserName

Target = NewVal

Application.EnableEvents = True
End Sub

因此,更改会记录在一个名为ChangeRecord的单独表格中,这样就可以了。

我想要做的是在G列中添加一个超链接,它直接转到更改记录,因此,如果B列中的单元格值是另一个名为Scotland的工作表,则更改的单元格是$ A $ 21,然后在G列中自动创建的超链接将我带到该工作表上的那个单元格。

非常感谢任何帮助。 最好的问候

2 个答案:

答案 0 :(得分:1)

检查以下代码

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If ActiveSheet.Name = "ChangeRecord" Then Exit Sub

Application.EnableEvents = False

UserName = Environ("USERNAME")

NewVal = Target.Value

Application.Undo

oldVal = Target.Value

lr = Sheets("ChangeRecord").Range("A" & Rows.Count).End(xlUp).Row + 1

Sheets("ChangeRecord").Range("A" & lr) = Now
Sheets("ChangeRecord").Range("B" & lr) = ActiveSheet.Name
Sheets("ChangeRecord").Range("C" & lr) = Target.Address
Sheets("ChangeRecord").Range("D" & lr) = oldVal
Sheets("ChangeRecord").Range("E" & lr) = NewVal
Sheets("ChangeRecord").Range("F" & lr) = UserName
Sheets("ChangeRecord").Hyperlinks.Add anchor:=Sheets("ChangeRecord").Range("G" & lr), _
            Address:="", SubAddress:=Target.Worksheet.Name & "!" & Target.Address, _
            TextToDisplay:="Changes"
Target = NewVal

Application.EnableEvents = True
End Sub

答案 1 :(得分:0)

我认为这将满足您的需求。请检查。

Sheets("ChangeRecord").Hyperlinks.Add Anchor:=Sheets("ChangeRecord").Range("G" & CStr(lr)), Address:="", SubAddress:=ActiveSheet.Name & "!" & Sheets("ChangeRecord").Cells(Target.Row, Target.Column).Address, TextToDisplay:=ActiveSheet.Name