Note: The other languages of the website are Google-translated. Back to English

How to export range of cells in Excel to csv file?

In Excel, we can save the whole worksheet as csv file by using the Save As function, but, have you ever tried to export a range of cells from one worksheet to a csv file? In this article, you will get the methods to solve this problem in Excel.

Export range of cells in Excel to csv file with VBA code

Export range of cells in Excel to csv file with Kutools for Excel

Export range of cells in Excel to csv file with VBA code

Here, I have a VBA code to export a range of cells from Excel to csv file, please do as follows:

1. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Export a range of cell contents to csv file

Sub ExportRangetoFile()
'Updateby Extendoffice
Dim Rng As Range
Dim WorkRng As Range
Dim xFile As Variant
Dim xFileString As String
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
WorkRng.Copy Application.ActiveSheet.Range("A1")
Set xFile = CreateObject("Scripting.FileSystemObject")
xFileString = Application.GetSaveAsFilename("", filefilter:="Comma Separated Text (*.CSV), *.CSV")
Application.ActiveWorkbook.SaveAs Filename:=xFileString, FileFormat:=xlCSV, CreateBackup:=False
End Sub

3. Then press F5 key to run this code, and a prompt box will pop out to remind you selecting the range of cells that you want to export as csv file.

doc export range to csv 1

4. And then click OK button, specify a directory for putting the new csv file, and give a name for the csv file in the File name text box, see screenshot:

doc export range to csv 2

5. Then click Save button, and the selected range has been saved as csv file, you can go to your specified folder to view it.

Export range of cells in Excel to csv file with Kutools for Excel

If you are not interested in the VBA code, here, I can introduce you an easy tool – Kutools for Excel, with its Export Range to File feature, you can quickly solve this job.

Kutools for Excel : with more than 300 handy Excel add-ins, free to try with no limitation in 30 days. 

After installing Kutools for Excel, please do with following steps:

1. Select the data range that you want to export to csv file.

2. Then click Kutools Plus > Import & Export > Export Range to File, see screenshot:

3. In the Export Range to File dialog box, choose CSV(Comma delimited) from the File format option, and then specify the Save directory to save the new csv file, see screenshot:

doc export range to csv 4 4

4. Then click Ok button, a prompt box will pop out to remind you specify a name for this new csv file as you need, and then click OK to close this box, and the selected data will be exported as a CSV file at once.

doc export range to csv 5 5


Save the actual value: It will display the actual values in the final file.

Save values as shown on-screen: It will display the values in the final file as the same you read them on the computer screen.

Open the file after export: It will automatically open the final file after exporting the range if you check this option.

Click to know more about this Export Range to File utility.

Download and free trial Kutools for Excel Now !

Demo: Export range of cells to csv / pdf / txt / html file with Kutools for Excel

Kutools for Excel: with more than 300 handy Excel add-ins, free to try with no limitation in 30 days. Download and free trial Now!

The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Comments (2)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Thank you for this macro, it is very helpful.

Do you know why on some existing large sheets I get extra rows appended? I am selecting the header row cells along with a few data row cells which might be 1000's of rows down in the sheet. These appended rows show up in the output .csv file with a comma for each column in the source selection. If I manually create a small sheet such as your example this does not happen.
This comment was minimized by the moderator on the site

this doesn't actually work

It still exports the entire sheet even when a range is selected.

Please fix it
There are no comments posted here yet
Leave your comments
Posting as Guest
Rate this post:
0   Characters
Suggested Locations