希望将审核日志值作为超链接获取

时间:2016-03-16 17:32:23

标签: vba excel-vba excel

'This is my code and I want the values retrieved by this code as linkable .Range("F" & NR).Value = Hyperlink("#'!sh.Name'!" & CELL("Target.Address"), "Target.Address")

'Below is the full code
Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
Dim NR As Long
Dim CELL As Range, Hyperlink As String
If sh.Name = "AuditLog" Then Exit Sub     

Application.EnableEvents = False
    With Sheets("AuditLog")
        NR = .Range("B" & .Rows.Count).End(xlUp).Row + 1
        .Range("B" & NR).Value = Environ("UserName")
        .Range("C" & NR).Value = Environ("ComputerName")
        .Range("D" & NR).Value = Now
        .Range("E" & NR).Value = sh.Name
'This code will retrieve values from the changed cell, I want this as a hyperlink to the value changed in another worksheet
        .Range("F" & NR).Value = Hyperlink("#'!sh.Name'!" & CELL("Target.Address"), "Target.Address")
        .Range("G" & NR).Value = Prior Val
        .Range("H" & NR).Value = Target(1).Value
        NR = NR + 1
    End With
Application.EnableEvents = True
End Sub

1 个答案:

答案 0 :(得分:0)

' This code will help to traverse back to the cell where the changes was made to get captured under Audit Log
Option Explicit
Public PriorVal As String

Private Sub Workbook_Open()
Dim NR As Long
    With Sheets("AuditLog")
        NR = .Range("C" & .Rows.Count).End(xlUp).Row + 1
        Application.EnableEvents = False
        .Range("B" & NR).Value = Environ("UserName")
        .Range("C" & NR).Value = Environ("ComputerName")
        Application.EnableEvents = True
    End With
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal sh As Object, ByVal Target As Range)
    If Selection(1).Value = "" Then
        PriorVal = "Blank"
    Else
        PriorVal = Selection(1).Value
    End If
End Sub

Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
Dim NR As Long
Dim CELL As Range
If sh.Name = "AuditLog" Then Exit Sub     'allows you to edit the log sheet

Application.EnableEvents = False
    With Sheets("AuditLog")
        NR = .Range("B" & .Rows.Count).End(xlUp).Row + 1
        .Range("B" & NR).Value = Environ("UserName")
        .Range("C" & NR).Value = Environ("ComputerName")
        .Range("D" & NR).Value = Now
        .Range("E" & NR).Value = sh.Name
        .Hyperlinks.Add Anchor:=Sheets("AuditLog").Cells(NR, 6), Address:="", SubAddress:="'" & sh.Name & "'" & "!" & Target.Address, TextToDisplay:=sh.Name & "!" & Target.Address
        .Range("G" & NR).Value = PriorVal
        .Range("H" & NR).Value = Target(1).Value
        NR = NR + 1
    End With
Application.EnableEvents = True

End Sub