使用VBA将Excel工作表链接到同一工作簿中的另一个工作表

时间:2015-06-02 21:40:48

标签: excel vba excel-vba

我正在尝试跟踪我的工作簿中已完成的工作,因此我希望获得一份工作表的副本,其中添加了注释以及更改日期和更改它的用户(不关心他们改变了什么,但我想知道他们用日期和用户名更改了哪些单元格),是的我知道你可以使用复制并将其粘贴为链接,但我现在拥有的VBA脚本无法识别原始版本中所做的更改表格代码如下:

    Private Sub Worksheet_Change(ByVal Target As Excel.range)
'If Target.Column <> 1 Then Exit Sub
Dim ccc As range
Dim comment As String
Application.ScreenUpdating = False
Application.Calculation = xlManual

comment = ("Cell Last Edited: ") & Now & (" by ") & Application.UserName
Target.ClearComments
    For Each ccc In Target
    range(ccc.Address).AddComment comment
    Next ccc

Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub

其中的作品非常好,现在要明确我想要将原始工作表复制到另一个工作表的脚本,如果有人进行更改,则复制或修改复制表并添加带有日期和用户名的注释

任何建议都会很棒并且事先感谢:D

2 个答案:

答案 0 :(得分:1)

不确定是否保留工作表的副本是最简单的方法:如果用户在现有数据或图表等中插入新行或列(数千个)或单元格,该怎么办

我建议记录与此相似的更改

  • 给出一张包含2张(Sheet1和Sheet2)的工作簿
  • Sheet1包含数据
  • Sheet2将包含一个包含4列的日志:日期,用户名,单元格,新值

然后Sheet1将包含此VBA代码:

re.findFirstIn(a).isDefined
res: Boolean: true

在Sheet2上,您将拥有4列:

Option Explicit

Private logLine As Long

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.CountLarge < 1000 Then    ' don't log deletion of all cells on Sheet1

        Dim editedCell As String

        ' get the clean cell address ("A1" instead of "$A$1")
        editedCell = Target.Address(RowAbsolute:=False, ColumnAbsolute:=False)

        With Sheet2     ' generate all log lines on Sheet2

            logLine = logLine + 1   ' move to the next row on Sheet2

            ' Column 1: Date and Time
            .Cells(logLine, 1).Value2 = Format(Now, "ddd mmm dd, yyyy hh:mm:ss")

            ' Column 2: User Name
            .Cells(logLine, 2).Value2 = Application.UserName

            ' Column 3: link to the edited cell, also showing the cell itself
            .Hyperlinks.Add _
                Anchor:=.Cells(logLine, 3), _
                Address:=vbNullString, _
                SubAddress:="Sheet1!" & editedCell, _
                TextToDisplay:=editedCell

            ' Column 4: the new value
            .Cells(logLine, 4).Value2 = Target.Value2
        End With
    End If
End Sub

答案 1 :(得分:0)

为什么首先需要VBA代码?您可以简单地使用已经内置的跟踪Excel中更改的功能(查看 - 更改 - 跟踪更改)。用户进行更改后,保存并关闭工作簿;你可以去看看&#34;突出变化&#34; &#34;跟踪变化&#34;功能并选择&#34;列出新工作表上的更改&#34;。这将创建一个新工作表,并将显示所有必要的信息(操作编号,日期,时间,人员,更改,工作表,范围,新值,旧值等)。