How to batch convert multiple CSV files to XLS(X) files in Excel?
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
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
Convert or Export a Range to Separate XLS, Word, PDF, and More |
Typically, Excel doesn't offer a quick option to export or save a selected range directly as a CSV, Excel, or other file types. To achieve this, you would need to either use a VBA macro or manually copy the range to a new workbook before saving. Kutools for Excel simplifies this process with its Export Range to File utility, allowing you to quickly and easily handle these operations in just a few clicks. |
Kutools for Excel - Supercharge Excel with over 300 essential tools. Enjoy permanently free AI features! Get It Now |
Related Articles:
- How to convert csv file to xls file or import/open csv in Excel?
- How to convert word document to excel worksheet?
Best Office Productivity Tools
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...
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!