Excel VBA to Convert CSV File to XLSX (2 Easy Examples)

We will use a CSV file that contains information about the Name, ID, and Date of Birth of some employees. We will convert this CSV file to an XLSX file.


Method 1 – Use the Command Button to Convert a CSV File to an XLSX File with Excel VBA

Steps:

  • Go to the Developer tab and select Design Mode.

Use Command Button to Convert CSV File to XLSX File with Excel VBA

  • Select Insert and pick the Command Button icon from the ActiveX Controls.

Use Command Button to Convert CSV File to XLSX File with Excel VBA

  • Draw the command button on the active sheet.

Use Command Button to Convert CSV File to XLSX File with Excel VBA

  • Double-click on the command button to open the Code window.
  • Insert this code in the Code window:
Private Sub CommandButton1_Click()
Dim x As FileDialog
Dim xPath As String
Dim CSVfile As String
Dim Wks As String
Application.DisplayAlerts = False
Application.StatusBar = True
Wks = ActiveWorkbook.Name
Set x = Application.FileDialog(msoFileDialogFolderPicker)
x.Title = "Select a folder:"
If x.Show = -1 Then
xPath = x.SelectedItems(1)
Else
Exit Sub
End If
If Right(xPath, 1) <> "\" Then xPath = xPath + "\"
CSVfile = Dir(xPath & "*.csv")
Do While CSVfile <> ""
Application.StatusBar = "Converting: " & CSVfile
Workbooks.Open Filename:=xPath & CSVfile
ActiveWorkbook.SaveAs Replace(xPath & CSVfile, ".csv", ".xlsx", vbTextCompare), _
xlWorkbookDefault
ActiveWorkbook.Close
Windows(Wks).Activate
CSVfile = Dir
Loop
Application.StatusBar = False
Application.DisplayAlerts = True
End Sub

Use Command Button to Convert CSV File to XLSX File with Excel VBA

  • Press Ctrl + S to save the code and close the Code window.
  • Deselect the Design Mode in the Developer tab.

Use Command Button to Convert CSV File to XLSX File with Excel VBA

  • Click on the command button. It will open a dialog box.
  • Select the folder that contains the CSV file and press OK.

Use Command Button to Convert CSV File to XLSX File with Excel VBA

  • The CSV file will open in Excel.

Use Command Button to Convert CSV File to XLSX File with Excel VBA

  • Go to the File tab and select Save As.
  • Change the file type to Excel Workbook (*.xlsx) and save it.

Use Command Button to Convert CSV File to XLSX File with Excel VBA

  • You will see the converted XLSX file in the same folder.

Use Command Button to Convert CSV File to XLSX File with Excel VBA


Method 2 – Apply VBA to Transform a CSV File to an XLSX File in Excel

Steps:

  • Go to the Developer tab and select Visual Basic. It will open the Visual Basic window.

Apply VBA to Transform CSV File to XLSX File in Excel

  • Select Insert and choose Module. The Module window will appear.

Apply VBA to Transform CSV File to XLSX File in Excel

  • Insert this code in the Module window:
Sub Convert_CSV_to_XLSX_File()
Dim w As Workbook
Set w = Workbooks.Open("D:\exceldemy\New folder (45)\Employee_info.csv")
w.SaveAs Filename:="D:\exceldemy\New folder (45)\Employee_info.xlsx", _
FileFormat:=xlWorkbookDefault, _
ReadOnlyRecommended:=False, CreateBackup:=False
End Sub

Apply VBA to Transform CSV File to XLSX File in Excel

  • Replace the directories (“D:\exceldemy\New folder (45)\Employee_info.csv”) and (“D:\exceldemy\New folder (45)\Employee_info.xlsx”) with your desired one. Write the name of the file you want to convert carefully.
  • Press Ctrl + S to save the code.
  • Press the F5 key to run the code.

Apply VBA to Transform CSV File to XLSX File in Excel

  • You will see the converted XLSX file in the desired location.

Read More: Excel VBA to Read CSV File into Array


Alternative – Convert a CSV File to an XLSX File without Excel VBA

Steps:

  • Open the CSV file in Excel.
  • Go to the File tab.

  • Select Save As.

  • Change the Save As type to Excel Workbook (*.xlsx) and save it.

  • You will see the XLSX file in the desired location.


Things to Remember

  • You must write the directory and file name correctly in the VBA code.
  • Also, be extra careful while writing the extensions.
  • Use Method-2 to convert a single CSV file.
  • You can convert multiple CSV files using Method-1.

Download the Practice Book=


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Mursalin Ibne Salehin
Mursalin Ibne Salehin

Mursalin Ibne Salehin holds a BSc in Electrical and Electronics Engineering from Bangladesh University of Engineering and Technology. Over the past 2 years, he has actively contributed to the ExcelDemy project, where he authored over 150 articles. He has also led a team with content development works. Currently, he is working as a Reviewer in the ExcelDemy Project. He likes using and learning about Microsoft Office, especially Excel. He is interested in data analysis with Excel, machine learning,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo