5

By clicking a button in Excel, the user exports a specific sheet to a csv with a dynamic filename and the csv is saved in a pre-determined directory.

Instead of saving to a predetermined directory, can users have the browse window to choose a directory to save to?

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
Community
  • 1
  • 1
Maz
  • 183
  • 1
  • 2
  • 10
  • Start reading [Application.FileDialog Property (Excel)](http://msdn.microsoft.com/en-us/library/office/ff836226.aspx) and it should help. – PatricK Feb 05 '14 at 04:51

4 Answers4

6

Excel has an inbuilt FileSave Dialog. It is called .GetSaveAsFilename. Use that.

Syntax

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

Usage

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
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
3

As Patrick suggested, you're looking for the .FileDialog property.

To implement it, try this:

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
ARich
  • 3,230
  • 5
  • 30
  • 56
  • After playing with it, I have come across a bug. It works great, however if you try to save it a second time I get a popup saying file already exists, would you like to overwrite. If you click yes, it works fine, if you click no, it crashes. Debug goes to this line:.SaveAs Filename:=MyPath & MyFileName, FileFormat:=xlCSV, CreateBackup:=False ;Any ideas? – Maz Feb 05 '14 at 23:08
  • [See this SO post](http://stackoverflow.com/questions/15967659/saveas-issue-overwrite-existing-file-excel-vba). Specifically, I think you're looking for the `ConflictResolution:=xlLocalSessionChanges` argument of the `SaveAs` method. – ARich Feb 06 '14 at 01:41
1

Try This......

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
CrashOverride
  • 338
  • 3
  • 10
  • 22
1

Here's a script I've been using lately that I like a lot. Thought I would leave this here:

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
peter.domanico
  • 61
  • 1
  • 1
  • 9