countryfan_nt
Well-known Member
- Joined
- May 19, 2004
- Messages
- 765
Greetings, Hope all is well.
Please help me. I have a sheet called "Headers" I want to please copy all the range of A1:R1, and paste it on a all the headers of all the workbooks in the folder:
C:\Users\nathan.pure\Desktop\Labs 365
1. The code isn't working, here is the error: "Runtime error '1004': PasteSpecial method of Range class failed".
2. one more thing, please instead of physically choosing the folder, can you please help tweak the code to go straight to the path? C:\Users\nathan.pure\Desktop\Labs 365
Truly appreciate it in advance.
Please help me. I have a sheet called "Headers" I want to please copy all the range of A1:R1, and paste it on a all the headers of all the workbooks in the folder:
C:\Users\nathan.pure\Desktop\Labs 365
1. The code isn't working, here is the error: "Runtime error '1004': PasteSpecial method of Range class failed".
2. one more thing, please instead of physically choosing the folder, can you please help tweak the code to go straight to the path? C:\Users\nathan.pure\Desktop\Labs 365
Truly appreciate it in advance.
VBA Code:
Sub LoopThroughFiles()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
Sheets("HEADERS").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
' opens the desired folder
If xFd.Show = -1 Then
xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
xFileName = Dir(xFdItem & "*.csv*")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
'your code here
' Code not pasting
Range("A1").PasteSpecial Paste:=xlPasteValues
ActiveWorkbook.Save
ActiveWorkbook.Close
End With
xFileName = Dir
Loop
End If
End Sub