我正在尝试从第一张工作表中的一行中复制值,当该行中的值位于" Called"改为" Y"到另一张纸上的另一张桌子。每个表之间的列不匹配,因此我需要定义哪个列在另一个表中的位置。我想在第二个表中创建一个新行以粘贴并仅复制其值已更改为" Y"的行中的值。这是我最简单的代码,用于传达我正在做的事情,但我尝试了很多东西,包括一个数组。 "表1"在第一张纸上,#34; Master",Table2在第二张纸上," Called List"。
编辑:这是我的新代码,它成功地将值复制到" Table2"上的正确列,但是它复制了所有行而不仅仅是当前行" Called&#34 ;改为" Y"。有没有办法复制到"表2"只有"表1和#34;中的行在哪里" Y"正在插入? Private Sub Worksheet_Change(ByVal Target As Range)
'Application.ScreenUpdating = False
Application.EnableEvents = False 'to prevent endless loop
Dim KeyCells As Range
Dim NewRow As ListRow
Set KeyCells = Range("Table1[Called]")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
If Target.Value = "Y" Then
Set NewRow = Worksheets("Called List").ListObjects("Table2").ListRows.Add '(AlwaysInsert:=True)
Worksheets("Called List").Range("Table2[Lessor]").Value = Worksheets("Master").Range("Table1[Lessor]").Value
Worksheets("Called List").Range("Table2[Phone]").Value = Worksheets("Master").Range("Table1[Phone Number]").Value
Worksheets("Called List").Range("Table2[Address]").Value = Worksheets("Master").Range("Table1[Address]").Value
Worksheets("Called List").Range("Table2[Sec]").Value = Worksheets("Master").Range("Table1[Sec]").Value
Worksheets("Called List").Range("Table2[Twn]").Value = Worksheets("Master").Range("Table1[Twn]").Value
Worksheets("Called List").Range("Table2[Rng]").Value = Worksheets("Master").Range("Table1[Rng]").Value
Worksheets("Called List").Range("Table2[County]").Value = Worksheets("Master").Range("Table1[County]").Value
Worksheets("Called List").Range("Table2[Legal Desc]").Value = Worksheets("Master").Range("Table1[Tract]").Value
Worksheets("Called List").Range("Table2[Gross Acres]").Value = Worksheets("Master").Range("Table1[Gross Acres]").Value
Worksheets("Called List").Range("Table2[Assumed NMA]").Value = Worksheets("Master").Range("Table1[Assumed NMA]").Value
Worksheets("Called List").Range("Table2[Comments]").Value = Worksheets("Master").Range("Table1[Notes]").Value
End If
End If
Application.EnableEvents = True
'Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
" Is there a way to copy to a new row on "Table2" only the row in "Table1" where "Y" is being inserted?"
1) "Target.Row" is your reference row on the master sheet, use it in your "Worksheets("Master").Range("Table1[Notes]").Value" statment
2) "NewRow" is your reference row on "called list" sheet, use it in your "Worksheets("Called List")**.Cells(Range("Table2[Lessor]").Value" statement
3) If you convert your Table?[...] to named ranges you can use something like:
Range("T2_Lessor").Cells(NewRow).Value = Range("T1_Lessor").Cells(KeyCells.Row).Value
Ex. of named range:
"Table1[Lessor]" becomes 'T1_Lessor' =OFFSET('Master'!$B$2,0,0,COUNTA('Master'!$B:$B),1)
"Table2[Lessor]" becomes 'T2_Lessor' =OFFSET('Called List'!$B$2,0,0,COUNTA('Called List'!$B:$B),1)