Как в vba excel удалить файл
Перейти к содержимому

Как в vba excel удалить файл

  • автор:

VBA Delete File / Workbook

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:

Image 1. Delete a single file

Here is the code which will delete the file:

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

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:

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

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


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.

VBA Delete File in Excel

VBA Delete File

VBA Delete File in Excel from a folder or directory. You can delete any file like Excel, Word, PowerPoint, CSV, Notepad etc. Let us see different examples in the following tutorial. We are using Kill statement to delete a file.

Example on VBA Delete File

Let us see an example how to delete a file in Excel using VBA. The following example helps to delete an Excel file.

Note:You can check your output by creating sample file in specified folder before and after running macro.

The following macro helps to delete Word document file.

The following VBA code helps to delete CSV file.

Example on Deleting a file using VBA FSO Object

Let us an example VBA macro code to delete Excel file using VBA FileSystemObject (FSO) in Excel.

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Delete Workbook (Excel File)

To delete an Excel file from a folder you can use two different methods. The first method is the “Kill” statement which takes the file path to refer to the file that you wish to delete. The second method is the FileSystemObject object which has a method associated with it to delete a file.

To use these codes, go to the VBE (Code Editor) from the developer tab.

Delete a File using VBA (Kill Function)

Kill function helps you to delete a single file or multiple files, and use wildcard characters to delete more than one file. Below is the one-line code that deletes the file from the folder that I have on the desktop.

This code will show you an error if the workbook that you specified to delete doesn’t exist.

Delete All the Files from a Folder using VBA

And if you want to delete all the files that you have in a folder, you can use a wildcard character.

Delete a File using the FileSystemObject (Object)

The file system object provides you with access to the computer’s file system. You can learn about it from here, but now, let’s write a code to remove a file.

  • First, you need to declare variables to use in the code as a workbook and FileSystemObject.
  • After that, use the create object function to return the FileSystemObject as assign it to the FSO variable.
  • Next, assign the path to the “myFile” variable to access the file that you wish to delete.
  • In the end, use the “DeleteFile” method (see this) to delete the file.

Full Code

Let’s say you need to write a code that can check for a file, (exists or not) and then delete it. Here’s the code that you need.

VBA Delete File

In VBA, we can delete any file present in the computer using VBA codes. The code used to delete files is known as the “Kill” command. The method to delete any file is that first, we must provide the file’s path, which means the file’s location on the computer. Then, we use the “Kill” command to delete the file.

Table of contents

How to Delete Files using VBA Code?

VBA is a tough thing initially, but as you spend more time with VBA, you will start loving it just like me. We can open files from another computer folder and work with them. Now, we can delete files as well by using VBA coding. This article will show you how to delete files using VBA code in a specific folder.

When working with large projects, we usually create many intermediate files to support our process. we need to delete those files to avoid future confusion after completing the work.

And one scenario is when we usually receive an email. For example, we save attachments for our regular work, or we want to see the report for that point in time, and later we may need to delete those files.

Deleting those files manually may take time, or we may forget to save them. In addition, it will occupy the space on our computer. Therefore, we will show you how to delete those files with simple VBA codes Simple VBA Codes VBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task. read more .

VBA Delete File

You are free to use this image on your website, templates, etc., Please provide us with an attribution link How to Provide Attribution? Article Link to be Hyperlinked
For eg:
Source: VBA Delete File (wallstreetmojo.com)

Kill Method to Delete Files in a Folder using VBA Code

A simple Kill function will delete the folder, specific file, all Excel files, etc. Take a look at the syntax of the Kill method in VBA. The Kill method cannot delete read-only files.

vba delete file - kill method

Path Name: Pathname is nothing but the folder path in the computer to delete the files.

Asterisk (*) is useful to match any length string. In addition, it also considers zero.

Question mark (?) is useful to match only a single character.

Delete Particular File Name

For example, we have a folder like the one below.

vba delete file example 1.1

We want to delete the file named “File 5” in this folder. But, first, start the code with the Kill function.


vba delete file example 1.2

Copy and paste the folder path.

vba delete file example 1.3

And Paste in double-quotes.

vba delete file example 1.4

Now put one more backward slash () and enter the file name with extension.

VBA Deletef example 1.5

When you run this code, it will delete the file named “File 5.xlsx” in the mentioned folder path.

Delete All Excel Files

Using VBA, we need to use wildcard characters with the Kill function to delete all the Excel files in the folder. After mentioning the folder path, we need to mention the file as “*.xl*.”


VBA Deletef example 2.1

When you run this code, it will delete all the Excel files in the folder.

We have seen how we can delete a single Excel file and all the Excel files. But if we want to delete all the files in the folder, how can we delete them? Also, since we are using Excel VBA, can it delete other files?

The answer is Yes! Use the below code to delete all the files in the folder.


VBA Deletef example 3.1

Delete Entire Folder Only

Is it possible to delete the entire folder itself?

Yes, it is possible.

We need to delete all the files in the folder using the Kill function. Then to delete the folder, we need to use one more function called RmDir.


VBA Deletef example 4.1

Here RmDir will delete only the empty folder if any subfolder is where it cannot delete them.

Delete All the Text Files in the Folder

To delete all the text files in the folder, use the below code.


VBA Deletef example 5.1

Delete Read-Only Files

As we said, the Kill function cannot delete “Read Only” files in the folder. In such a scenario, we need to use the other two functions: “Dir$” and “SetAttr.” Below is the example code to delete the read-only files as well.


VBA Deletef example 6.1

You can download this VBA Delete File Excel Template from here – VBA Delete File Excel Template.

Recommended Articles

This article has been a guide to VBA Delete Files. Here, we discuss the VBA code to delete 1. Particular File Name, 2. All files, 3. Entire folder, and 4. Read-only Files with downloadable Excel template. Below are some useful articles related to VBA: –


juan says

thank you so much .

Dheeraj Vaidya says

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *