QuickSilver
QuickSilver

Reputation: 770

'File may be corrupt' when opening file saved from CSV to Workbook format

Can someone help me fix below code as it's saving but I can't open the file as it says that the file may be corrupt. Probably I'm doing something wrong here. Please note that the file is received as CSV format and needs to be saved as a regular excel file.

Sub SaveAsToFolderPath()

Dim MyFileName As String 
Dim folderPath As String 
Dim dateFormat As String

       folderPath = "C:\Users\A\Desktop\M work\DFMS\"
       dateFormat = Format(Now, "dd.mm.yyyy HH-mm-ss AMPM")
       MyFileName = Range("G2").Value

    If Not ActiveWorkbook.Saved Then
        ActiveWorkbook.SaveAs Filename:=folderPath & MyFileName & " - Next Delivery " & dateFormat & ".xlsm"
    End If

End Sub

Upvotes: 0

Views: 1081

Answers (1)

Mathieu Guindon
Mathieu Guindon

Reputation: 71197

You're changing the extension of a CSV file, while saving a CSV file. When you later open the saved .XLSX file, Excel expects the XLSX format, but sees comma-separated values - hence, file isn't in the expected format, it must be corrupt.

Specify the file format when you SaveAs:

SaveAs intellisense

Dim path As String
path = folderPath & MyFileName & " - Next Delivery " & dateFormat & ".xlsm"

ActiveWorkbook.SaveAs path, xlWorkbookDefault

The file formats available to SaveAs are members of the XlFileFormat enum.

Upvotes: 2

Related Questions