We at times are required to convert excel files into csv to read as flat files and sometime require to convert a csv file into excel file to use excel features on the data.
Below function shows how to convert an csv file into excel file and vice versa. We can also convert to other formats based on constants
Here constant value 23 is used to create a csv file and constant -4143 to save a file as xls file.Once the destination file is created, we can delete the source file as shown below. In case of any issue in understanding the code, please add in comment section
Call func_ConversionCSVExcel("E:\Test.csv", "E:\Test_converted.xls", "csvtoexcel")
Public Function func_ConversionCSVExcel(strSrcFile, strDestFile, Conversion)
on error resume next
Set objExcel = CreateObject("Excel.application")
set objExcelBook = objExcel.Workbooks.Open(strSrcFile)
objExcel.application.visible=false
objExcel.application.displayalerts=false
If(Conversion = "ExceltoCSV") Then
objExcelBook.SaveAs strDestFile, 23
else
objExcel.ActiveWorkbook.SaveAs strDestFile,-4143
End If
objExcel.Application.Quit
objExcel.Quit
Set objExcel = Nothing
set objExcelBook = Nothing
Set objFSO = CreateObject("scripting.FileSystemObject")
objFSO.DeleteFile(strSrcFile)
Set objFSO =nothing
End Function