How to Convert a CSV File to XLSX Using VBA in Excel
Question details
The user needs a VBA macro script to automatically open a CSV export and save it as a standard XLSX workbook.

- Product
- Spreadsheet
- Device & OS
- not provided
- Scenario
- Automating the conversion of CSV data exports into the standard Excel format for use with Access databases or further data processing.
- Observed behavior
- The user wants to programmatically convert file formats using VBA instead of manually opening and saving each file.
Ensure the Developer tab is enabled in your spreadsheet program so you can access the Visual Basic for Applications (VBA) editor.
Use a VBA Macro to Open and Save As XLSX
Run a short VBA script to programmatically open the target CSV file and immediately save it in the XLSX format.
This method uses the Workbooks.Open method to load the CSV data, followed by the SaveAs method with the xlOpenXMLWorkbook format code to convert the file to XLSX.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
Click on 'Insert' in the top menu, then select 'Module' to create a new script window.
Enter the following code into the module: Sub ConvertCSV() Workbooks.Open "C:\Path\MyExport.csv" ActiveWorkbook.SaveAs "C:\Path\MyExport.xlsx", xlOpenXMLWorkbook End Sub
Press F5 or click the Run button to execute the code. Ensure you replace 'C:\Path\...' with the actual file paths on your computer.

Automate Your Tasks with WPS Spreadsheet
WPS Spreadsheet provides excellent compatibility with standard macros, allowing you to run VBA scripts to convert CSVs to XLSX effortlessly.
- 1. Open WPS Spreadsheet: Launch WPS Office and open a new blank spreadsheet.
- 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon and click on the VBA Editor icon.
- 3. Run the Conversion Script: Paste your CSV to XLSX conversion macro into a new module and execute it directly within WPS.

Frequently Asked Questions
Why do I get a run-time error when executing the macro?
This usually happens if the file path is incorrect, the specified folder doesn't exist, or the CSV file is already open in another program. Double-check your file paths in the code.
Can I convert multiple CSV files at once using VBA?
Yes, you can use the Dir function in your VBA script to create a loop that iterates through a specific folder, opening and converting all CSV files to XLSX automatically.
What is the FileFormat number for XLSX?
The FileFormat value for a standard XLSX file is 51, which is represented by the constant xlOpenXMLWorkbook in VBA.




