将整行复制到另一个工作表

时间:2017-10-11 13:24:19

标签: excel vba

我有一个代码在另一个工作表中查找新值并将新值复制到原始工作表的1行,但是,现在,我需要更改代码以复制新找到的值( 1个单元格),但整行,我尝试更改代码,但我无法让它工作,这里是只复制一个单元格的代码:

 Dim Lastrow       As Long  'the last row in Sheet2 col E
   Dim iRow1         As Long  'the row number on Sheet1
   Dim Sh1           As Worksheet
   Dim Sh2           As Worksheet

   Set Sh2 = ThisWorkbook.Worksheets("originalwkb")
 Set Sh1 = Workbooks("383839.xlsb").Sheets(3)

ThisWorkbook.Activate
   Lastrow = Sh2.Range("e65536").End(xlUp).Row

   iRow1 = 2

  ' Loop through values in column E of Sheet1


   Do
      Set FindCell = Sh2.Range("E2", Sh2.Cells(Lastrow, "E")).Find(What:=Sh1.Cells(iRow1, "E"), _
             After:=Sh2.Range("E2"), LookIn:=xlValues, LookAt:=xlWhole)
      If FindCell Is Nothing Then
         'add to bottom of list
         Lastrow = Lastrow + 1
         Sh2.Cells(Lastrow, "E") = Sh1.Cells(iRow1, "E")
      End If
      iRow1 = iRow1 + 1
   Loop Until IsEmpty(Sh1.Cells(iRow1, "E"))

1 个答案:

答案 0 :(得分:0)

您需要更改以下行。

Sh2.Cells(Lastrow, "E") = Sh1.Cells(iRow1, "E")

Sh2.Rows(Lastrow).Value = Sh1.Rows(iRow1).Value

编辑:由于我没有阅读答案,因此没有看到@Scott Craner的回复。猜猜我们可以为这个答案给他一些学分:)