How can I use VBA to delete files? 2

How can I use VBA to delete files?

VBA (Visual Basic for Applications) is a programming language that allows users to automate tasks in Microsoft Office applications. This includes the ability to delete files using VBA code. To use VBA to delete files, the user can use the “Kill” function, which allows for the deletion of a specific file or a group of files based on certain criteria. This can be useful for automating file management tasks, such as removing outdated or unnecessary files. Additionally, VBA can be used to create a more efficient and streamlined process for deleting files, saving time and effort for the user.

Delete Files Using VBA (With Examples)


You can use the Kill statement in VBA to delete a specific Excel file in a specific folder.

Here is one common way to use this statement in practice:

Sub DeleteFile()

    On Error Resume Next
    Kill "C:UsersBobDesktopMy_Datasoccer_data.xlsx"
    On Error GoTo 0End Sub

This particular macro deletes the Excel file called soccer_data.xlsx located in the following folder:

C:UsersBobDesktopMy_Data

The line On Error Resume Next tells VBA that if an error occurs and the file is not found that no error message should be shown.

We then use On Error GoTo 0 to reset the default error message settings.

If you would like to display an error message if the file is not found, then simply remove these two lines from the code.

The following example shows how to use this syntax in practice.

Example: Delete File Using VBA

Suppose we have the following folder with three Excel files:

Suppose we would like to use VBA to delete the file called soccer_data.xlsx.

We can create the following macro to do so:

Sub DeleteFile()

    On Error Resume Next
    Kill "C:UsersBobDesktopMy_Datasoccer_data.xlsx"
    On Error GoTo 0End Sub

Once we run this macro and open the folder again, we will see that the file called soccer_data.xlsx has been deleted:

If you would like an error message to be shown if the file doesn’t exist, you can use the following macro instead:

Sub DeleteFile()

    Kill "C:UsersBobDesktopMy_Datasoccer_data.xlsx"

End Sub

When we run this macro, we receive the following error message:

We receive this error message because the file soccer_data.xlsx has already been deleted and no longer exists in the folder.

Note: Be aware that the Kill statement permanently deletes a file and does not simply send it to the recycling bin.

Cite this article

stats writer (2024). How can I use VBA to delete files?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-delete-files/

stats writer. "How can I use VBA to delete files?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-delete-files/.

stats writer. "How can I use VBA to delete files?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-delete-files/.

stats writer (2024) 'How can I use VBA to delete files?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-vba-to-delete-files/.

[1] stats writer, "How can I use VBA to delete files?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I use VBA to delete files?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top