Save to Desktop as a text file vba

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, Is it possible to save the Active Sheet data to the desktop as a text file using the Active Workbook Name and the file Extension .asc instead of .txt.
I need to save it as tab delimited if the workbook name starts with the Letter A if not then save as comma delimited.
Also If this can't be done Is it possible to change the extension once saved?. Many Thanks
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
.
Here are two macros to get you started :

Code:
Sub textSave()
Dim ws As Worksheet, wsName As String
    Application.ScreenUpdating = False
    For Each ws In ThisWorkbook.Worksheets
        wsName = ws.Name
        ws.Copy
        ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & wsName & ".txt", FileFormat:=xlTextMSDOS, CreateBackup:=False
        ActiveWorkbook.Close False
    Next ws
    Application.ScreenUpdating = True
End Sub


Code:
Sub Export_Sheets_To_Text_Files()
    Dim Ws          As Worksheet 
    Dim strFile     As String 
     
     
    With Application 
        .ScreenUpdating = False 
        For Each Ws In .ActiveWorkbook.Worksheets 
            Ws.Copy 
            strFile = ThisWorkbook.Path & "\" & Ws.Name & ".txt" 
            .ActiveWorkbook.SaveAs Filename:=strFile, FileFormat:=xlText 
            .ActiveWorkbook.Saved = True 
            .ActiveWorkbook.Close 
        Next Ws 
        .ScreenUpdating = True 
    End With 

    MsgBox "Done...", 64 
End Sub
 
Last edited:
Upvote 0
This little script will ask you where to save it and will use Excel file format to export as a csv. I have added an extra line for you to try outputting it to asc - it worked on my machine but I don't know if its in the correct format for you?

it copies the activesheet and

I used the code to ask for a file location by vaskov17 via the post:

https://www.mrexcel.com/forum/excel...apture-file-path-post3169593.html#post3169593

Code:
Sub test()
Dim TempWB As Workbook
   
ActiveSheet.Copy
Set TempWB = ActiveWorkbook

Dim fd As FileDialog
    Dim sPath As String
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    
    If fd.Show = -1 Then
        sPath = fd.SelectedItems(1)
    End If
[COLOR="#008000"]    'sPath now holds the path to the folder or nothing if the user clicked the cancel button[/COLOR]
  
With TempWB
    .SaveAs Filename:=sPath & "\CSV-Exported", FileFormat:=xlCSVWindows, CreateBackup:=False
    
[COLOR="#008000"]    'use below to try your asc format to see if it saves ok for you
    'SaveAs Filename:=sPath & "\CSV-Exported.asc",  CreateBackup:=False[/COLOR]
    .Close
    End With

End Sub
 
Upvote 0
Thank you for your codes, however I have put together a simple macro that does the job

Code:
Sub SavetoDesktop()
' Saves the Activesheet to the Desktop as a text file with a different extension
    Dim strPath As String
    ActiveSheet.Copy
    strPath = Environ("HomeDrive") & Environ("HomePath") & "\Desktop\" & ActiveSheet.Name & ".asc"
    ActiveWorkbook.SaveAs filename:=strPath, FileFormat:=xlText
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,205
Members
452,618
Latest member
Tam84

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