在 VBA 中导出时设置自定义路径

在 VBA 中导出时设置自定义路径

我正在使用这个帖子的接受我的一个出口的答案。

根据答案,它将我导出的.csv文件保存在我的C:\Users\username\AppData\Roaming\Microsoft\Excel\XLSTART.

但是,我需要将此导出到我要导出数据的文件位置。它应该是C:\Users\username\Desktop\Sample_Files\Excel_to_csv_export

根据答案,我尝试了以下操作:

Sub ExportTable()

    Dim wb As Workbook, wbNew As Workbook
    Dim ws As Worksheet, wsNew As Worksheet
    Dim wbNewName As String


   Set wb = ThisWorkbook
   Set ws = ActiveSheet

   Set wbNew = Workbooks.Add

   'I set the path below, which did not work
   Set Path = 'C:\Users\username\Desktop\Sample_Files\Excel_to_csv_export'

   With wbNew
       Set wsNew = wbNew.Sheets("Sheet1")
       wbNewName = ws.ListObjects(1).Name
       ws.ListObjects(1).Range.Copy
       wsNew.Range("A1").PasteSpecial Paste:=xlPasteAll
       .SaveAs Filename:=wb.Path & "\" & wbNewName & ".csv", _
             FileFormat:=xlCSVMSDOS, CreateBackup:=False
   End With

End Sub

我该如何将路径设置为我想要的路径?任何帮助都非常感谢!

相关内容