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
jjxia2001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EmenizerCommented:
This might work.
The file name (and folder) is suggested but can be overwritten.

I'm not sure if you meant that the filename could or couldn't be overwritten though.
Sub saveTheFile()
    Dim strFileName As String
    'get the name and folder
    strFileName = Application.GetSaveAsFilename( _
        InitialFileName:="C:\yourFileName.xlsm")
    'save as such
    Application.ThisWorkbook.SaveAs (strFileName)
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jjxia2001Author Commented:
Thanks, your code works!  One more question: how do I identify if the user click "Cancel" button in the saving window.
0
jjxia2001Author Commented:
I forgot to mention that the file name needs a option to be overwriiten and as Excel file
0
EmenizerCommented:
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

0
jjxia2001Author Commented:
Works! Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.