Reputation: 1
I have the following code:
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
My data that I am copying always goes from column A to BU, but the number of rows varies. For some reason, it will only copy and paste in two rows worth of data every time.
Why won't it copy the whole data set and paste?
Upvotes: 0
Views: 1841
Reputation: 123
What happens if you replace
x.Sheets("Details").Range("A2:BU" & Cells(Rows.Count, "BU").End(xlUp).Row).Copy
with the following:
With x.Sheets("Details")
.Range("A2:BU" & .Cells(.Rows.Count, "BU").End(xlUp).Row).Copy
end With
Which is basically what Tehscript mentions above.
Upvotes: 0
Reputation: 29244
VBA Uses ActiveSheet
in front of Range()
and Cells()
objects if they are not specified.
Secondary, there is no need to use the clipboard. If you want to copy values, use direct assignment. Expand a range into multiple cells with the .Resize()
function.
Try the following:
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
Upvotes: 2