Deleting a file from VBA

Bengt

Active Member
Joined
Mar 4, 2008
Messages
267
I want to do the following: I want to save my active workbook as an XMLSpreadsheet (format:=xlXMLSpreadsheet) to another directory under another name. If a file with that name already exists on that directory, I want to delete that file first, and then perform the saveas on my ActiveWorkbook. I understand how to do with the SaveAs-part, but how does the code look to delete a file (It is assumed of course, that I have the right to delete the particular file)?

Bengt
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
For instance:

Code:
Sub Delete_a_File()

    Const sFileName As String = "C:\MyTestXMLfile.XML"
    
    If Len(Dir(sFileName)) Then Kill sFileName

End Sub
 
Upvote 0
Of course, thank you. I overlooked the existence of the "Kill"-statement.

A follow up: If I have VBA-code in a workbook and then close that particular workbook, can I still execute statements that follow after the close-statement?
 
Upvote 0
Hi there,

At home and in Excel2000, so no ability at xml, so...this may well be off the mark, but couldn't you just kill alerts while overwriting the file?

Rich (BB code):
Option Explicit
    
Sub TryCodeAfterClose()
    
    With ThisWorkbook
        Application.DisplayAlerts = False
        .SaveAs .Path & .Application.PathSeparator & Left(.Name, InStrRev(.Name, ".") - 1) & "_ver2", xlWorkbookNormal
        Application.DisplayAlerts = True
        ThisWorkbook.Close False
    End With
    MsgBox "Maybe someone knows a way, but I sure don't"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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