Excel VBA to Delete All Files in a Folder

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
Excel VBA to Delete All Files in a Folder
Use VBA to remove files in a folder. (Chad Baker/Digital Vision/Getty Images)

Computer programmers often create temporary files used in their applications, and it's good programming practice to delete these files when they are no longer in use. In Visual Basic for Applications, the "Kill" statement is available to delete files from a disk. This statement takes one argument that specifies one or more file names to be deleted. The argument also supports wild cards such as "*" to specify multiple files to be deleted in a folder.

Skill level:
Moderately Easy


  1. 1

    Launch Excel, click the "Developer" tab and click "Visual Basic" to open the Visual Basic Editor. Click the "Insert" menu and click "Module" to add a new module.

  2. 2

    Create a new sub-procedure in your module by adding the following VBA code:

    Private Sub deleteAllFiles().

  3. 3

    Type the following code to create a new variable and define the path for your files:

    Dim file Path As String

    file Path = "C:\Temp*.*"

  4. 4

    Use the following line of code to delete all the files in the folder you defined: Kill file Path.

  5. 5

    Finish your sub-procedure by typing "End Sub." Press "F5" to execute the procedure.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.