Issues copy/move files

keithrl74

New Member
Joined
May 25, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am using excel with Macro to accomplish this action Hope the below makes sense, as I am lost as to why it post and error and does not copy/move files

The below code I am having trouble with copy/move files to a directory
I receive an error that the "directory does not exist" I have validated the the directory is vaild with the correct spelling.

The files are names ABBCCDD - BBBBBBB - CCCCCCC.docx
The destination folder would be the first part of the file name i.e. ABBCCDD and the files should be copied / moved into that folder.

I have watched that the variables myFilePrefix, mySrc, & myDest has the correct data.

when the code gets to "DelFile = True" the "FileCopy mySrc, myDest" at that point it goes to the "No Folder routine"


VBA Code:
Private Sub CommandButton1_Click()
 
    Dim myDestDir As String
    Dim myFileExt As String
    Dim i As Long
    Dim myFilePrefix As String
    Dim myFile
    Dim mySrc As String
    Dim myDest As String
    Dim DelFile As Boolean


'   Set up an array for all the different directories you wish to copy files from
'   Number in parentheses of variable declaration should be number of items in array - 1
   
    Dim mySourceDir(1)
    mySourceDir(0) = Environ$("USERPROFILE") & "\my documents\shells\" '"C:\Users\keith\Documents\shells\"
  
   
'   Set source directory where subfolders are found
    myDestDir = "C:\RIPS_AUTO\Mail\"
   
'   Designate file extensions to move
    myFileExt = "*.docx*"
 
 
 Loop through all each directory
    For i = LBound(mySourceDir) To UBound(mySourceDir)
'       Loop through each Excel file in each directory
        myFile = Dir(mySourceDir(i) & myFileExt)
        Do While Len(myFile) > 0
'           Get file prefix
            myFilePrefix = Left(myFile, InStr(1, myFile, "-") - 1)
'           Build source and destination references
            mySrc = mySourceDir(i) & myFile
            myDest = myDestDir & myFilePrefix & "\" & myFile
'           Set boolean value to delete file
            DelFile = True
'           Copy file from source to destination
            On Error GoTo No_Folder
            FileCopy mySrc, myDest
            On Error GoTo 0
'           Delete source file, if flag is true
            If DelFile = True Then Kill mySrc
'           Reinitialize myFile
            myFile = Dir
        Loop
    Next i
   
    MsgBox "Moves complete!"
   
    Exit Sub
   
No_Folder:
'   If cannot find direcory for a file, do not delete, return message box, and continue
    If Err.Number = 76 Then
        DelFile = False
        MsgBox "Folder " & myDestDir & myFilePrefix & " does not exist.", vbOKOnly, _
               "Cannot move file " & mySrc & "!!!"
        Err.Clear
        Resume Next
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If

End Sub
 
Last edited by a moderator:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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