How to Replace Existing Excel File

B

Bruce Bates

Please help, I'm trying to save a worksheet to a CSV file. The following
code works but requires the user to select "Yes"to replace the existing
file. What do I need to include to automatically overwrite the file with the
new one.

Thanks for any help.
Bruce Bates

ActiveWorkbook.SaveAs Filename:="Ngatapa", FileFormat:= _
xlCSV, CreateBackup:=False


ActiveWorkbook.Saved = True
ActiveWorkbook.Close
 
N

Nigel

Hi Bruce
Wrap the saveas line as follows...

Application.DisplayAlerts = False

ActiveWorkbook.SaveAs Filename:="Ngatapa", FileFormat:= _
xlCSV, CreateBackup:=False

Application.DisplayAlerts = True

Cheers
Nigel
 
E

e18

Same topic - another question:

I do not want my macro to overwrite, so I have the DisplayAlerts=True
The problem is when I answer "No" to the question if I want to replace
I get run-time error.

Thanks in advance,
Erlend

filetypes = "Text files (*.txt),*.txt,All files (*.*),*.*"

filename = "C:\bbar\" & name & ".txt"
fn = Application.GetSaveAsFilename(InitialFilename:=filename
Filefilter:=filetypes, FilterIndex:=1)

If fn = False Then Exit Sub

ActiveWorkbook.SaveAs Filename:=fn, FileFormat:= _
xlText, CreateBackup:=Fals
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top