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 January 13, 2020)
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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Excel doesn't allow you to run a macro while editing the contents of a cell. The only solution is to get out of Edit ...Discover More
Need to know the character code used for a particular character? In a macro you can use the Asc function to determine the ...Discover More
Need to run a DOS command from within one of your macros? The answer is the Shell command, described in this tip.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.