How to Save an Excel File (.xlsx) or Google Sheet as CSV
Written by
Reviewed by
This tutorial demonstrates how to save an Excel file (.xlsx) or Google Sheets as a CSV file.
Save an Excel File as CSV
Excel files and CSV files have similarities and differences. There are several reasons you might want to convert an Excel file.
- In the Ribbon, go to File > Save a Copy (or Save as).
- In the new window, on the right side, choose the CSV (Comma delimited (*.csv)) file format and click Save.
Tip: If you have special or foreign-language characters in the file, the best solution is to use UTF-8 for the CSV file. This way, special characters from Excel remain the same in CSV format.
As a result, the CSV file is saved in the same folder as the Excel file.
Save Excel Range as CSV
This code will export a range to CSV.
Use the CallExport Sub to define the range to export and the name of the text file that you’re saving to:
Sub CallExport()
'ExportRange(range,where,delimiter)
Call ExportRange(Sheet1.Range("A1:C20"), "C:mark.txt", ",")
End Sub
You’ll need to add this function to your code module as well:
Function ExportRange(WhatRange As Range, _
Where As String, Delimiter As String) As String
Dim HoldRow As Long 'test for new row variable
HoldRow = WhatRange.Row
Dim c As Range
'loop through range variable
For Each c In WhatRange
If HoldRow <> c.Row Then
'add linebreak and remove extra delimeter
ExportRange = Left(ExportRange, Len(ExportRange) - 1) _
& vbCrLf & c.Text & Delimiter
HoldRow = c.Row
Else
ExportRange = ExportRange & c.Text & Delimiter
End If
Next c
'Trim extra delimiter
ExportRange = Left(ExportRange, Len(ExportRange) - 1)
'Kill the file if it already exists
If Len(Dir(Where)) > 0 Then
Kill Where
End If
Open Where For Append As #1 'write the new file
Print #1, ExportRange
Close #1
End Function
Save a Google Sheets File as CSV
You can also save a Google Sheets file in CSV format. In the Menu, go to File > Download > Comma-separated values (.csv, current sheet).
As a result, a new CSV file is downloaded with data from the current Google sheet.