Hello,
I have two folders each containing equal number files (n=20) with similar file names. I want to copy the first row of the files in Folder1 to the files of Folder 2. I have written the below VBA code, but it's not working:
Sub CopyData()
Dim wk1, wk2 As Workbook
Dim MyDir1, MyDir2 As Variant
Dim MyFile1, MyFile2 As String
Dim i As Integer
Application.ScreenUpdating = True
Application.DisplayAlerts = False
MyDir1 = "D:\Path to Folder1\"
MyDir2 = "D:\Path to Folder2\"
MyFile1 = Dir(MyDir1 & "*.xlsx")
MyFile2 = Dir(MyDir2 & "*.xlsx")
'Open workbooks
Set wk1 = Workbooks.Open(MyDir1)
Set wk2 = Workbooks.Open(MyDir2)
For i = 1 To 20
If MyFile1.FileName = MyFile2.FileName Then:
wk1.Sheets(1).Rows("1").Copy Destination:=wk2.Sheets(1).Rows("1")
wk1.Close
wk2.Save
wk2.Close True
i = i + 1
Next
'Turn on screen updating
Application.ScreenUpdating = True
End Sub
Could someone please help fixing this code.
Thank you,
Shawn
I have two folders each containing equal number files (n=20) with similar file names. I want to copy the first row of the files in Folder1 to the files of Folder 2. I have written the below VBA code, but it's not working:
Sub CopyData()
Dim wk1, wk2 As Workbook
Dim MyDir1, MyDir2 As Variant
Dim MyFile1, MyFile2 As String
Dim i As Integer
Application.ScreenUpdating = True
Application.DisplayAlerts = False
MyDir1 = "D:\Path to Folder1\"
MyDir2 = "D:\Path to Folder2\"
MyFile1 = Dir(MyDir1 & "*.xlsx")
MyFile2 = Dir(MyDir2 & "*.xlsx")
'Open workbooks
Set wk1 = Workbooks.Open(MyDir1)
Set wk2 = Workbooks.Open(MyDir2)
For i = 1 To 20
If MyFile1.FileName = MyFile2.FileName Then:
wk1.Sheets(1).Rows("1").Copy Destination:=wk2.Sheets(1).Rows("1")
wk1.Close
wk2.Save
wk2.Close True
i = i + 1
Next
'Turn on screen updating
Application.ScreenUpdating = True
End Sub
Could someone please help fixing this code.
Thank you,
Shawn