This is working for me, but the file extension is not changing. Any tips?
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
To convert a CSV file to XlS or XLSX file is very easy for you by applying the Save As feature. However, to convert multiple CSV files to XLS or XLSX files from a folder is time-consuming by saving one by one manually. Here I introduce a macro code to quickly batch convert all CSV files to XLS(x) files from a folder.
Batch convert CSV files to XlS(X) files with macro code
To convert multiple CSV files from one folder to XLS(X) files, you can do as below steps:
1. Enable a new workbook, press Alt + F11 keys to open Microsoft Visual Basic for Applications window, and click Insert > Module. See screenshot:
Note: Make sure all CSV files you want to convert are closed.
2. Then paste below macro code to the Module script, and press F5 key to run the code.
VBA: Convert CSV to XLS
Sub CSVtoXLS() 'UpdatebyExtendoffice20170814 Dim xFd As FileDialog Dim xSPath As String Dim xCSVFile As String Dim xWsheet As String Application.DisplayAlerts = False Application.StatusBar = True xWsheet = ActiveWorkbook.Name Set xFd = Application.FileDialog(msoFileDialogFolderPicker) xFd.Title = "Select a folder:" If xFd.Show = -1 Then xSPath = xFd.SelectedItems(1) Else Exit Sub End If If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\" xCSVFile = Dir(xSPath & "*.csv") Do While xCSVFile <> "" Application.StatusBar = "Converting: " & xCSVFile Workbooks.Open Filename:=xSPath & xCSVFile ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xls", vbTextCompare), xlNormal ActiveWorkbook.Close Windows(xWsheet).Activate xCSVFile = Dir Loop Application.StatusBar = False Application.DisplayAlerts = True End Sub
3. In the popping out dialog, select the specified folder containing the CSV files you want to convert. See screenshot:
4. Click OK, all the CSV files in the selected folder have been converted to XLS files in it.
Tip: If you want to convert CSV files to XLSX files, you use below VBA code.
VBA: Convert CSV files to XLSX
Sub CSVtoXLS() 'UpdatebyExtendoffice20170814 Dim xFd As FileDialog Dim xSPath As String Dim xCSVFile As String Dim xWsheet As String Application.DisplayAlerts = False Application.StatusBar = True xWsheet = ActiveWorkbook.Name Set xFd = Application.FileDialog(msoFileDialogFolderPicker) xFd.Title = "Select a folder:" If xFd.Show = -1 Then xSPath = xFd.SelectedItems(1) Else Exit Sub End If If Right(xSPath, 1) <> "\" Then xSPath = xSPath + "\" xCSVFile = Dir(xSPath & "*.csv") Do While xCSVFile <> "" Application.StatusBar = "Converting: " & xCSVFile Workbooks.Open Filename:=xSPath & xCSVFile ActiveWorkbook.SaveAs Replace(xSPath & xCSVFile, ".csv", ".xlsx", vbTextCompare), xlWorkbookDefault ActiveWorkbook.Close Windows(xWsheet).Activate xCSVFile = Dir Loop Application.StatusBar = False Application.DisplayAlerts = True End Sub
quickly convert or export a range of a sheet to separate XLS/Word/PDF or other format files in once time
|
Normally, Excel does not support you with an option to quickly export or save a range as a CSV or Excel file. If you want to save a range of data as a CSV or workbook in Excel, you may need to use a VBA Macro for doing this or to copy the range to clipboard and paste it in a new workbook and then save the workbook as CSV or Workbook. Kutools for Excel augments Excel with Export Range to File utility for Excel users who want to quickly process the following operations: Click for 30-day full featured free trial! |
![]() |
Kutools for Excel: with more than 300 handy Excel add-ins, free to try with no limitation in 30 days. |