error file already exists when move file from folder to another

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
175
Office Version
  1. 2019
Platform
  1. Windows
Hi experts
it gives me error file already exists in this line
VBA Code:
  FSO.MoveFile Source:=SourcePath & NameFile, Destination:=DestPath
when try move file from folder to another
so should move file extraction 10 Oct. 2022 (1).xlsx from directory C:\Users\PC lLATER\Downloads\ to directory
C:\Users\PC WORLD\output\rep
how can I fix error?
how can I add more than file name into array like this ("extraction 10 Oct. 2022 (1).xlsx" , "report.pdf", "pictures.jpg","montly.docx")?


VBA Code:
Sub Move_Folder()

    Dim FSO As Object
    Dim SourcePath As String
    Dim DestPath As String
    Dim NameFile As String

    SourcePath = "C:\Users\PC lLATER\Downloads\"  '<< Change as needed
    DestPath = "C:\Users\PC lLATER\output\rep"    '<< Change as needed
    NameFile = "extraction 10 Oct. 2022 (1).xlsx"       '<< Change as needed

    Set FSO = CreateObject("scripting.filesystemobject")

    If FSO.FolderExists(Left(SourcePath, Len(SourcePath) - 1)) = False Then
        MsgBox SourcePath & " doesn't exist"
        Exit Sub
    End If

    'to move
    FSO.MoveFile Source:=SourcePath & NameFile, Destination:=DestPath

End Sub
is there any chance to make this code works efficiantly ?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
If these file names as static you could use your macro with only these few changes:
VBA Code:
Option Explicit
Sub Move_Folder()
    Dim FSO    As Object
    Dim SourcePath As String
    Dim DestPath As String
    Dim NameFile As Variant                       '<- changed
    Dim x      As Long                            '<- added
    SourcePath = "C:\Users\PC lLATER\Downloads\"  '<< Change as needed
    DestPath = "C:\Users\PC lLATER\output\rep"    '<< Change as needed
    NameFile = Array("extraction 10 Oct. 2022 (1).xlsx", "report.pdf", "pictures.jpg", "monthly.docx") '<- changed
    Set FSO = CreateObject("scripting.filesystemobject")
    If FSO.FolderExists(Left(SourcePath, Len(SourcePath) - 1)) = False Then
        MsgBox SourcePath & " doesn't exist"
        Exit Sub
    End If
    'to move
    For x = LBound(NameFile) To UBound(NameFile)  '<- added
        FSO.MoveFile Source:=SourcePath & NameFile(x), Destination:=DestPath '<- changed
    Next x                                        '<- added
End Sub
 
Upvote 0
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0
sorry buddy !
just I want getting rid of the error when one of them the file name is not existed
how can I add procedure to show me message what's the name file is not existed and finish the prcodure without do any thing ?
 
Upvote 0
Now the macro will give you feedback for the file names that had problems (missing source or destination already present).
VBA Code:
Option Explicit
Sub Move_Folder()
    Dim FSO    As Object
    Dim SourcePath As String
    Dim DestPath As String
    Dim NameFile As Variant
    Dim x      As Long
    Dim msgDiag As String
    msgDiag = "Had problems with:" & vbLf
    SourcePath = "C:\Users\PC lLATER\Downloads\"  '<< Change as needed
    DestPath = "C:\Users\PC lLATER\output\rep"    '<< Change as needed
    NameFile = Array("extraction 10 Oct. 2022 (1).xlsx", "report.pdf", "pictures.jpg", "monthly.docx")
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FolderExists(Left(SourcePath, Len(SourcePath) - 1)) = False Then
        MsgBox SourcePath & " doesn't exist"
        Exit Sub
    End If
    'to move
    On Error GoTo diag
    For x = LBound(NameFile) To UBound(NameFile)
        FSO.MoveFile Source:=SourcePath & NameFile(x), Destination:=DestPath
    Next x
    If msgDiag <> "" Then MsgBox msgDiag
    Exit Sub
diag:
    msgDiag = msgDiag & NameFile(x) & vbLf
    Resume Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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