(Solved) – vba code help to convert xlsx files to csv code

  • by

Could you please help me with my script. I have tried this script. The only problem is that I want the old files to be there in the folder and my new name on csv file to be exact as xlsx file (I am getting . extra on the csv extension like filename..csv). Can someone help me please??
Thanks !!!

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 & “*.xlsx”)
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) & “.CSV”
ActiveWorkbook.SaveAs Filename:=newfname, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
‘ Delete old CSV file
Kill oldfname
workfile = Dir()

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *