VBA export to CSV error

Billdub417

New Member
Joined
Nov 5, 2019
Messages
37
Hello all,

I have the following code, but when this exports to a separate csv file, it comes up with an error message "File format and extension of xxx don't match. The filecould be corrupted or unsafe."

Is there a way to stop this error message coming up, without going in and resaving it as a csv file:

VBA Code:
Dim wbI As Workbook, wbO As Workbook
    Dim wsI As Worksheet, wsO As Worksheet
    Set wbI = ThisWorkbook
    Set wsI = wbI.Sheets("Export")
    Set wbO = Workbooks.Add

    With wbO
        Set wsO = wbO.Sheets("Sheet1")
        wsI.Range("A1:aa700").Copy
        wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
    End With
    Dim IntialName As String
Dim sFileSaveName As Variant
IntialName = "Sample Output"
sFileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName, fileFilter:="Excel Files (*.csv), *.csv")
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,658
Office Version
  1. 365
Platform
  1. Windows
There is nothing in your code that saves the file.
 

Billdub417

New Member
Joined
Nov 5, 2019
Messages
37
VBA Code:
If sFileSaveName <> False Then
    ActiveWorkbook.SaveAs sFileSaveName
End If

End Sub

Apologies, missed the bottom lines off the code
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,658
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Try
VBA Code:
ActiveWorkbook.SaveAs sFileSaveName, xlCSVUTF8
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,658
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

Forum statistics

Threads
1,141,858
Messages
5,709,035
Members
421,606
Latest member
Faisel135

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
Top