Error handling - renaming files and moving to new location

keaveneydan

Board Regular
Joined
Apr 29, 2014
Messages
144
Hi

I am trying to automate a process and it works 80% of the time. The macro goes to a directory location and renames each file using it's lastmodified date. However the macro seems to trip itself up by renaming and moving a file and then trying to open it again from it's previous location....but it isn't there anymore.
Can anyone help with a good error handling solution that won't
1. Just kill the macro
2. Let the mcaro run continuously when all the files have been moved

Something like End and then start again from beginning

Thanks



Code:
Sub RenamePortFiles()
Application.DisplayAlerts = False
        Application.ScreenUpdating = False
Dim FilePath As String
Dim StopMacro As Boolean
Dim CurrentFile As String
Dim FileDate As String
    
    FilePath = "S:\Market_Funds\PORT Data\FTP download\"
    If Len(Dir(FilePath & "Renamed Files", vbDirectory)) = 0 Then
        MkDir (FilePath & "Renamed Files")
    End If
    Do Until StopMacro = True
        CurrentFile = Dir(FilePath & "*.xlsx")
        If CurrentFile = "" Then
            MsgBox "All Excel Files renamed.", vbOKOnly + vbCritical, "No Excel Files Found"
            Exit Sub
        End If
            Workbooks.Open FILENAME:=FilePath & CurrentFile
    Dim FSO As New FileSystemObject
    Dim aFile As File
            Set aFile = FSO.GetFile(FilePath & CurrentFile)
            FileDate = Format(aFile.DateLastModified - 1, "YYYYMMDD")
            ActiveWorkbook.SaveAs FILENAME:=FilePath & "Renamed Files\" & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 13) & " " & FileDate
                Set aFile = Nothing
            ActiveWorkbook.Close
        Kill FilePath & CurrentFile
    Loop
    
    Application.DisplayAlerts = True
        Application.ScreenUpdating = True
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,215,257
Messages
6,123,916
Members
449,133
Latest member
rduffieldc

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