-1

I'm trying to move ALL excel files from one folder path to another and I'm getting a permission denied error. Here's my code:

Sub Move_Certain_Files_To_New_Folder()

    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim FileExt As String
    Dim FNames As String

    FromPath = "R:\FromPath\Files"  '<< Change
    ToPath = "R:\ToPath\Backup" & Format(Now, "yyyy-mm-dd h-mm-ss") 

    FileExt = "*.xl*"

    If Right(FromPath, 1) <> "\" Then
        FromPath = FromPath & "\"
    End If

    FNames = Dir(FromPath & FileExt)
    If Len(FNames) = 0 Then
        MsgBox "No files in " & FromPath
        Exit Sub
    End If

    Set FSO = CreateObject("scripting.filesystemobject")

    FSO.CreateFolder (ToPath)

    FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
    MsgBox "You can find the files from " & FromPath & " in " & ToPath

End Sub

The error is from this part: FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath Appreciate any help! :)

Mathieu Guindon
  • 65,145
  • 8
  • 95
  • 208
Meedee
  • 129
  • 1
  • 3
  • 13

1 Answers1

1

Moving a file can't be done without exclusive access to that file. If another process has it opened (which would cause that runtime error 70 you're getting), there's not much you can do, other than try moving the next file, and try that locked file again later.

Or you can copy it to destination instead. But then, whatever has the file opened probably means to make changes to it, and your copy won't have them.

Mathieu Guindon
  • 65,145
  • 8
  • 95
  • 208