将文件另存为csv,并继续显示为.xlsx

时间:2018-12-20 17:05:11

标签: excel vba csv export-to-csv file-format

我正在尝试将工作簿的副本输出为CSV文件。我有下面的代码,但保存时会以Excel工作簿的文件类型显示。

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

2 个答案:

答案 0 :(得分:1)

您知道要将其保存为的名称,所以也许这只是选择正确文件夹的一种情况:

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

答案 1 :(得分:1)

替换为:

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

遵循以下逻辑:

  1. 路径-目录
  2. “ \”
  3. 文件名
  4. 文件类型-.csv