VBA code for Save As Text(MS-DOS).txt file w/ choice of location

susanmarie2300

New Member
Joined
Jan 12, 2016
Messages
4
I would like my users to be able to hit a button in the spreadsheet and have the Save As dialog box pop up already set to save the document as ta Text(MS-DOS).txt file but still allow them to choose the name & location where the file will be saved.

Can anyone help me with the code for this? I tried creating a macro but it doesn't give me the code.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Something like this...

Code:
Sub test()
Dim lngCount As Long
Dim str As String
With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .Show
    
    For lngCount = 1 To .SelectedItems.Count
        str = .SelectedItems(lngCount)
    Next lngCount

End With

ActiveWorkbook.SaveAs ActiveWorkbook.Name & ".txt", xlTextMSDOS

End Sub
 
Upvote 0
So, after looking at responses from both Sektor & Irobbo314 below is the code that is working for what I want.
Is there a way to put in an escape for the user? Right now if I run the macro and hit cancel in the Save As dialog box it saves a txt file with the name False.
Is there a way to just have it cancel and go back to the workbook?

Code:
Sub TextFileSave()
    Dim newFile As String, fName As String, gName, fileSaveName
    gName = "FGM_"
    fName = Range("A2").Value
    newFile = gName & " " & fName & " " & Format$(Date, "dd-mm-yyyy")
    fileSaveName = Application.GetSaveAsFilename(newFile, "Text (MS-DOS) (*.txt), *.txt")
    ThisWorkbook.SaveAs fileSaveName, xlTextMSDOS
End Sub
 
Upvote 0
I found the answer on another post.
Thanks for all the help!

If fileSaveName <> False Then ThisWorkbook.SaveAs fileSaveName, xlTextMSDOS
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,453
Members
449,161
Latest member
NHOJ

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