这就是我想要做的事情:
这是我为我想要做的事情而编写的代码(不能正常工作)和一些截图:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 6 Then
If Len(Target.Value) > 0 Then
Dim r As Range
Dim a As String
Dim b As String
b = "sheet3!A" & Sheet1.Cells(1, 9) + 3 & ":C" & Sheet1.Cells(1, 9) + 3
a = "E" & Target.Row & ":G" & Target.Row
Dim r1, r2 As Range
r1 = Sheet1.Range(a)
Sheet2.Range(b).Value = r1
Sheet1.Range(a).EntireRow.Delete
End If
End If
End Sub
答案 0 :(得分:1)
将以下代码放入"待定"工作表模块,在Worksheet_Change
事件下。
首先,您应该学会享受Target
变量带来的好处。
例如,如果你想复制一个值" F"在同一行的#G;#34; (您的Target
),您可以使用Target.Offset(, -1).Copy
。
如果您要复制Target
后的整个行,只需使用Target.EntireRow.Delete
。
另一件事,根据您的屏幕截图(以及您帖子的第一部分)" Letter No"位于Column" B" in" Records"片。因此,Sort
根据列" B"。
<强>代码强>
Option Explicit
Private Sub Worksheet_Change(ByVal Target As range)
Dim RecSht As Worksheet
Dim NextRow As Long
Application.ScreenUpdating = False
Application.EnableEvents = False
If Target.Column = 7 Then ' Column "G"
If Target.Value <> "" Then
Set RecSht = Worksheets("Records")
NextRow = RecSht.Cells(RecSht.Rows.Count, "A").End(xlUp).Row + 1 ' <-- get next empty row at Column A in "Records" sheet
RecSht.Range("B" & NextRow).Value = Target.Value ' column "G"
RecSht.Range("A" & NextRow).Value = Target.Offset(, -1).Value ' column "F"
RecSht.Range("C" & NextRow).Value = Target.Offset(, -2).Value ' column "E"
Target.EntireRow.Delete ' <-- Delete entire row that was copied
'--- Sort Section ---
RecSht.Range("A2:C" & NextRow).Sort key1:=RecSht.Range("B2:B" & NextRow), _
order1:=xlAscending, Header:=xlYes
End If
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub