将工作表另存为csv文件时,提示另存为文件doalog

时间:2016-01-25 16:54:08

标签: excel excel-vba csv savefiledialog vba

我有以下代码将我的工作表作为csv文件保存到保存工作簿的文件夹中。如何修改此项以显示“另存为”对话框,让我选择我要保存的位置?

更具体地说,我想修改代码,以便只能指定保存所有文件的路径。我不希望得到每个工作表的保存。

Sub SaveOnlyCSVsThatAreNeeded()
Dim ws As Worksheet, newWb As Workbook

Application.ScreenUpdating = False
For Each ws In Sheets(Array("01 - Currencies", ..."14 - User Defined Fields"))
    ws.Copy
    Set newWb = ActiveWorkbook
    With newWb
      .SaveAs ws.Name, xlCSV
      .Close (False)
    End With
Next ws
Application.ScreenUpdating = True

End Sub

我已经用文件夹选择器替换了整个文件,以简化它。发布更新的代码。现在我得到错误代码9 - 下标超出范围。

Sub SaveOnlyCSVsThatAreNeeded()
Dim ws As Worksheet, newWb As Workbook
Dim pathh As Variant

Dim FolderName As String

With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    If .Show = -1 Then
        FolderName = .SelectedItems(1)
    End If
End With

pathh = FolderName

Application.ScreenUpdating = False
For Each ws In Sheets(Array("01 - Currencies", "02 - .....14 - User Defined Fields"))
    ws.Copy
    Set newWb = ActiveWorkbook
    With newWb
      .SaveAs pathh.path & "\" & ws.Name, xlCSV
      .Close (False)
    End With
Next ws
Application.ScreenUpdating = True

End Sub

2 个答案:

答案 0 :(得分:0)

使用以下代码显示“另存为”对话框屏幕:

pathh = Application.GetSaveAsFilename( _
            FileFilter:="CSV Files (*.csv), *.csv", _
            Title:="Save all spreadsheets", _
            InitialFileName:=filenamestring)

干杯

答案 1 :(得分:0)

在VB中支持另存为对话框的命令是:

Application.GetSaveAsFilename