VBA to copy a row of Excel files from one folder to another

Bamh1

New Member
Joined
Oct 7, 2021
Messages
40
Office Version
  1. 2016
Platform
  1. Windows
  2. Mobile
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
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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.
Similar file names or the same file names between the folders? If the latter then Excel doesn't allow 2 workbooks with the same name to be open at the same time, so this macro temporarily renames the file in Folder2.

VBA Code:
Public Sub Copy_Row_Between_Workbooks_In_2_Folders()

    Dim folder1 As String, folder2 As String
    Dim fileName As String
    Dim wb1 As Workbook, wb2 As Workbook
   
    folder1 = "D:\Path to Folder1\"
    folder2 = "D:\Path to Folder2\"
   
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
   
    fileName = Dir(folder1 & "*.xlsx")
    While fileName <> vbNullString
        Name folder2 & fileName As folder2 & "COPY " & fileName
        Set wb1 = Workbooks.Open(folder1 & fileName)
        Set wb2 = Workbooks.Open(folder2 & "COPY " & fileName)
        wb1.Worksheets(1).Rows("1").Copy Destination:=wb2.Worksheets(1).Rows("1")
        wb1.Close False
        wb2.Close True
        Name folder2 & "COPY " & fileName As folder2 & fileName
        fileName = Dir
    Wend
   
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Solution
Similar file names or the same file names between the folders? If the latter then Excel doesn't allow 2 workbooks with the same name to be open at the same time, so this macro temporarily renames the file in Folder2.

VBA Code:
Public Sub Copy_Row_Between_Workbooks_In_2_Folders()

    Dim folder1 As String, folder2 As String
    Dim fileName As String
    Dim wb1 As Workbook, wb2 As Workbook
  
    folder1 = "D:\Path to Folder1\"
    folder2 = "D:\Path to Folder2\"
  
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
  
    fileName = Dir(folder1 & "*.xlsx")
    While fileName <> vbNullString
        Name folder2 & fileName As folder2 & "COPY " & fileName
        Set wb1 = Workbooks.Open(folder1 & fileName)
        Set wb2 = Workbooks.Open(folder2 & "COPY " & fileName)
        wb1.Worksheets(1).Rows("1").Copy Destination:=wb2.Worksheets(1).Rows("1")
        wb1.Close False
        wb2.Close True
        Name folder2 & "COPY " & fileName As folder2 & fileName
        fileName = Dir
    Wend
  
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
  
End Sub
Hi John,

Thank you for taking the time to review this request, and for the tip about Excel not opening the same names. I ran your code, but I get an error on this line: Name folder2 & fileName As folder2 & "COPY " & fileName
 
Upvote 0
You didn't answer my question.

What is the error? Do both folder paths end with a back slash character? If you have "File1.xlsx" in folder1, do you also have "File1.xlsx" in folder2?
 
Upvote 0
You didn't answer my question.

What is the error? Do both folder paths end with a back slash character? If you have "File1.xlsx" in folder1, do you also have "File1.xlsx" in folder2?
Yes, file names ARE exactly the same. The first row of File1.xlsx in folder 1 should be copied to File1.xlsx in folder 2 and so on. The error is: Run time error'53' File not found, and debugger highlights this line: Name folder2 & fileName As folder2 & "COPY " & fileName
 
Upvote 0
They don't have the backslash at the enn, but when I removed the backslashes, it still won't work
 
Upvote 0
They don't have the backslash at the enn,
They should have back slashes, as coded.

The error suggests File1.xlsx exists in folder1, but not in folder2. Add this line above the Name ... line and verify that the file displayed actually exists.

VBA Code:
        MsgBox "Rename " & folder2 & fileName
 
Upvote 0
That was true, I found and removed the additional file and the code started executing. Thank you for your help
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top