The code below works to delete a File or Folder from within a excel Spreadsheet Using the selected cell that contains the full path of either the file or folder.
The issue I'm having is that I want to have some sort of safety that it just does not delete the file but it also appears in their recycle bin.
But for some reason I just cannot get any code to delete the file and it appears in the recycle bin any assistance will be appreciated.
The issue I'm having is that I want to have some sort of safety that it just does not delete the file but it also appears in their recycle bin.
But for some reason I just cannot get any code to delete the file and it appears in the recycle bin any assistance will be appreciated.
VBA Code:
Sub DeleteFileOrFolderBasedOnCellPath()
Dim fso As Object
Dim Path As String
Set fso = CreateObject("Scripting.FileSystemObject")
Path = ActiveCell.Value ' Get the path from the selected cell
If fso.FileExists(Path) Then ' Check if the path is a file
On Error Resume Next
Kill Path ' Delete the file
On Error GoTo 0
If Not fso.FileExists(Path) Then ' Check if the file is deleted
MsgBox "File deleted successfully." ' Display success message
Else
MsgBox "Failed to delete the file." ' Display failure message
End If
ElseIf fso.FolderExists(Path) Then ' Check if the path is a folder
On Error Resume Next
fso.DeleteFolder Path, True ' Delete the folder
On Error GoTo 0
If Not fso.FolderExists(Path) Then ' Check if the folder is deleted
MsgBox "Folder deleted successfully." ' Display success message
Else
MsgBox "Failed to delete the folder." ' Display failure message
End If
Else
MsgBox "File or folder not found." ' Display message if the file or folder doesn't exist
End If
Set fso = Nothing
End Sub