Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA : How to delete a file?

Tags:

excel

vba

Is there VBA code that can delete Excel files?

Any help or example would be welcome.

like image 601
Studix Avatar asked Dec 07 '25 07:12

Studix


1 Answers

Here is something I use. Very clean and effective:

Sub DeleteTEMPIMPORTWorkbook()

    Dim MyDir As String, fn As String
    MyDir = CreateObject("WScript.Shell").SpecialFolders("MyDocuments") & "\My Files" ' Change this to the directory where the file you need to delete is located.

    On Error Resume Next
    Kill MyDir & "\TEMPIMPORT.xlsx"
    On Error GoTo 0

End Sub

Please note, using Kill does NOT send the file to the Recycle Bin. It will permanently delete the file.

like image 142
Iron Man Avatar answered Dec 09 '25 23:12

Iron Man