VBA Delete File / Workbook

Written by

Mel Jenkins

Reviewed by

Steve Rynearson

Last updated on July 19, 2021

VBA allows you to delete an existing file, using the Kill command. In this tutorial, you will learn how to delete a specific file or multiple files.

If you want to learn how to copy and rename a file, you can click on this link: VBA Copy File

 

Delete a Single File (or Workbook) in VBA

We will show how to delete the file Sample file 1.xlsx in the folder VBA Folder. The folder with the file now looks like in Image 1:

vba-delete-file

Image 1. Delete a single file

 

Here is the code which will delete the file:

Kill "C:\VBA Folder\Sample File 1.xlsx"

After running the code, the file Sample file 1.xlsx is now deleted from the VBA Folder. The output is in Image 2:

vba-delete-file-result

Image 2. File deleted from the C:\VBA Folder

 

Delete All Excel Files From the Folder

The same command enables you to delete all Excel files from the folder. You just need to put an asterisk (*) instead of the file name. An asterisk replaces any string. Here is the code:

Kill "C:\VBA Folder\*.xlsx"

As you can see in Image 3, all Excel files from Folder VBA are deleted:

vba-delete-all-files

Image 3. Delete all Excel files from the C:\VBA Folder

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! vba save as


Learn More!
vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples