Sub Move_DeleteFilesV1B()
'
Dim ArrayRow As Long, LastRowColumnA As Long
Dim ProblemFileCounter As Long
Dim ArchivePath As String
Dim FileToProcess As String
Dim ProblemFilesArray() As String
Dim ProblemSheetName As String
Dim InputArray As Variant
'
ArchivePath = "P:\Archive\" ' <--- Set this to the Archive path to be used
ProblemSheetName = "Problem Files" ' <--- Set this to the name of the sheet to be added to store
' ' the names of files that were not processed properly
LastRowColumnA = Range("A" & Rows.Count).End(xlUp).Row ' Find last row used in Column A
'
InputArray = Range("A1:H" & LastRowColumnA) ' Load all data from sheet into InputArray
ProblemFileCounter = 0 ' Initialize ProblemFileCounter
'
'-------------------------------------------------------------------------------
'
For ArrayRow = 1 To LastRowColumnA ' Loop through all used rows of the sheet
If LCase(Trim(InputArray(ArrayRow, 2))) = "delete" Or _
LCase(Trim(InputArray(ArrayRow, 2))) = "archive" Then ' If File in the row is marked for archive or deletion then ...
InputArray(ArrayRow, 8) = Trim(InputArray(ArrayRow, 8)) ' Remove forward and trailing spaces from path
'
If Right$(InputArray(ArrayRow, 8), 1) <> "\" Then _
InputArray(ArrayRow, 8) = InputArray(ArrayRow, 8) & "\" ' Append a '\' to end of path name if not already there
'
FileToProcess = InputArray(ArrayRow, 8) & Trim(InputArray(ArrayRow, 1)) ' Combine data columns to create full path of file to archive
End If
'
If LCase(Trim(InputArray(ArrayRow, 2))) = "delete" Then ' If File in the row is marked for deletion then ...
On Error GoTo ErrorHandler ' Enable our error-handling routine.
If Dir$(FileToProcess) <> "" Then ' If FileToProcess exists then ...
SetAttr FileToProcess, vbNormal ' Set FileToProcess to a deletable status
Kill FileToProcess ' Permanently delete the FileToProcess
End If
ElseIf LCase(Trim(InputArray(ArrayRow, 2))) = "archive" Then ' If File in the row is marked for Archive then ...
On Error GoTo ErrorHandler ' Enable our error-handling routine.
If Dir$(FileToProcess) <> "" Then ' If FileToProcess exists then ...
If Dir$(ArchivePath, vbDirectory) <> "" Then ' If ArchivePath exists then ...
Name FileToProcess As ArchivePath & InputArray(ArrayRow, 1) ' Move the file
End If
End If
End If
CheckNextFile:
On Error GoTo 0 ' Return Error handling back over to Excel
Next ' Loop back to check for next FileToProcess
'
'-------------------------------------------------------------------------------
'
If Not Not ProblemFilesArray Then ' If any files couldn't be deleted then ...
'
Sheets.Add(Before:=Sheets(1)).Name = ProblemSheetName ' Add a sheet to store the file names that weren't processed properly
'
Sheets(ProblemSheetName).Range("A1").Resize(UBound(ProblemFilesArray)) _
= Application.Transpose(ProblemFilesArray) ' Display the problem file names to the added sheet
Sheets(ProblemSheetName).Columns(1).AutoFit ' Adjust the column width of column A to display entire file names
End If
'
MsgBox "Script has completed." ' Notify the user that the script has completed
Exit Sub ' exit the sub
'
'-------------------------------------------------------------------------------
'
ErrorHandler:
ProblemFileCounter = ProblemFileCounter + 1 ' Increment ProblemFileCounter
ReDim Preserve ProblemFilesArray(1 To ProblemFileCounter) ' Increase the size of the ProblemFilesArray
ProblemFilesArray(ProblemFileCounter) = FileToProcess ' Save the file path and name of the file that wasn't processed into ProblemFilesArray
Resume CheckNextFile ' Remove error encountered and return back to check for the next file
End Sub