Convert Csv file to xlsx and delete that csv file

Sachin Singh
Sachin Singh used Ask the Experts™
on
Convert Csv file to xlsx and delete that csv file
Condition=
1)Both the files are located in same place  that is C:\Users\Sholtan\Desktop
2)the name of the csv file may differ
3)My vba code will be placed in Analyser.xlsb
1.csv
Analyser.xlsb
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Roy CoxGroup Finance Manager

Commented:
You can do this manually in seconds using the Get & Transform data" function from the Data Tab of the Ribbon
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
Place the following code in Analyser.xlsb file.
The code will convert all the csv files into excel files and delete the csv files from the folder where Analyser.xlsb is saved.

Sub ConvertCSVToXLSX()
Dim fso As Object
Dim FolderPath As String
Dim srcFolder As Object
Dim file As Object
Dim wb As Workbook
Dim FileName As String

Application.ScreenUpdating = False

Set fso = CreateObject("Scripting.FileSystemObject")

FolderPath = ThisWorkbook.Path
Set srcFolder = fso.GetFolder(FolderPath)

For Each file In srcFolder.Files
    If fso.GetExtensionName(file) = "csv" Then
        Set wb = Workbooks.Open(file)
        FileName = Left(file.Name, InStr(file.Name, ".") - 1)
        FileName = FileName & ".xlsx"
        wb.SaveAs FolderPath & "\" & FileName, 51
        wb.Close True
        fso.DeleteFile (file)
    End If
Next file
Application.ScreenUpdating = True
End Sub

Open in new window

Author

Commented:
This code is perfect but i missed something
i dont want the  empty rows between the data
Sorry Neeraj Sir  for the same
plz see the sample file i have highlighted  the empty row  i dont want this type of empty row between the data
3.xlsx
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
Then try this...
Sub ConvertCSVToXLSX()
Dim fso As Object
Dim FolderPath As String
Dim srcFolder As Object
Dim file As Object
Dim wb As Workbook
Dim FileName As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set fso = CreateObject("Scripting.FileSystemObject")

FolderPath = ThisWorkbook.Path
Set srcFolder = fso.GetFolder(FolderPath)

For Each file In srcFolder.Files
    If fso.GetExtensionName(file) = "csv" Then
        Set wb = Workbooks.Open(file)
        FileName = Left(file.Name, InStr(file.Name, ".") - 1)
        FileName = FileName & ".xlsx"
        wb.SaveAs FolderPath & "\" & FileName, 51
        On Error Resume Next
        wb.Sheets(1).Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        wb.Close True
        fso.DeleteFile (file)
    End If
Next file
Application.ScreenUpdating = True
End Sub

Open in new window

Author

Commented:
Thnx Neeraj Sir and Roy Cox Sir for ur Great Support
Have a Great Day
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome Sachin!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial