Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Need help writing macro to save excel file as .CSV?

I am trying to write a macro that will save a workbook as a .csv file. I would also like the user to be able to pick the path in which to save the file.

Currently I have the following code, but I keep getting an error.

Any help is appreciated!

--------------------

Sub SaveAsCsv()

Dim strFileName As String

strFileName = Application.GetSaveAsFilename(filefilter:="Comma Seperated Values (*.csv), *.csv")

If strFileName <> "False" Then

Sheets("Manual_Template").Copy

ActiveWorkbook.SaveAs strFileName, xlCSV

End If

End Sub

Update:

Every time it runs the macro it wants to save the file as "FileName.xlsm.csv" (includes quotations).

I want the file to Save as FileName.csv - no quotations and no .xlsm

1 Answer

Relevance
  • 8 years ago

    I dont really see anything but small problems with your code. The .getsaveasfilename method will return a boolean false if the user doesn't enter a filename, or presses cancel, so it would be better if you initialized strFileName as Variant. so the the code would become:

    Sub SaveAsCsv()

    Dim strFileName As Variant

    strFileName = Application.GetSaveAsFilename( filefilter:="Seperated Values (*.csv), *.csv")

    If strFileName <> False Then

    Sheets("Manual_Template").Activate

    ActiveWorkbook.SaveAs strFileName, xlCSV

    End If

    End Sub

Still have questions? Get your answers by asking now.