更新单元格,自动将行复制到单独的工作表

我有一个由两列(A和B)组成的工作表…第一个只是一个名字,第二个是一个数字。

如果我编辑B列中的数字,我希望Excel自动将整行logging复制到第二个工作表中,以创build我所做的编辑列表。

然后,第二个工作表将是我对第一个工作表所做的不断更新的列表,并将最新的更改(两个更新列的副本)添加到下一个未使用的行中。

我希望有一点VBA的诡计可能能够做到这一点,但需要一些帮助,使之发生。

在有数据的表格中(在Excel对象下)试试这个,例如Sheet1

Option Explicit Dim PrevVal As Variant Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column = 2 Then Application.ScreenUpdating = False Dim rng As Range Dim copyVal As String Set rng = Nothing Set rng = Range("A" & Target.Row & ":B" & Target.Row) 'copy the values With Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp) .Offset(1, 0).Resize(1, rng.Cells.Count).Value = rng.Value With Worksheets("Sheet1") Range("A" & Target.Row).Copy copyVal = CStr(PrevVal) End With .Offset(1, 0).PasteSpecial xlPasteFormats .Offset(1, 1) = copyVal Application.CutCopyMode = False End With End If Application.ScreenUpdating = True End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Rows.Count > 1 Then Exit Sub If Target.Columns.Count > 1 Then Exit Sub PrevVal = Target.Value End Sub 

根据上面的回答和评论,以下是一些额外的代码来检查行号。

 Dim row_num As Long row_num = Cells(Rows.Count, "B").End(xlUp).Row If row_num > 1 then row_num = row_num + 1 'Add 1 only when row number doesn't equal to 1, otherwise - 1.