How to batch convert multiple Excel files to CSV files in Excel?
In Excel, we can convert the workbook to CSV file with the Save as function, but could you know how to batch convert multiple Excel files to separate CSV files? In this article, I introduce a VBA code to batch convert all Excel files in a folder to CSV files in Excel.
In Excel, there is no built-in function can solve this job quickly except VBA.
1. Enable Excel, and press Alt + F11 keys open Microsoft Visual Basic for Applications window.
2. Click Insert > Module to create a new Module.
3. Copy below code and paste them to the new Module window.
VBA: Batch convert Excel files to CSV
Sub WorkbooksSaveAsCsvToFolder() 'UpdatebyExtendoffice20181031 Dim xObjWB As Workbook Dim xObjWS As Worksheet Dim xStrEFPath As String Dim xStrEFFile As String Dim xObjFD As FileDialog Dim xObjSFD As FileDialog Dim xStrSPath As String Dim xStrCSVFName As String Application.ScreenUpdating = False Application.EnableEvents = False Application.Calculation = xlCalculationManual On Error Resume Next Set xObjFD = Application.FileDialog(msoFileDialogFolderPicker) xObjFD.AllowMultiSelect = False xObjFD.Title = "Kutools for Excel - Select a folder which contains Excel files" If xObjFD.Show <> -1 Then Exit Sub xStrEFPath = xObjFD.SelectedItems(1) & "\" Set xObjSFD = Application.FileDialog(msoFileDialogFolderPicker) xObjSFD.AllowMultiSelect = False xObjSFD.Title = "Kutools for Excel - Select a folder to locate CSV files" If xObjSFD.Show <> -1 Then Exit Sub xStrSPath = xObjSFD.SelectedItems(1) & "\" xStrEFFile = Dir(xStrEFPath & "*.xls*") Do While xStrEFFile <> "" Set xObjWB = Workbooks.Open(Filename:=xStrEFPath & xStrEFFile) xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".") - 1) & ".csv" xObjWB.SaveAs Filename:=xStrCSVFName, FileFormat:=xlCSV xObjWB.Close savechanges:=False xStrEFFile = Dir Loop Application.Calculation = xlCalculationAutomatic Application.EnableEvents = True Application.ScreenUpdating = True End Sub
4. Press F5 key, select the folder contains the Excel files you want to convert to CSV files in first popping dialog.
5. Click OK, then in the second popping dialog, select the folder to place the CSV files.
6. Click OK, now the Excel files in the folder have been converted to CSV files and saved in another folder.
As we known, we only can convert the whole workbook into one CSV file in Excel with its Save As function. But in some times, you want to convert the single sheet into CSV file, in this case, the Split Workbook utility of Kutools for Excel can help you.
|Kutools for Excel, with more than 300 handy functions, makes your jobs more easier.|
After installing Kutools for Excel, please do as below:（Free Download Kutools for Excel Now!)
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
- To post as a guest, your comment is unpublished.· 5 months agoI think it is worth adding better error handling for files with special characters, currently they are simply ignored.
- To post as a guest, your comment is unpublished.· 5 months agoThank you for sharing. I'm trying to save out multiple xls files which contain a unique value, producing a prompt asking yes or no before saving. The prompt reads..
"Some features in your workbook might be lost if you save it as a CSV (Comma delimited). Do you want to keep using that format?"
Would someone know where to add the code to answer yes to this prompt?
- To post as a guest, your comment is unpublished.· 1 years agoAnother small remark:
If the cells in the original Excel files are all formatted as "General", some accuracy is lost when the file is saved as a CSV
For example, if a cell value in Excel is 0.123456789123456, then the value in the CSV will be 0.123456789 (missing the remaining decimals), as long as the cell was formatted as 'General'. This can be solved by formatting all cells in the Excel file to anything else than 'General' (for example, 'Text'). In that case, the CSV *will* still have the full detail/accuracy. I.e. the values in the Excel files will be fully intact after saving as a CSV.
How could this macro be changes, so it sets the formatting of all cells in the Excel file to 'Text', before saving as a CSV?
I imagine that it must somehow make use of the following, but I can't figure out how to correctly include in within the macro:
Selection.NumberFormat = "@"
- To post as a guest, your comment is unpublished.· 1 years agoWorks great, thanks for the code!
My only remark would be that this code cuts of file names when there is a "." in the filename itself (e.g. file.123.csv turns into file.csv).
- To post as a guest, your comment is unpublished.· 7 months agoHave you found a way around this issue?
- To post as a guest, your comment is unpublished.· 7 months agoCarol,
On line 33 I've replaced this code:
xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".") - 1) & ".csv"
With this code:
xStrCSVFName = xStrSPath & Left(xStrEFFile, InStr(1, xStrEFFile, ".xlsx") - 1) & ".csv"
Note that if you're using some other excel extension (.xls, .xlsm, etc.) you should change it as such :)
- To post as a guest, your comment is unpublished.· 6 months agoThank you so much! This has saved me so much time!!
- To post as a guest, your comment is unpublished.· 1 years agoTy it really works dear !!