Excelmacros – 导出到CSV文件并浏览保存目录

我已经成功地做了一个macros观的工作,做我所需要做的事情,但我想做得更好,而且不能。

– 这个位起作用 – 通过在excel中单击一个button,用户将一个特定工作表导出到一个带有dynamic文件名的csv,并将csv保存在一个预先确定的目录中。

– 它可以这样做 – 而不是它保存到预定的目录,我可以有浏览窗口显示,以便他们可以select一个目录,以保存到? 我无法弄清楚如何做到这一点。

这是我的macros:

Sub Export() Dim MyPath As String Dim MyFileName As String MyPath = "C:\importtest" MyFileName = "MR_Update_" & Sheets("Monthly Review").Range("D3").Value & "_" & Format(Date, "ddmmyyyy") If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\" If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv" Sheets("Export Data").Copy With ActiveWorkbook .SaveAs Filename:= _ MyPath & MyFileName, _ FileFormat:=xlCSV, _ CreateBackup:=False .Close False End With End Sub 

正如帕特里克build议,你正在寻找.FileDialog属性。

要实现它,试试这个:

 Sub Export() Dim MyPath As String Dim MyFileName As String MyFileName = "MR_Update_" & Sheets("Monthly Review").Range("D3").Value & "_" & Format(Date, "ddmmyyyy") If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv" Sheets("Export Data").Copy With Application.FileDialog(msoFileDialogFolderPicker) .Title = "Select a Folder" .AllowMultiSelect = False .InitialFileName = "" '<~~ The start folder path for the file picker. If .Show <> -1 Then GoTo NextCode MyPath = .SelectedItems(1) & "\" End With NextCode: With ActiveWorkbook .SaveAs Filename:=MyPath & MyFileName, FileFormat:=xlCSV,CreateBackup:=False .Close False End With End Sub 

Excel有一个内置的FileSave对话框 。 它被称为.GetSaveAsFilename 。 使用它。

句法

expression式.GetSaveAsFilename(InitialFilename,FileFilter,FilterIndex,Title,ButtonText)

用法

 Dim fileSaveName As Variant fileSaveName = Application.GetSaveAsFilename( _ fileFilter:="Excel Files (*.csv), *.csv") If fileSaveName <> False Then ' '~~> Your code to save the file here ' End If 

尝试这个……

 Sub Export() Dim MyPath As String Dim MyFileName As String MyPath = "C:\importtest" MyFileName = "MR_Update_" & Sheets("Monthly Review").Range("D3").Value & "_" & Format(Date, "ddmmyyyy") If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv" Sheets("Export Data").Copy With ActiveWorkbook .SaveAs Filename:= _ MyFileName, _ FileFormat:=xlCSV, _ CreateBackup:=False .Close False End With End Sub 

这是我最近使用的一个脚本,我喜欢很多。 以为我会留在这里:

 Sub ExportCSV() Dim FlSv As Variant Dim MyFile As String Dim sh As Worksheet Dim MyFileName As String Dim DateString As String DateString = Format(Now(), "yyyy-mm-dd_hh_mm_ss_AM/PM") '<~~ uses current time from computer clock down to the second MyFileName = DateString & "_" & "Whatever you like" Set sh = Sheets("Sheet you'd like to export") sh.Copy FlSv = Application.GetSaveAsFilename(MyFileName, fileFilter:="CSV (Comma delimited) (*.csv), *.csv", Title:="Where should we save this?") If FlSv = False Then GoTo UserCancel Else GoTo UserOK UserCancel: '<~~ this code is run if the user cancels out the file save dialog ActiveWorkbook.Close (False) MsgBox "Export canceled" Exit Sub UserOK: '<~~ this code is run if user proceeds with saving the file (clicks the OK button) MyFile = FlSv With ActiveWorkbook .SaveAs (MyFile), FileFormat:=xlCSV, CreateBackup:=False .Close False End With End Sub