VBA Code to Delete File

VBA Code to Delete File

In this article we are going to show you how you can delete file or files using a single line of VBA code.

There are three types of deletion you may want to perform:

1. Delete a specific file from the folder

VBA Code to Delete File
Here is the VBA code to delete a file from the folder.

Kill "E:WorkExcelSirJi Test 3.txt"

2. Delete specific type of files from the folder.

For example, you may want to delete all .txt files from the folder and do not want to delete any other types of files
VBA Code to Delete File
Here is the VBA code to delete specific types of files from the folder:

Kill "E:WorkExcelSirJi*.txt"

3. Delete all files from the folder

VBA Code to Delete File
Here is the VBA Code to delete all files from the folder:

Kill "E:WorkExcelSirJi*"

Note
This code does not delete files inside the sub-folders in the given path

How can I use this VBA code?

If you want to use this code in your VBA tool, then follow below steps:

Step 1: Open the Excel file in which you want to copy this code

Step 2: Press Alt+F11

Step 3: Open the module in which you want to add this code

Step 4: Paste this code just after the code where you want to delete the file(s)

VBA Code to Delete File

What will happen when I run this code?

The code will delete file(s) from the given folder

Thanks for reading the article, subscribe us to get more VBA tricks

Author:
Excelsirji.com was started with the thought of making people experience easy while working with Excel, data analytics, vba, automations etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a Data Enthusiast?

Join us for a ride on your data and automate your stuffs

Business Analytics
Data Analyst
Programming for Data Science
Predictive Analytics for Business