I've put the following code together to copy specific files from one folder to another based on a list contained in a spreradsheet. There are 2 problems which I'd appreaciatte some help with
1 the kill instuction doesn't work
and
2 the code is copying all of the files, not the ones identified in the list in the spreadsheet.
Thanks
Geoff
Sub copy_specific_files_in_folder()
Dim fso As Object
Dim sourcePath As String
Dim destinationPath As String
Dim xSPathStr As Variant, xDPathStr As Variant
Dim xRg As Range, xCell As Range
Dim fileExtn As String
Dim xVal As String
Dim ws As Worksheet
sourcePath = ThisWorkbook.Sheets("draft Intro").Range("B10")
destinationPath = ThisWorkbook.Sheets("draft Intro").Range("B22")
fileExtn = ".txt"
If Right(sourcePath, 1) <> "" Then
sourcePath = sourcePath & ""
End If
Set fso = CreateObject("scripting.filesystemobject")
If fso.folderexists(sourcePath) = False Then
MsgBox sourcePath & " does not exist"
Exit Sub
End If
If fso.folderexists(destinationPath) = False Then
MsgBox sourcePath & " does not exist"
Exit Sub
End If
'delete existing files
On Error Resume Next
Kill destinationPath & ".txt"
On Error GoTo 0
On Error Resume Next
Set xRg = Application.Worksheets("draft Intro").Range("H53:H4318")
For Each xCell In xRg
xVal = xCell.Value
If TypeName(xVal) = "String" And xVal <> "" Then
FileCopy sourcePath & xVal & fileExtn, destinationPath & xVal & fileExtn
End If
Next
'FSO.copyfile Source:=sourcePath & fileExtn, destination:=destinationPath
'MsgBox "files have been copied from " & sourcePath & vbCr & "to" & destinationPath
End Sub
1 the kill instuction doesn't work
and
2 the code is copying all of the files, not the ones identified in the list in the spreadsheet.
Thanks
Geoff
Sub copy_specific_files_in_folder()
Dim fso As Object
Dim sourcePath As String
Dim destinationPath As String
Dim xSPathStr As Variant, xDPathStr As Variant
Dim xRg As Range, xCell As Range
Dim fileExtn As String
Dim xVal As String
Dim ws As Worksheet
sourcePath = ThisWorkbook.Sheets("draft Intro").Range("B10")
destinationPath = ThisWorkbook.Sheets("draft Intro").Range("B22")
fileExtn = ".txt"
If Right(sourcePath, 1) <> "" Then
sourcePath = sourcePath & ""
End If
Set fso = CreateObject("scripting.filesystemobject")
If fso.folderexists(sourcePath) = False Then
MsgBox sourcePath & " does not exist"
Exit Sub
End If
If fso.folderexists(destinationPath) = False Then
MsgBox sourcePath & " does not exist"
Exit Sub
End If
'delete existing files
On Error Resume Next
Kill destinationPath & ".txt"
On Error GoTo 0
On Error Resume Next
Set xRg = Application.Worksheets("draft Intro").Range("H53:H4318")
For Each xCell In xRg
xVal = xCell.Value
If TypeName(xVal) = "String" And xVal <> "" Then
FileCopy sourcePath & xVal & fileExtn, destinationPath & xVal & fileExtn
End If
Next
'FSO.copyfile Source:=sourcePath & fileExtn, destination:=destinationPath
'MsgBox "files have been copied from " & sourcePath & vbCr & "to" & destinationPath
End Sub