Hi
I am trying to use the below code to save a worksheet as CSV, the code runs without any issues. When i open the csv file i get the below error message
"The file format differs from the format that the file name extension specifies"
How can this be rectified?
I am trying to use the below code to save a worksheet as CSV, the code runs without any issues. When i open the csv file i get the below error message
"The file format differs from the format that the file name extension specifies"
How can this be rectified?
Code:
Sub SaveValues()
Dim SourceBook As Workbook, DestBook As Workbook, SourceSheet As Worksheet, DestSheet As Worksheet
Dim SavePath As String, i As Integer
Application.ScreenUpdating = False
Set SourceBook = ThisWorkbook
'*********************************************
'Edit next two lines as necessary
SavePath = Range("rngpath").Value
Set SourceSheet = SourceBook.Sheets("Sheet3")
'*********************************************
Set DestBook = Workbooks.Add
Set DestSheet = DestBook.Worksheets.Add
Application.DisplayAlerts = False
For i = DestBook.Worksheets.Count To 2 Step -1
DestBook.Worksheets(i).Delete
Next i
Application.DisplayAlerts = True
SourceSheet.Cells.Copy
With DestSheet.Range("A1")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats 'Delete if you don't want formats copied
End With
DestSheet.Name = SourceSheet.Name
DestBook.Activate
With ActiveWindow
.DisplayGridlines = False
.DisplayWorkbookTabs = False
End With
SourceBook.Activate
Application.DisplayAlerts = False 'Delete if you want overwrite warning
DestBook.SaveAs Filename:=SavePath
Application.DisplayAlerts = True 'Delete if you delete other line
SavePath = DestBook.FullName
DestBook.Close 'Delete if you want to leave copy open
MsgBox ("A copy has been saved to " & SavePath)
End Sub