Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

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()
'Update 20150628
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)
Application.ActiveSheet.Copy
Application.ActiveSheet.Cells.Clear
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 60 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 Enterprise > Import / Export > Export Range to File, see screenshot:

doc export range to csv 3 3

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

Notes:

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 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!


Excel Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    D Pritchard · 6 months ago
    Hi,


    this doesn't actually work


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


    Please fix it
  • To post as a guest, your comment is unpublished.
    Bill James · 1 years ago
    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.