{"id":14228,"date":"2017-09-02T13:35:55","date_gmt":"2017-09-02T13:35:55","guid":{"rendered":"http:\/\/learnexcelmacro.com\/wp\/?p=14228"},"modified":"2022-08-09T19:35:46","modified_gmt":"2022-08-09T19:35:46","slug":"save-excel-range-data-as-csv-file-through-excel-vba","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2017\/09\/save-excel-range-data-as-csv-file-through-excel-vba\/","title":{"rendered":"Top 4 VBA Methods to Export Excel data as CSV File"},"content":{"rendered":"
In this article, I am going to teach you 4 VBA Methods of – how to export data from Excel Range to a CSV file format using Excel VBA. <\/em><\/strong><\/p>\n CSV<\/strong> is an abbreviation of C<\/strong>omma S<\/strong>eparated V<\/strong>alue. As the name suggests, it is clear that this file stores the tabular data in a format where data is separated by comma. Important Note:<\/strong> Since comma is used as delimiter in CSV file – so what if your data itself has comma (,) as a value?<\/strong> To overcome this issue, CSV file format, stores such values within double quotes (” “) and then separated by comma(,). In this article, following are the methods which I am using to Export Excel data i CSV format.<\/p>\n Before we go in to details, I would like to recommend you guys to go through following tutorials<\/a> – this will help you in understanding the code better – <\/a><\/p>\n 1. This is a very simple and quickest method to export your Excel data to a CSV file. At the same time, this method has some short comings or challenges as well.<\/p>\n 1. In this method, data from ActiveSheet is saved as CSV file only. It ignores rest other sheets and its data. This is the best option, when your excel sheet has, the only data which you want to export it as a CSV file. That means it does not have any other data which you want to ignore while exporting it to csv.<\/p>\n This method is simply using the SaveAs feature of ActiveSheet to CSV format. Rest is self explanatory.<\/p>\n <\/a><\/p>\n This method overcomes both the challenges of the first Method. 1. The only shortcoming with this method, as compared to first method, is it has few more lines of code and execution time will be more because you are reading data for each row and column and putting them together in CSV file – separating them by comma.<\/p>\n 1. When your data is scattered In above code, I am doing the following: \u00a0 Did you know?<\/i><\/h2>\n
\nInterestingly, CSV is also a plain text file type. Here each line represents a row and each value separated by comma, resides in columns.<\/p>\n<\/div>\n
\nLet’s get started then…<\/strong><\/p>\nMethods of Exporting Excel data to CSV Files using VBA<\/h1>\n
<\/i>\u00a0 1. Export ActiveWorkSheet as CSV file<\/a><\/h2>\n
<\/i>\u00a0 2. VBA to export Excel Range to CSV File [Method 1]<\/a><\/h2>\n
<\/i>\u00a0 3. VBA to export Excel Range to CSV File [Method 2]<\/a><\/h2>\n
<\/i>\u00a0 4. VBA to Export Excel Table as CSV File<\/a><\/h2>\n<\/div>\n<\/div>\n
\nIn following tutorial<\/a> about interaction with text files through Excel VBA, we have talked a lot about creating new text files, exporting data from an Excel Range to Text file and so many other different topics.
\nVBA Guide to Interact with Text Files \u2013 Part \u2013 1<\/a>
\nVBA Guide to Interact with Text Files \u2013 Part \u2013 2 <\/a><\/p>\n1. Export ActiveWorkSheet as CSV file<\/h1>\n
Advantages of this method<\/h2>\n
\n2. No extra coding required in order to maintain the comma delimiter or double quotes etc. Excel does it by itself.<\/p>\nDrawbacks of this Method<\/h2>\n
\n2. You do not have control over data – which one to be exported or ignored. It will export every single data from the sheet to CSV format.
\nFor example: If you have some blank rows at the beginning of the sheet etc., which you do not want to save it in CSV, it is not possible to ignore them. It will still save those lines as blank values in the CSV.<\/p>\nBest case when it should be used?<\/h2>\n
VBA Code<\/h2>\n
\r\nSub saveSheetToCSV()\r\n \r\n Dim myCSVFileName As String\r\n Dim tempWB As Workbook\r\n \r\n Application.DisplayAlerts = False\r\n On Error GoTo err\r\n \r\n myCSVFileName = ThisWorkbook.Path & \"\\\" & \"CSV-Exported-File-\" & VBA.Format(VBA.Now, \"dd-MMM-yyyy hh-mm\") & \".csv\"\r\n\r\n ThisWorkbook.Sheets(\"YourSheetToCopy\").Activate\r\n ActiveSheet.Copy\r\n Set tempWB = ActiveWorkbook\r\n \r\n With tempWB\r\n .SaveAs Filename:=myCSVFileName, FileFormat:=xlCSV, CreateBackup:=False\r\n .Close\r\n End With\r\nerr:\r\n Application.DisplayAlerts = True\r\nEnd Sub\r\n<\/code><\/pre>\n
Explanation of the Code<\/h2>\n
2. VBA to Export Specific Range to CSV – Method 1<\/h1>\n
Advantages of this method<\/h2>\n
\n1. Here you have full control over which all data you want to be part of your CSV file.
\n2. You can read data from random places and even from different sheets as well.
\n3. You can use your own delimiter – For example: <\/strong> instead of comma, you may use semicolon(;)<\/p>\nDrawbacks of this Method<\/h2>\n
Best case when it should be used?<\/h2>\n
\n2. You want to have control over data (Format check, some transformation logic etc.)<\/p>\nVBA Codes<\/h2>\n
\r\nSub exportRangeToCSVFile()\r\n \r\n Dim myCSVFileName As String\r\n Dim myWB As Workbook\r\n Dim rngToSave As Range\r\n Dim fNum As Integer\r\n Dim csvVal As String\r\n \r\n Set myWB = ThisWorkbook\r\n myCSVFileName = myWB.Path & \"\\\" & \"CSV-Exported-File-\" & VBA.Format(VBA.Now, \"dd-MMM-yyyy hh-mm\") & \".csv\"\r\n csvVal = \"\"\r\n fNum = FreeFile\r\n Set rngToSave = Range(\"B2:H30\")\r\n \r\n Open myCSVFileName For Output As #fNum\r\n \r\n For i = 1 To rngToSave.Rows.Count\r\n For j = 1 To rngToSave.Columns.Count\r\n csvVal = csvVal & Chr(34) & rngToSave(i, j).Value & Chr(34) & \",\"\r\n Next\r\n Print #fNum, Left(csvVal, Len(csvVal) - 2)\r\n csvVal = \"\"\r\n Next\r\n \r\n Close #fileNumber\r\nEnd Sub\r\n<\/code><\/pre>\n
Explanation of above Code<\/h2>\n
\n1. It is a simple for loop, using which I am concatenating each row and columns data separated by comma (,)
\n2. Print each rows data in csv file.
\n3. That’s all… your csv file is ready to use<\/p>\n