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
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,958
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
For instance:

Code:
Sub Delete_a_File()

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

End Sub
 

Bengt

Active Member
Joined
Mar 4, 2008
Messages
267
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?
 

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,402
Messages
5,601,475
Members
414,452
Latest member
Dannysamworth

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
Top