复制数据并粘贴到选定的行

我有一个从我的工作代码中复制和粘贴来自其他工作表的数据到一个masterworkbooks主表。 下面的代码允许我将BX列中的数据复制并粘贴到列A的第一个空行,然后将CC列复制到列B的第一个空行。 但是,我想将列CC粘贴到列B(10)行。 我怎样才能做到这一点?

lRow = copySheet.Cells(copySheet.Rows.Count, 1).End(xlUp).Row With copySheet.Range("BX2:BX" & lRow) pasteSheet.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) .Resize(.Rows.Count, .Columns.Count) = .Value End With 'Determine last row of Column B in copySheet lRow = copySheet.Cells(copySheet.Rows.Count, 1).End(xlUp).Row With copySheet.Range("CC2:CC" & lRow) pasteSheet.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) .Resize(.Rows.Count, .Columns.Count) = .Value End With 

你能告诉我怎样才能确定有多less行被选中复制?

编辑:现在我想添加一个if条件的另一列,应该说:

如果

工作表“数据”中的列U的单元格值为“8636”,则应将这些值粘贴到工作表“KomKo”(贴图)中的H列; 到下一行,因为我在上面的代码中使用了“with”部分。

否则(如果H列中的值不是8636)则应粘贴

将此列中的值添加到工作表“KomKo”(贴图)中的G列,并再次具有与上面相同的偏好

我怎样才能做到这一点 ?

更改pasteSheet.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0).Resize(.Rows.Count, .Columns.Count) = .Value 。该值pasteSheet.Range("B10").Resize(.Rows.Count, .Columns.Count) = .Value

*************解答编辑****************
*******增加了maxR – H列最后一行和G *******
你可以做这样的事情来得到你需要的东西:

 Sub check8636values() Dim copySheet, pasteSheet As Worksheet Dim lRowU, lRowH, lRowG, maxR, i As Long 'Dont forget to change to the correct sheet names!!!! Set copySheet = ThisWorkbook.Sheets("data") Set pasteSheet = ThisWorkbook.Sheets("KomKo") lRowU = copySheet.Cells(copySheet.Rows.Count, "U").End(xlUp).Row For i = 1 To lRowU lRowG = pasteSheet.Cells(pasteSheet.Rows.Count, "G").End(xlUp).Row + 1 lRowH = pasteSheet.Cells(pasteSheet.Rows.Count, "H").End(xlUp).Row + 1 maxR = Application.Max(lRowG,lRowH) If copySheet.Cells(i, "U").Value = "8636" Then pasteSheet.Cells(maxR, "H").Value = copySheet.Cells(i, "U").Value pasteSheet.Cells(maxR, "Y").Value = copySheet.Cells(i, "T").Value Else pasteSheet.Cells(maxR, "G").Value = copySheet.Cells(i, "U").Value pasteSheet.Cells(maxR, "X").Value = copySheet.Cells(i, "T").Value End If Next i End Sub 

因为只处理lRow范围,所以不需要With-End With块来缩写Resize方法参数:只使用lRow作为第一个

此外,由于您不显示copySheetpasteSheet是否来自同一个工作簿,因此在.Rows.Count之前引用它们并防止从其源工作簿中获得的问题.Rows.Count安全。

  'Determine last row of Column B in copySheet lRow = copySheet.Cells(copySheet.Rows.Count, 1).End(xlUp).Row pasteSheet.Cells(pasteSheet.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(lRow) = copySheet.Range("BX2:BX" & lRow).Value pasteSheet.Range("B10").Resize(lRow).Value = copySheet.Range("CC2:CC" & lRow).Value