Microsoft Excel

Ron de Bruin
Excel Automation

Microsoft MVP Program

Use VBA Kill to delete files

Note: The code examples below are only working in O365 MacExcel 2016 and above, be sure your install have installed the latest updates.

Note: You should also note the use of wildcard character (*) in the macro. You can use it to search for all files of a specific type: *.pdf only for PDF files or *.docx to delete only Microsoft Word 2007 or later files.

Sub DeleteExample1()
'You can use this to delete all the files in the folder TestFolder
    On Error Resume Next
    Kill "/Users/rondebruin/Desktop/TestFolder/*.*"
    On Error GoTo 0
End Sub

Sub DeleteExample2()
'You can use this to delete all xl? files in the folder TestFolder
'Easy to change it to for example only for xlsm files (*.xlsm)
    On Error Resume Next
    Kill "/Users/rondebruin/Desktop/TestFolder/*.xl*"
    On Error GoTo 0
End Sub

Sub DeleteExample3()
'You can use this to delete one xls file in the folder TestFolder
    On Error Resume Next
    Kill "/Users/rondebruin/Desktop/TestFolder/ron.xls"
    On Error GoTo 0
End Sub
 

Important, read also this page :

Problems with Apple’s sandbox requirements and Mac Office 2016 with VBA code

On this page you find information for Excel for Windows
http://www.rondebruin.nl/win/s4/win004.htm