Skip to main content

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)
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 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

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

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


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!
Comments (2)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi,


this doesn't actually work


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


Please fix it
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.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations