Deleting a file from a folder using VBA

This question is probably pretty straight forward but given the consequences if I get it wrong I thought I would ask.

I have a table that has the name of all the files in a particular folder on my c: drive.  If I delete it out of the table I want it to delete the file out of the folder too.  I have the functionality to import the file names and to delete the file name out of the table I need to delete the file now out of the folder.  

How would I do this.  I get once the file is deleted it is gone, so that is why I am asking.
Who is Participating?
You can use the Kill() statement in VBA to delete a FileName:

Kill Filename

Jeffrey CoachmanMIS LiasonCommented:
Just a note, that in some situations, "Kill" is prohibited in VBA because of certain security settings...

In this case you will have load a reference to the Windows Scripting Runtime Library in your VBA editor and use code like this:

Dim fso As FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.DeleteFile ("C:\YourFolder\YourFile.xls")

Patrick MatthewsCommented:
No points for this, please :)

You can run Jeff's code above without setting a reference by changing:

Dim fso As FileSystemObject


Dim fso As Object

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Hi Patrick:  I have to ask: "Why would you want to?"  It has aspects of using a table saw without a guard ;-)
Patrick MatthewsCommented:

1) Laziness :)

2) Not really applicable for the Scripting Runtime library, but I try to do most of my external references using late binding so as to keep maximum flexibility if there may be a range of versions in use

mtrussellAuthor Commented:
<...range of versions in use> -  Thanks, completely overlooked that.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.