[英]Searching an Excel workbook and listing results in another sheet or report using VBA
[英]Linking an Excel Sheet to another in the same workbook using 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
不確定是否最簡單的方法是保留工作表副本:如果用戶在現有數據或圖表等中插入新的行或列(數千)或單元格,該怎么辦?
我建議保留與此類似的更改日志
然后Sheet1將包含以下VBA代碼:
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
在Sheet2上,您將擁有4列:
Date-Time User Nane Cell New Value
Wed Jun 03, 2015 01:07:43 User1 A1 Test 1
Wed Jun 03, 2015 01:07:48 User2 B3 Test 3
Wed Jun 03, 2015 01:07:52 User5 D2 Test 7
為什么首先需要具有VBA代碼? 您可以簡單地使用Excel中已內置的跟蹤更改的功能(查看-更改-跟蹤更改)。 用戶進行更改后,保存並關閉工作簿; 您可以轉到“跟蹤更改”功能內的“突出顯示更改”行,然后選擇“在新工作表上列出更改”。 這將創建一個新工作表,並顯示所有必要的信息(動作編號,日期,時間,人員,更改,工作表,范圍,新值,舊值等)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.