CSV to XLSX Not Working Anymore

Samrari

New Member
Joined
Feb 18, 2016
Messages
15
I wrote this code to convert csv files to xlsx which worked well for the first set of about 100 files. I just got a second batch from our vendor and I tried converting them but am now getting the "Excel cannot open the file 'SomeName.xlsx' because the file format or file extension is not valid. Verify that the files has not been corrupted and the file extension matches the format of the file." Not sure what broke. Please help. I would also be open to code that can convert all the csv files in the folder to xlsx. But that is not a priority.
Code:
Sub Set_Default_Folder_Click()
  'This sets the 'Data Folder' name using 'Folder Picker'
    Dim DefaultFolder As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show = -1 Then
            'Ok Clicked
            DefaultFolder = .SelectedItems(1)
            Range("E6").Value = DefaultFolder
        Else
        'Cancel
            MsgBox ("New Default Folder was not selected. Same folder will be used to store the new data")
            Exit Sub
            
        End If
        
    End With
     
 
End Sub

Sub Convert_to_XLSX_Click()
    Dim MyFile As String
    Dim XLSName As String
    Dim SheetName As String
    Dim CurrentFolder As String
       
    With Application.FileDialog(msoFileDialogFilePicker)
        If .Show = -1 Then
            MyFile = .SelectedItems(1)
            Workbooks.Open (MyFile)
        Else
        'Cancel
            MsgBox ("No File was selected.")
            Exit Sub
                
        End If
    End With
    'Get name of the sheet from the CSV file
    SheetName = ActiveSheet.Name
    
    Sheets(SheetName).Columns("A:A").EntireColumn.AutoFit
    Sheets(SheetName).Columns("B:B").EntireColumn.AutoFit
    Sheets(SheetName).Columns("C:C").EntireColumn.AutoFit
    Sheets(SheetName).Columns("D:D").EntireColumn.AutoFit
    Sheets(SheetName).Columns("E:E").EntireColumn.AutoFit
    Sheets(SheetName).Columns("F:F").EntireColumn.AutoFit
    Sheets(SheetName).Columns("G:G").EntireColumn.AutoFit
    Sheets(SheetName).Columns("J:J").EntireColumn.AutoFit
    Sheets(SheetName).Range("K:K").Select
    Selection.NumberFormat = "0"
    Sheets(SheetName).Columns("L:L").ColumnWidth = 60
    Sheets(SheetName).Range("B:B").Select
    Selection.NumberFormat = "0"
    Sheets(SheetName).Range("C:C").Select
    Selection.NumberFormat = "0"
    CurrentFolder = Range("E6").Value
    ChDir (CurrentFolder)
    XLSName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)
    ActiveWorkbook.SaveAs Filename:=(CurrentFolder) & "\" & XLSName & ".xlsx"
    Application.DisplayAlerts = False
    ActiveWorkbook.Close
End Sub
 
Re: SOLVED: Re: CSV to XLSX Not Working Anymore

Since you are opening a CSV, and then saving as an Excel file, you may need to explicitly tell it the file type you are saving to (because it may be defaulting to the file type of the file you just opened).

Try changing this line:
Code:
ActiveWorkbook.SaveAs Filename:=(CurrentFolder) & "\" & XLSName & ".xlsx"
to this:
Code:
ActiveWorkbook.SaveAs Filename:=(CurrentFolder) & "\" & XLSName & ".xlsx", _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
and see if that makes a difference.
 
Last edited:
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Re: SOLVED: Re: CSV to XLSX Not Working Anymore

Hi Joe,

I changed it to my code as follows based on answer from excelforum.com. Now I see the value of not cross posting and appologize for being an idiot. Here's what I changed and it is working again.
Code:
ActiveWorkbook.SaveAs Filename:=(CurrentFolder) & "\" & XLSName & ".xlsx", FileFormat:=51, CreateBackup:=False

Again, thank you for your help.

Regards,

Kas
 
Upvote 0

Forum statistics

Threads
1,223,532
Messages
6,172,878
Members
452,486
Latest member
standw01

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