0

Hi I want to delete an directory files using kill method in excel VBA for example :

Kill "D:\*.*"

but every time that a file on that directory is open it gives me this error and terminate the whole code :

runtime error 70 permission denied

is there any way to jump over that file (Ignore the error on that specific file) and continue to delete other files ?

braX
  • 11,506
  • 5
  • 20
  • 33
  • That is simply not possible. The error comes deep from the operating system, not from VBA or Excel. If a file is open, you can't delete it - what should happen if you for example have an Excel file open, delete it in the background and then Excel needs to reread or write some information? – FunThomas Dec 21 '21 at 12:54
  • Have you tried error trapping it? https://stackoverflow.com/questions/1038006/what-are-some-good-patterns-for-vba-error-handling – braX Dec 21 '21 at 13:01
  • 1
    Could you explain (in detail) why you would need to do this? – VBasic2008 Dec 21 '21 at 13:37
  • You can use `On Error Resume Next` to skip the error when you fail to delete a file. To reset the option (show errors again) you can use `On Error Goto 0` – Toddleson Dec 21 '21 at 17:31
  • You could use something like the code here: https://stackoverflow.com/questions/24086307/kill-file-that-is-currently-locked-without-admin-rights/24087002#24087002 That's about deleting locked files but you can easily modify it to remove just the unlocked ones. – Tim Williams Dec 21 '21 at 17:50

0 Answers0