Delete Files on Mac

Note : When you use VBA in Mac Excel 2016 or higher that is working with files and folders you will notice that it is possible that it will ask you permission to access the file or folder (Grant File Access Prompt), this is because of Apple’s sandbox requirements.This means that when you want to save/open files or check if it exists with VBA code the first time you will be prompted to allow access on the first attempt to access such a folder or file. If you want to avoid problems like this read this page : Problems with Apple’s sandbox requirements

Note: The code examples with wildcards below are working in 2019 and O365, 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 docx 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
26-February-2021
Web design by Will Woodgate