VBA: How to save excel file as a separate .txt file

iand5

New Member
Joined
Jul 26, 2017
Messages
36
Hi, I currently have the following code to save an Excel file as a .txt file.

Code:
Sub Macro1()

'Saves the Activesheet to the Desktop as a text file with a different extension
Dim newFile As String, fName As String, gName, fileSaveName
    gName = "FGM_"
    newFile = gName & Format$(Date, "dd-mm-yyyy")
    fileSaveName = Application.GetSaveAsFilename(newFile, "Text (MS-DOS) (*.txt), *.txt")
    ThisWorkbook.SaveAs fileSaveName, xlTextMSDOS
End Sub

The problem is that when the .txt file is saved, it overrides the excel file.

How can I save the .txt file so that is separate from the excel file?

Thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You could try the SaveCopyAs method instead of SaveAs.
 
Upvote 0
I get a compiler error.

"Wrong number of arguments or invalid property assignment".
Yep, I was just going to amend my post and say that would not work because it does not allow for a different file format. It works great for the same format though. I guess the only solution in your case is to add a line of code to open the original workbook and a line to close the new one.
 
Last edited:
Upvote 0
Code:
Sub Macro1()
'Saves the Activesheet to the Desktop as a text file with a different extension
Dim newFile As String, fName As String, gName As String, fileSaveName As String, oldFile As String
oldFile = ThisWorkbook.Name [COLOR=#ff0000]'capture the current file name
  [/COLOR]  gName = "FGM_"
    newFile = gName & Format$(Date, "dd-mm-yyyy")
    fileSaveName = Application.GetSaveAsFilename(newFile, "Text (MS-DOS) (*.txt), *.txt")
    ThisWorkbook.SaveAs fileSaveName, xlTextMSDOS
Workbooks.Open oldFile [COLOR=#ff0000]'Re-open original file[/COLOR]
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,800
Messages
6,121,641
Members
449,044
Latest member
hherna01

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