VBA Convert .CSV to .Xlsx

rplohocky

Active Member
Joined
Sep 25, 2005
Messages
292
Office Version
  1. 365
Platform
  1. Windows
Hello,
I found a snippet of code that will loop through a folder and do a specific function (i.e. insert a formula). I am looking for one of the functions to be changing the extension from CSV to xlsx without creating a new workbook. Can any one share the line of code that will perform this action?

Thank you.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I am looking for one of the functions to be changing the extension from CSV to xlsx without creating a new workbook.
Note that changing the extension on a file does NOT actually convert the file.
So the only time I could understand doing something like this is if the files were already actually Excel files, and were incorrectly assigned a CSV extension.
If you want to actually convert the file from a CSV file to an Excel file, you would need to open it as do a SaveAs and change the file type.
Otherwise, if you just change the extension, you will probably get errors when you try to open the file, saying the format or extension is not valid.
 
Upvote 0
Note that changing the extension on a file does NOT actually convert the file.
So the only time I could understand doing something like this is if the files were already actually Excel files, and were incorrectly assigned a CSV extension.
If you want to actually convert the file from a CSV file to an Excel file, you would need to open it as do a SaveAs and change the file type.
Otherwise, if you just change the extension, you will probably get errors when you try to open the file, saying the format or extension is not valid.

Hello Joe4,
You are right. I have a folder full of CSV workbooks that need to be converted to XLSX. I know you need to open the workbook then do a savas but then the folder has duplicated workbooks. I'm trying to figure out a way for my macro to take the original workbook and save it as xlsx without keeping the csv version. Your right changing the extension does cause a warning when opened. Any way to accomplish this or do I need to make the macro save it to a new folder?
 
Upvote 0
This VBA code will convert all CSV files in a folder to XLSX files and delete the original CSV:
Code:
Sub ConvertCSVToXlsx()
    
    Dim myfile As String
    Dim oldfname As String, newfname As String
    Dim workfile
    Dim folderName As String
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
'   Capture name of current file
    myfile = ActiveWorkbook.Name
    
'   Set folder name to work through
    folderName = "C:\Test\"
    
'   Loop through all CSV filres in folder
    workfile = Dir(folderName & "*.CSV")
    Do While workfile <> ""
'       Open CSV file
        Workbooks.Open Filename:=folderName & workfile
'       Capture name of old CSV file
        oldfname = ActiveWorkbook.FullName
'       Convert to XLSX
        newfname = folderName & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & ".xlsx"
        ActiveWorkbook.SaveAs Filename:=newfname, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Close
'       Delete old CSV file
        Kill oldfname
        Windows(myfile).Activate
        workfile = Dir()
    Loop
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
This VBA code will convert all CSV files in a folder to XLSX files and delete the original CSV:
Code:
Sub ConvertCSVToXlsx()
    
    Dim myfile As String
    Dim oldfname As String, newfname As String
    Dim workfile
    Dim folderName As String
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
'   Capture name of current file
    myfile = ActiveWorkbook.Name
    
'   Set folder name to work through
    folderName = "C:\Test\"
    
'   Loop through all CSV filres in folder
    workfile = Dir(folderName & "*.CSV")
    Do While workfile <> ""
'       Open CSV file
        Workbooks.Open Filename:=folderName & workfile
'       Capture name of old CSV file
        oldfname = ActiveWorkbook.FullName
'       Convert to XLSX
        newfname = folderName & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & ".xlsx"
        ActiveWorkbook.SaveAs Filename:=newfname, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Close
'       Delete old CSV file
        Kill oldfname
        Windows(myfile).Activate
        workfile = Dir()
    Loop
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub

Thanks Joe4 this works great! I really appreciate it!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top