Link to home
Start Free TrialLog in
Avatar of jjxia2001
jjxia2001

asked on

How to code to save a file with a suggested name and in a selected folder

I need create a save button for saving the file with a suggested name in the file name box but being able to choose where they want to save and have the default to C:\drive

Sub btnSave_Click()
.....
.....

End Sub
ASKER CERTIFIED SOLUTION
Avatar of Emenizer
Emenizer
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jjxia2001
jjxia2001

ASKER

Thanks, your code works!  One more question: how do I identify if the user click "Cancel" button in the saving window.
I forgot to mention that the file name needs a option to be overwriiten and as Excel file
You could check the Cancel button by checking if the fileName is False.

As for your last remark: I'm not sure what you mean by 'file name needs a option to be overwritten'?
Do you mean in the 'Save As' Dialog?
Sub saveTheFile()
    'get the name and folder
    Filename = Application.GetSaveAsFilename( _
        InitialFileName:="C:\yourFileName.xlsm", _
        fileFilter:="Excel Macro-Enabled Workbook (*.xlsm), *.xlsm, Excel 97 Workbook (*.xls), *.xls")
    'save if not canceled
    If Filename <> False Then
        Application.ThisWorkbook.SaveAs (Filename)
    End If
End Sub

Open in new window

Works! Thanks!