Howardc, as snb said, please have a look at the code and analyze what's the code trying to do. That way, you'll also be able to come up with some of your own code to counter any errors or make any modifications based on your requirement. In the above error you've mentioned for example, you already understood that the error is because the file already exists. So you could either decide to keep the original file, OR overwrite it.
One of the ways to manage this is to use an On Error statement that will ignore an error. So you could either use
to overwrite the file, ORCode:Sub MoveFilesToAnotherFolder() Dim objFSO As Object 'FileSystemObject Dim objFile As Object 'File Dim objFolder As Object 'Folder Const strFolder As String = "C:\pull" Const strNewFolder As String = "C:\summary profits" Set objFSO = CreateObject("Scripting.FileSystemObject") For Each objFolder In objFSO.GetFolder(strFolder & "\").SubFolders If Right(objFolder.Name, 2) = "tb" Then For Each objFile In objFolder.Files If InStr(1, objFile.Type, "Excel", vbTextCompare) Then On Error Resume Next Kill strNewFolder & "\" & objFile.Name Err.Clear:On Error GoTo 0 Name objFile.Path As strNewFolder & "\" & objFile.Name End If Next objFile End If Next objFolder End Sub
to keep the original fileCode:Sub MoveFilesToAnotherFolder() Dim objFSO As Object 'FileSystemObject Dim objFile As Object 'File Dim objFolder As Object 'Folder Const strFolder As String = "C:\pull" Const strNewFolder As String = "C:\summary profits" Set objFSO = CreateObject("Scripting.FileSystemObject") For Each objFolder In objFSO.GetFolder(strFolder & "\").SubFolders If Right(objFolder.Name, 2) = "tb" Then For Each objFile In objFolder.Files If InStr(1, objFile.Type, "Excel", vbTextCompare) Then On Error Resume Next Name objFile.Path As strNewFolder & "\" & objFile.Name Err.Clear:On Error GoTo 0 End If Next objFile End If Next objFolder End Sub




Reply With Quote
Bookmarks