Reputation: 547
I want to copy data from one workbook to another.
The following line works:
Workbooks(TempBook.Name).Worksheets(sheet_Index).Range("A1:A20").Copy _
Workbooks(mainWB.Name).Worksheets(sheet_Index).Range("A1")
This throws
Application-defined or object-defined
Workbooks(TempBook.Name).Worksheets(sheet_Index).Range("A1", Range("A1").End(xlDown)).Copy _
Workbooks(mainWB.Name).Worksheets(sheet_Index).Range("A1")
Why can I not select a dynamic range?
Upvotes: 0
Views: 93
Reputation: 49998
Range("A1").End(xlDown)
needs to be qualified with the worksheet and workbook, otherwise it's implicitly on the ActiveSheet
.Workbooks(TempBook.Name)
should just be TempBook
. Similarly for Workbooks(mainWB.Name)
.xlUp
to find the last row instead of xlDown
.With TempBook.Worksheets(sheet_Index)
.Range("A1", .Range("A1").End(xlDown)).Copy _
Destination:=mainWb.Worksheets(sheet_Index).Range("A1")
End With
Upvotes: 5