+ Reply to Thread
Results 1 to 10 of 10

CSV to XLSX Not Working Anymore

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2016
    Location
    Elk Grove, CA, USA
    MS-Off Ver
    2010
    Posts
    12

    Exclamation CSV to XLSX Not Working Anymore

    Greetings,

    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.

    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

  2. #2
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: CSV to XLSX Not Working Anymore

    Can you upload a sample of a csv file that was converted, a sample of a csv file from the new batch, and a copy of the workbook which contains the code. Thanks

  3. #3
    Registered User
    Join Date
    01-26-2016
    Location
    Elk Grove, CA, USA
    MS-Off Ver
    2010
    Posts
    12

    Re: CSV to XLSX Not Working Anymore

    I am sorry I can't upload the original csv files as they contain data only sent to us by the vendor upon a subpoena. I am attaching a sample csv file that I saved as csv and converted it to xlsx. All three attached here. Thank you for your help.
    Attached Files Attached Files

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: CSV to XLSX Not Working Anymore

    Hi, kas.samrari,

    did you change the standard file saving format for Excel in the meantime?

    Instead of
        ActiveWorkbook.SaveAs Filename:=(CurrentFolder) & "\" & XLSName & ".xlsx"
    try
        ActiveWorkbook.SaveAs Filename:=CurrentFolder & "\" & XLSName & ".xlsx",  FileFormat:=51
    And you should work a little bit on the coding as it looks rather ... macro recorded.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Registered User
    Join Date
    01-26-2016
    Location
    Elk Grove, CA, USA
    MS-Off Ver
    2010
    Posts
    12

    Re: CSV to XLSX Not Working Anymore

    Hello Holger,

    Thank you. That did it. Yes, I am new to VBA learning it by recording macros and from experts like you.

    I have one more posting on the forum for copying data, can you help with that please.

    Regards,

    Kas

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: CSV to XLSX Not Working Anymore

    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    01-26-2016
    Location
    Elk Grove, CA, USA
    MS-Off Ver
    2010
    Posts
    12

    Re: CSV to XLSX Not Working Anymore

    Am not allowed to post on multiple forums. Are they all connected.

  8. #8
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: CSV to XLSX Not Working Anymore

    Sub Convert_to_XLSX_Click()
        Dim MyFile As String
        Dim XLSName As String
        Dim SheetName As String
        Dim CurrentFolder As String
        
        Application.ScreenUpdating = False
        
        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
        
        Stop
        
        'Get name of the sheet from the CSV file
        SheetName = ActiveSheet.Name
        
        With Sheets(SheetName)
            .Columns("A:G").EntireColumn.AutoFit
            .Columns("J:J").EntireColumn.AutoFit
            .Range("B:B").NumberFormat = "0"
            .Range("C:C").NumberFormat = "0"
            .Range("K:K").NumberFormat = "0"
            .Columns("L:L").ColumnWidth = 60
        End With
        
        CurrentFolder = Range("E6").Value
        XLSName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)
        
        Application.DisplayAlerts = False
        ActiveWorkbook.SaveAs _
            Filename:=(CurrentFolder) & "\" & XLSName & ".xlsx", _
            FileFormat:=xlOpenXMLWorkbook, _
            CreateBackup:=False
        ActiveWorkbook.Close
        
        MsgBox "done"
        
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
    
    End Sub

  9. #9
    Registered User
    Join Date
    01-26-2016
    Location
    Elk Grove, CA, USA
    MS-Off Ver
    2010
    Posts
    12

    Re: CSV to XLSX Not Working Anymore

    Awesome, thank you.

  10. #10
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: CSV to XLSX Not Working Anymore

    You are allowed to post on multiple forums. However, you are expected to tell everyone where you have posted so they don't waste time working on a problem that has been solved elsewhere.

    And be sure you will be found out as most of the people who answer questions here also answer questions on ALL the other forums.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel shortcuts not working anymore
    By flyingdutch06 in forum Excel General
    Replies: 0
    Last Post: 05-16-2015, 06:03 AM
  2. VBA Macro not working anymore
    By winsomeness in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-09-2015, 08:20 PM
  3. VBA code not working anymore
    By random379 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-24-2012, 03:01 AM
  4. Excel filter does not working anymore
    By Berry in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-16-2006, 08:45 PM
  5. [SOLVED] Filter is not working anymore.
    By Berry in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2006, 02:45 AM
  6. [SOLVED] VLOOKUP not working anymore
    By Phil in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-27-2006, 02:45 PM
  7. [SOLVED] unselecting not working anymore?
    By Joe Magiera in forum Excel General
    Replies: 1
    Last Post: 08-31-2005, 11:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1