如何在范围函数上使用LastRow?

所以我想从工作表1-工作簿A到工作表1-工作簿B复制一定范围的单元格中的值。

我想复制源工作表中的所有内容:更具体地说,每个单元格上都有一个值。

在目标工作表上,为源工作表上的值指定了单元格。

这是我的代码到目前为止(这是糟糕的,但我是VBA的菜鸟!):

Sub CopyRangeofCells() Dim x As Workbook Dim y As Workbook Set x = Workbooks.Open("C:\test\template.xlsx") Set y = Workbooks.Open("C:\test\finalfile.xlsx") x.Sheets("RDBMergeSheet").Range("A1").Copy y.Sheets("CW Fast").Range("A1").PasteSpecial 'Close x: x.Close End Sub 

在我的范围内,我想要做一些类似Range(“A1:LastRow”)或类似的事情。 我该怎么做? 我可以创build一个拉斯特罗variables,然后做(“A1:mylastrowvariable”)?

希望你能帮助! VBA对我来说太混乱了,整天给我Java! :P

让我们一步一步来做:

 Sub CopyRangeofCells() Dim x As Workbook Dim y As Workbook Dim LastRow As Long Set x = Workbooks.Open("C:\test\template.xlsx") Set y = Workbooks.Open("C:\test\finalfile.xlsx") With x.Sheets("RDBMergeSheet") LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' get last row with data in column "A" .Range("A1:A" & LastRow).Copy End With y.Sheets("CW Fast").Range("A1").PasteSpecial xlPasteValues 'Close x: x.Close End Sub 

像这样的东西:

 Sub CopyRangeofCells() Dim x As Workbook Dim y As Workbook Dim LastRow as Long Dim LastRowToCopy as Long Set x = Workbooks.Open("C:\test\template.xlsx") Set y = Workbooks.Open("C:\test\finalfile.xlsx") LastRowToCopy = x.Sheets("RDBMergeSheet").Cells(x.Sheets("RDBMergeSheet").Rows.Count, "A").End(xlUp).Row x.Sheets("RDBMergeSheet").Range("A1:A" & LastRowToCopy).Copy 'copy from A1 to lastrow LastRow = y.Sheets("CW Fast").Cells(y.Sheets("CW Fast").Rows.Count, "A").End(xlUp).Row + 1 'find the last row y.Sheets("CW Fast").Range("A" & LastRow).PasteSpecial xlPasteValues 'paste on the lastrow of destination + 1 (so next empty row) x.Close End Sub