this doesn't actually work
It still exports the entire sheet even when a range is selected.
Please fix it
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.
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.
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:
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.
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:
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.
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.