I am working on automating an excel model by copying data from other sheets into a masterfile. I have a bit of an issue that after adding the code the file went from 25mb to 60mb, without changing the content, only adding the code. Below you can find a snippet of how I automated the imports
Sub copytest() 'Procedure for retrieving data from the sourcefiles
Dim wbTarget, wbSource As Workbook
Dim target As Object
Dim pathSource, fileName As String
Dim xlApp As Application
Dim lastRow As Long
Application.EnableEvents = False
Application.ScreenUpdating = False
'path where the data source folders are located (please keep all of them in the same directory)
pathSource = "C:\Users\vferraz\Desktop\crm stock\RAPOARTE IMPORTANTE\18.02\Rapoarte pentru Handsets\"
Set wbTarget = ThisWorkbook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Application.CutCopyMode = False
'Stock 0001
Set wbSource = xlApp.Workbooks.Open(pathSource & "Stoc 0001.xls")
wbSource.Sheets(1).UsedRange.Copy
wbSource.Close
Set target = wbTarget.Sheets("Stock 0001")
target.UsedRange.Clear
Range("A1").Select
target.Paste
xlApp.Quit
Set wbSource = Nothing
Set xlApp = Nothing
ThisWorkbook.Sheets("Mastersheet").Activate
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
In the snippet above I only added the parsing of one file (Stock 0001), but the same method is done for other 10-15 files.
Does anyone have any ideas to improve the efficiency/size of this file based on this procedure?
P.S. Im aware that the "Paste" method might be adding formats rather than values only, then I tried adding .PasteSpecial xlPasteValues
instead of paste but it eventually throw errors that I couldn't identify
Update:
Based on this solution, this is the new version I tried:
Stock 0001
Set wbSource = xlApp.Workbooks.Open(pathSource & "Stoc 0001.xls")
lastRow = wbSource.Sheets(1).Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
wbTarget.Sheets("Stock 0001").Cells.Clear
wbSource.Sheets(1).Range("A1:C" & lastRow).Copy Destination:=wbTarget.Sheets("Stock 0001").Range("A1")
wbSource.Clo
The line wbSource.Sheets(1).Range("A1:C" & lastRow).Copy Destination:=wbTarget.Sheets("Stock 0001").Range("A1"
Throws the "copy method of range class failed error.