Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 创建工作表中所有更改的日志,包括将值复制和粘贴到某个范围_Excel_Vba - Fatal编程技术网

Excel 创建工作表中所有更改的日志,包括将值复制和粘贴到某个范围

Excel 创建工作表中所有更改的日志,包括将值复制和粘贴到某个范围,excel,vba,Excel,Vba,所附代码适用于对sheet1进行单条目更改,并保存到sheet9上的日志表中。但我还需要找到一种方法来记录所有的更改,如果有一个复制和粘贴的值到一个范围。因此,复制和粘贴过程中更改的每个单元格都会被记录下来 Dim vOldVal Private Sub Worksheet_Change(ByVal Target As Range) Dim bBold As Boolean Dim col Dim row If Target.Cells.Count > 1 Then Exit Sub

所附代码适用于对sheet1进行单条目更改,并保存到sheet9上的日志表中。但我还需要找到一种方法来记录所有的更改,如果有一个复制和粘贴的值到一个范围。因此,复制和粘贴过程中更改的每个单元格都会被记录下来

Dim vOldVal
Private Sub Worksheet_Change(ByVal Target As Range)

Dim bBold As Boolean
Dim col
Dim row

If Target.Cells.Count > 1 Then Exit Sub
On Error Resume Next
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
If IsEmpty(vOldVal) Then vOldVal = "Empty Cell"
bBold = Target.HasFormula
With Sheet9

With .Cells(.Rows.Count, 1).End(xlUp)(2, 1)
.Value = vOldVal

row = Target.row
col = Target.Column
.Offset(0, 3) = Sheet1.Cells(row, 1).Value 'id
.Offset(0, 4) = Sheet1.Cells(row, 2).Value 'name
.Offset(0, 5) = Sheet1.Cells(1, col).Value 'column name
With .Offset(0, 1)
If bBold = True Then
.ClearComments
.AddComment.Text Text:= _
"Dev:" & Chr(10) & "" & Chr(10) & _
"Bold values are the results of formulas"
End If
.Value = Target
.Font.Bold = bBold
End With
.Offset(0, 2) = Date & " " & Time

End With
.Cells.Columns.AutoFit
End With
vOldVal = vbNullString
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
On Error GoTo 0
Sheet10.PivotTables("Log_Pivot").PivotCache.Refresh
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

vOldVal = Target
End Sub

请您仔细阅读缩进代码,因为它将使您的代码更具可读性,更容易发现错误。也许您可以将
vOldVal
转换为一个范围或数组,并在您的更改事件中循环。感谢您的快速响应,是否可以查看将此日期保存到数组的工作方式,因为我以前没有使用过数组,所以实际上比我最初认为的要复杂一些,就像粘贴多个单元格一样,目标只会拾取第一个单元格。