Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Deleting a File in a Macro.
by Allen Wyatt
(last updated April 17, 2021)
Sometimes you may use a macro to create temporary files which you later need to delete. Similarly, you may need to just delete a file within a macro. You can accomplish this task using the Kill command. This is a holdover from other versions of BASIC. The syntax is:
where File is the full path and file name of the file you want to delete. When you delete a file in this manner, the file is not moved to the Windows Recycle bin; instead, it is immediately deleted from your drive.
If desired, you can also use wildcard characters in the File specification. For instance, if you wanted to delete all the files in the current directory that end in the TMP extension, you could use a command like this:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10001) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Deleting a File in a Macro.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
One of the automatic macros you can set up in Excel is one that is triggered when a workbook is closed. This tip explains ...Discover More
You can, within a macro, save a workbook in several different file formats that are understood by Excel. However, you may ...Discover More
Copying worksheets (one or many) is easy to do manually. What is not well known is that it is even easy to make the ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.