Hi all,
I have set up a macro to import data from two sets of columns in several workbooks into one master workbook.
his is my first time creating a macro, and as you can see from below it is not very efficient. I am looking for any tips on how I can improve it, as I need to import data from 25 workbooks into one.
To summarise the code does the following:
1. Copies the data in the column that contains the ID numbers ("Workbook A")
2. Paste them in the masterfile
3. Copies the data in the column that contains the date ("Workbook A")
4. Paste them in the masterfile
5. Repeats the process in the next workbook, and paste it in the masterfile underneath the data from step 2/4.
The workbooks are formatted differently, so it is not always the same column that is being copied over.
As you can see, my below code is just mimicking how the process would be carried out manually. Any tips on how to improve it would be appreciated.
Sub Import()
'
' Import Macro
'
'
Application.ScreenUpdating = False
Workbooks.Open ("C:\Users\tom_000\Documents\Excel test file\Testfile1.xlsx")
Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("MasterFile.xlsx").Activate
Range("D2").Select
ActiveSheet.Paste
Windows("Testfile1.xlsx").Activate
Range("H2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Windows("MasterFile.xlsx").Activate
Range("F2").Select
ActiveSheet.Paste
Windows("Testfile1.xlsx").Activate
ActiveWindow.Close
Workbooks.Open ("C:\Users\tom_000\Documents\Excel test file\Testfile2.xlsx")
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("MasterFile.xlsx").Activate
lastRow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row
ActiveSheet.Range("D" & lastRow + 1).Select
ActiveSheet.Paste
Windows("Testfile2.xlsx").Activate
Range("F4").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Windows("MasterFile.xlsx").Activate
lastRow = ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Row
ActiveSheet.Range("F" & lastRow + 1).Select
ActiveSheet.Paste
Windows("Testfile2.xlsx").Activate
ActiveWindow.Close
Application.ScreenUpdating = True
End Sub
Additionally: I would also like the rows in column A in the Masterfile to be populated based on the name of the sourcefile, how can I make a code to do this? Bearing in mind that this needs to correspond with the amount of rows that I have imported from that particular sourcefile.
Any help and tips would be greatly appreciated!
Bookmarks