Copy worksheet and save to desktop as a csv file

Daddylouc

New Member
Joined
Nov 28, 2012
Messages
9
I have this code to copy an existing worksheet to my desktop as an xlsx file but I need to save it as a csv file. I tried just changing the named extension but that didn't work. How would I modify this code to save the file as a csv?

Sub Expo()


Application.ScreenUpdating = False


'Get path for desktop of user PC
Path = Environ("USERPROFILE") & "\Desktop"
Sheet1.Cells.Copy


'Create new workbook and past copied data in new workbook & save to desktop
Workbooks.Add (xlWBATWorksheet)
ActiveWorkbook.ActiveSheet.Paste
ActiveWorkbook.ActiveSheet.Name = "report"
ActiveWorkbook.SaveAs Filename:=Path & "" & "report " & Format(CStr(Now()), "dd-mmm (hh.mm.ss AM/PM)") & ".xlsx"
ActiveWorkbook.Close SaveChanges:=True


Application.ScreenUpdating = True


MsgBox "Exported to Desktop"


End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I think you need to add in the file format as well. See basic example below of recorded macro to save workbook as csv.

Sub Macro1()
ChDir "C:\Desktop"
ActiveWorkbook.SaveAs Filename:="C:\Desktop\rubbish.csv", _
FileFormat:=xlCSV, CreateBackup:=False
End Sub
 
Upvote 0
It is more than just changing the file extension (that is just a name). You also have to change the file type. Otherwise, it will still save as an Excel file.
So you need to add the FileFormat argument to your SaveAs function, i.e.
Code:
[COLOR=#333333]ActiveWorkbook.SaveAs Filename:=Path & "\" & "report " & Format(CStr(Now()), "dd-mmm (hh.mm.ss AM/PM)") & [/COLOR][COLOR=#ff0000]".csv", FileFormat:=xlCSV[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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