logo
search
VBA & Macro Problems

How to Fix VBA Kill Statement Error When Deleting Temporary Files

Maira MehtabMaira Mehtab Sep 22, 2026 869 views

Question details

The user needs to resolve an issue where a VBA procedure fails and highlights the Kill statement during debugging when attempting to delete temporary files.

Product
Excel VBA
Device & OS
not provided
Scenario
Automating the deletion of temporary files in a specific directory using a VBA macro.
Observed behavior
The VBA script throws an error and the Kill statement is highlighted by the debugger.
Before you start

Before modifying your VBA script, verify that the target files are not currently open or locked by another application, and always test deletion scripts on a dummy folder first to prevent accidental data loss.

Solution 1Recommended

Correct VBA Syntax and Match Wildcard Patterns

Fix the Kill statement by removing unnecessary parentheses and ensuring the directory search pattern matches the deletion pattern.

A common cause for Kill statement errors in VBA is the use of parentheses around the path argument, which is invalid syntax for this command. Additionally, using mismatched wildcard patterns between your Dir check and your Kill execution can cause the script to attempt to delete files that do not exist or mismatch the intended target.

1
Remove Parentheses

Open the VBA Editor (Alt + F11), locate the erroring module, and remove any parentheses surrounding the argument in your Kill statement. Change it from 'Kill(path)' to 'Kill path'.

2
Define the Path and Wildcard

Declare a string variable for your path and apply a consistent wildcard. For example: stPad = "C:\TEMP\"

3
Check for Files Using Dir

Use the Dir function to verify files exist before attempting to delete them: If Dir(stPad & "*.*") <> "" Then

4
Execute the Kill Statement

Use the exact same wildcard pattern in the Kill statement: Kill stPad & "*.*" and then close your IF statement.

Wildcard Specifics: Using "*.*" targets all files with an extension. If you wish to delete files regardless of whether they have an extension, you can use the wildcard "*.?*" consistently in both your Dir and Kill statements.
Automate with WPS Spreadsheet

Write and Execute VBA Macros Effortlessly with WPS Office

WPS Office provides robust support for VBA macros, allowing you to write, debug, and run automation scripts seamlessly. Easily handle file operations like temporary file deletion using a familiar and lightweight developer interface.

  1. 1. Install WPS Office: Download and install the free version of WPS Office on your computer.
  2. 2. Access Developer Tools: Open WPS Spreadsheet and navigate to the Developer tab on the top ribbon.
  3. 3. Open the VBA Editor: Click on 'Visual Basic' to open the VBA Editor and paste your corrected macro script.
  4. 4. Run Your Macro: Execute the macro to automatically manage and delete your specified temporary files without syntax errors.
Full compatibility with Microsoft Excel VBA syntax and file structuresLightweight application that executes complex macros efficientlyBuilt-in developer tools for identifying and debugging script errors
microsoft office alternative - wps office

Frequently Asked Questions

Why do I get a 'Permission Denied' error when using the VBA Kill statement?

This error occurs if the file you are attempting to delete is currently open in another program, is set to Read-Only, or if you lack sufficient Windows user permissions to modify the target directory.

Can the VBA Kill statement delete entire folders?

No, the Kill statement is exclusively used for deleting files. To remove an empty directory, you must use the RmDir statement instead.

How do I delete a specific file type using the Kill statement?

You can specify the target file extension in the Kill statement's path string by using a wildcard. For example, Kill "C:\Temp\*.txt" will only delete text files within that specific directory.

What happens if the Kill statement tries to delete a file that doesn't exist?

If the specified file is not found, VBA will throw a runtime error (Error 53: File not found). This is why it is highly recommended to use the Dir function to check if the file exists before executing the Kill command.