我在 Excel 2010 中有一个电子表格,当在同一行中的特定单元格中输入某个值时,我想连续获取某些单元格并将它们复制到另一个工作表中。
例如:在工作表 A 中,在单元格 A2 中,如果输入“X”,我希望将单元格 A5、A7 和 A13-17 复制到工作表 B。在工作表 B 中,应将它们复制到下一个可用行,但我需要将它们复制到特定的单元格(与工作表 A 中的不同 - 即:单元格 A5 需要复制到工作表 B 中的单元格 2;A7 到单元格 4 和 A13-17 到 10-14)。并且它们将在工作表 B 中复制到的单元格也需要在新行中创建。
我有一些做类似事情的 VBA;但是,它会复制整行。对于上述情况,我只需要特定的单元格,它们不会匹配工作表 B 中的相同单元格。
这是我能够开始工作的内容(复制整行):
Sub Testing_Issue(ByVal Target As Range)
'Disable events so code doesn't re-fire when row is deleted
Application.EnableEvents = False
'Was the change made to Column T?
If Target.Column = 20 Then
'If yes, does the Target read "Y"?
If Target = "Y" Then
'If Y, determine next empty row in Sheet "TEST"
nxtRw = Sheets("TEST").Range("A" & Rows.Count).End(xlUp).Row + 1
'Copy the row from the Open worksheet and move to the TEST worksheet
Target.EntireRow.Copy Destination:=Sheets("TEST").Range("A" & nxtRw)
Else
MsgBox ("That is not a valid entry")
End If
End If
'Re-enable Events
Application.EnableEvents = True
End Sub
我尝试使用以下内容而不是整个行部分,但不起作用。
Target.Range("A13").Copy Destination:=Sheets("TEST").Range("A5 & nxtRw")
Target.Range("B13").Copy Destination:=Sheets("TEST").Range("C5 & nxtRw")
Target.Range("I13:J13").Copy Destination:=Sheets("TEST").Range("E5 & nxtRw")
我将不胜感激一些建议,因为我真的需要让它正常工作。如果有不清楚的地方,请告诉我。