0

I am trying to output a copy of a workbook as a CSV File. I have the code below but on saving it comes up in file type as Excel workbook.

Sub SAVE_CSV() Dim FileName As String FileName = "CSV Import File" Dim fPth As Object Set fPth = Application.FileDialog(msoFileDialogSaveAs) With fPth .InitialFileName = FileName .Title = "Save Your Import File" .InitialView = msoFileDialogViewList If .Show <> 0 Then ThisWorkbook.SaveAs FileName:=.SelectedItems(1) & "*.csv", FileFormat:=xlCSV End If End With 
3
  • 2
    Try taking out the * in *.csv? Also make sure the fileName is an acceptable one, and .SelectedItems(1) doesn't have illegal characters. Commented Dec 20, 2018 at 17:12
  • @BruceWayne Thanks for the comments! On changing both things my save as dialogue box still comes up with the save as file type = Excel Workbook. Strangely though the .csv extension isn't appearing in the filename input box with the title Commented Dec 20, 2018 at 17:24
  • Right before the .SaveAs line, add Debug.Print .SelectedItems(1) & ".csv". Is the output a legitimate path and file name? Commented Dec 20, 2018 at 17:30

2 Answers 2

1

You know the name you want to save it as, so maybe it's just a case of selecting the right folder:

Sub SAVE_CSV() Dim FileName As String FileName = "CSV Import File Again" Dim fPth As Object Set fPth = Application.FileDialog(msoFileDialogFolderPicker) With fPth .InitialFileName = "C:\Users\Testing\Documents\Can be deleted\" 'Change as required. .Title = "Save Your Import File" .InitialView = msoFileDialogViewList If .Show <> 0 Then ThisWorkbook.SaveAs FileName:=.SelectedItems(1) & "\" & FileName & ".csv", FileFormat:=xlCSV End If End With End Sub 
Sign up to request clarification or add additional context in comments.

Comments

1

Replace with:

ThisWorkbook.SaveAs FileName:=.SelectedItems(1) & ".csv" 

Follow the below logic:

  1. Path - Directory
  2. "\"
  3. File Name
  4. File type - .csv

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.