If you want to avoid the current name of your excel file being changed, just save the current worksheet, not the whole workbook (the VBA equivalent of the SaveAs function is ActiveWorkbook.SaveAS
, to save just the current sheet use ActiveSheet.SaveAS
).
You can use the following macro:
Sub Macro1()
Application.DisplayAlerts = False
ActiveSheet.SaveAs Filename:="NewFile.txt", FileFormat:=xlTextWindows
Application.DisplayAlerts = True
End Sub
Toggling the DisplayAlerts
property avoids a message box that is displayed if the given file already exists.
If want to save more than one sheet, you need to iterate through the Sheets
collection of the ActiveWorkbook
object and save each sheet to a separate file.