我有以下代码:
Sub PrepWork()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("O:\SFS_Data_Repository\CR&G\PCRBA\Rcn_24000646\FIS &
Profile Filtered Reports\Raw Data FIS_04112018_24000646.xlsx")
Set y =
Workbooks.Open("O:\SFS_Data_Repository\CR&G\PCRBA\Rcn_24000646\Matching on
Loaner Computer 6\FIS_AND-VAN-Trxn_lst6_DDA_last4_cardnum_20180411-Filtered-
LCPTR.xlsx")
x.Sheets("Details").Range("A2:BU" & Cells(Rows.Count,
"BU").End(xlUp).Row).Copy
y.Sheets("FISV").Range("A4").PasteSpecial
'Close x:
x.Close
End Sub
我正在复制的数据总是从A列到BU,但行数会有所不同。出于某种原因,它每次只会复制和粘贴两行数据。
为什么不复制整个数据集并粘贴?
答案 0 :(得分:2)
VBA在ActiveSheet
和Range()
对象前面使用Cells()
(如果未指定)。
辅助,无需使用剪贴板。如果要复制值,请使用直接分配。使用.Resize()
函数将范围扩展为多个单元格。
尝试以下方法:
Sub PrepWork()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("O:\SFS_Data_Repository\CR&G\PCRBA\Rcn_24000646\FIS Profile Filtered Reports\Raw Data FIS_04112018_24000646.xlsx")
Set y = Workbooks.Open("O:\SFS_Data_Repository\CR&G\PCRBA\Rcn_24000646\Matching on Loaner Computer 6\FIS_AND-VAN-Trxn_lst6_DDA_last4_cardnum_20180411-Filtered-LCPTR.xlsx ")
Dim n As Long
Dim r As Range
Set r = x.Sheets("Details").Range("BU2")
' Start form BU2 and count down the rows
n = x.Range(r, r.End(xlDown)).Rows.Count
' Take n rows and 73 columns from 'A2' in x and copy the
' values into n rows and 72 columns under "A4" in y
y.Sheets("FISV").Range("A4").Resize(n, 73).Value = _
x.Sheets("Details").Range("A2").Resize(n, 72).Value
'Close x:
x.Close
End Sub
答案 1 :(得分:0)
如果替换
会发生什么x.Sheets("Details").Range("A2:BU" & Cells(Rows.Count, "BU").End(xlUp).Row).Copy
以下内容:
With x.Sheets("Details")
.Range("A2:BU" & .Cells(.Rows.Count, "BU").End(xlUp).Row).Copy
end With
这基本上是Tehscript在上面提到的内容。