Deleteing a file in VBA

AJSatori

Board Regular
Joined
Jul 11, 2002
Messages
62
I am tring to delete a file in one folder after I have saved it into another. Any suggestions?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
What do you mean? I don't think you can delete the current file while you are working on it.

Judging from your previous post, if you mean delete it form the previous directory after you saved it to a new one, then use can use:

Code:
Kill "C:\" & ThisWorkbook.Name

(this example assumes C:\ is the old directory, changes yours to fit_
 
Upvote 0
You cannot delete a file that is open. Here's a some examples of how to "move" a file into a new directory.

Code:
Sub OpenandRename()

Workbooks.Open "C:\Test\OldFile.xls"

Activeworkbook.SaveAs FileName:="C:\New\NewFile.xls"
Activeworkbook.Close False

Kill "C:\Test\OldFile.xls"

End Sub

Another way using the name statement.

Code:
Sub MoveFile()

Name "C:\Test\OldFile.xls" as "C:\New\NewFile.xls"

End Sub

NOTE: Haven't tested, but both should work.
 
Upvote 0
Yes you can delete an open file or ThisWorkbook active file, and here is how.

One friendly piece of advice, make **** sure you save a copy of your victim workbook beforehand, in case you change your mind after the suicide code is executed.

'''''''''''''''''''''''''''''''''''''''''''''

Sub Suicide()
Dim KillShot As Integer
KillShot = MsgBox("Wanna kill this file?" & vbCrLf & "Be sure to make a backup if you change your mind later.", 36, "Feeling lucky?")
Select Case KillShot
Case 7
MsgBox "You live another day.", 64, "Spared the gallows, kill cancelled."
Exit Sub
Case 6
MsgBox "Click OK to flip the kill switch.", 48, "I hope you know what you're doing."
With ThisWorkbook
.Saved = True
.ChangeFileAccess xlReadOnly
Kill .FullName
.Close False
End With
End Select
End Sub
 
Upvote 0
Tom,

I didn't know that. Interesting, but SCARY...
 
Upvote 0
I stand corrected.
bow.gif
 
Upvote 0
As I PM'd to TG, a little knowledge can be a dangerous thing. However, it's a good practice to responsibly to learn the boundaries of Excel's limitations, not just what you read in books. If nothing else, you'll be more aware of what the bad guys can do, to at least stay on your guard for yourself and your clients.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top