I need to copy data from files located within specific folder to current open workbook. The data range for each files is from A2 to cell located at most right column and a row above of last row. There will be 4 files in the folder. My objective is to copy
1. data from files 1 and paste to active workbook A3
2. data from files 2 and paste to active workbook D3
3. data from files 3 and paste to active workbook F3
4. data from files 4 and paste to active workbook J3
I have tried below code but nothing happen.
1. data from files 1 and paste to active workbook A3
2. data from files 2 and paste to active workbook D3
3. data from files 3 and paste to active workbook F3
4. data from files 4 and paste to active workbook J3
I have tried below code but nothing happen.
VBA Code:
[/
Dim directory As String, fileName As String
Application.ScreenUpdating = False
directory = "C:\Users\users\Downloads\My\"
fileName = Dir(directory & "*.xlsx")
Set wb1 = ThisWorkbook
Do While fileName <> ""
Set wb2 = Workbooks.Open(directory & fileName)
Lastrow = Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1
Lastcol = Worksheets(1).Cells(1, Columns.Count).End(xlToLeft).Column - 1
With wb2.Sheets(1)
wb1.Sheets(1).Range("a3") = .Range(.Cells(2, 1), .Cells(Lastrow, Lastcol))
wb1.Sheets(1).Range("d3") = .Range(.Cells(2, 1), .Cells(Lastrow, Lastcol))
wb1.Sheets(1).Range("f3") = .Range(.Cells(2, 1), .Cells(Lastrow, Lastcol))
wb1.Sheets(1).Range("j3") = .Range(.Cells(2, 1), .Cells(Lastrow, Lastcol))
End With
wb2.Close savechanges:=True
fileName = Dir
Loop
Application.ScreenUpdating = True
End Sub
]