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
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Trevor G

Well-known Member
Joined
Jul 17, 2008
Messages
6,362
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
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
52,098
Office Version
365
Platform
Windows
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]
 

Forum statistics

Threads
1,089,559
Messages
5,408,958
Members
403,246
Latest member
NateD1

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top