Macro to have SaveAS dialog box come up with file format already chosen

rltidd27

New Member
Joined
Mar 18, 2017
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Wish to have users click on the button to make a copy of the spreadsheet in which they are working.
1. Would like to pause so the user can browse for directory and input the name
2. Would already like to have the file format chosen for them
3. After choosing the location and file name hit enter and a new copy is saved.

I have tried several things, but am missing something.
Below is what I am currently using.

'this will offer a save as to make a new cust copy
Sub Save_as()

'Application.GetSaveAsFilename
ActiveWorkbook.SaveAs FileFormat = 52

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Coming from Access mainly, I would approach this using the msoFileDialogFolderPicker (if you're trying to chose a folder) or msoFileDialogFilePicker to choose a file, or msoFileDialogSaveAs. File picker will allow you to specify the file extension to filter to, but folder picker won't show files. You also cannot filter folder picker, nor can you use multi-select. It is a 1 folder operation.

However, I don't see how picking a file to Save As for a sheet could fit your needs.
choosing the location and file name
Choosing a file name with a save as operation would overwrite an existing file?
 
Upvote 0
Basically the workbook is like a template. Users would want to save a copy with a customer name, and choose the folder to which it is saved. I do not want them to have to know which file extension to use, would rather that come up automatically. Then they would just choose file location and name and save.
Not sure how to use what you sent to accomplish that.
Thanks
Rick
 
Upvote 0
Upvote 0
Using this suggestion: Workbook.SaveAs method (Excel)
My macro gave this response: File name already exists in this location. Do you want to replace it?
Answering no, and I get the debug window.
All I am attempting to accomplish is opening the save as dialog, wit the format of .XLSM already chosen.
The other suggestions are a bit above my knowledge, but if you think they will accomplish the simple task I wish to achieve, I can dig in.
Thanks for the suggestions!


This is what I tried:
'this will offer a save as to make a new cust copy
Sub Save_as()

'Application.GetSaveAsFilename
ActiveWorkbook.SaveAs

End Sub
 
Last edited:
Upvote 0
That will attempt to save the active workbook using the same name - not what you want. There are lots of code samples on how to save a sheet. Here's just one
Here's code that I have that uses the dialog to pick a folder. You'd need to incorporate code that chooses the sheet and the file name (which you haven't stated where that comes from). Note: this should do steps 1 and 2. The result should be that strPath is the full path including the extension type hard coded (xls). Change that to suit. strFile line needs to be completed as well.

VBA Code:
Sub SaveSheetAsFile
Dim fd as FileDialog
Dim strPath As String, strFile As String

Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
   .InitialFileName = "C:\Users\Micron\etc\etc\" 'use if you want to set folder to start in. 
    'Could use ThisWorkbook.Path to get current folder as starting folder

   If .Show Then
      strFile = ???
      strPath = .SelectedItems(1) & "\" strFile & ".xls"
   End If
End With

End Sub
 
Upvote 0
That will attempt to save the active workbook using the same name - not what you want. There are lots of code samples on how to save a sheet. Here's just one
Here's code that I have that uses the dialog to pick a folder. You'd need to incorporate code that chooses the sheet and the file name (which you haven't stated where that comes from). Note: this should do steps 1 and 2. The result should be that strPath is the full path including the extension type hard coded (xls). Change that to suit. strFile line needs to be completed as well.

VBA Code:
Sub SaveSheetAsFile
Dim fd as FileDialog
Dim strPath As String, strFile As String

Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
   .InitialFileName = "C:\Users\Micron\etc\etc\" 'use if you want to set folder to start in.
    'Could use ThisWorkbook.Path to get current folder as starting folder

   If .Show Then
      strFile = ???
      strPath = .SelectedItems(1) & "\" strFile & ".xls"
   End If
End With

End Sub
Thanks for your help and suggestions. I still could not figure it out.
I ran across someone else and they wrote me a macro that did what I wanted.
Thanks again for the effort.
 
Upvote 0
You're welcome. Glad you got a solution. If you think it might help someone else in the future, it's customary to post it. You can mark your own post as the solution if you want.
 
Upvote 0
Here is the solution that another person provided for me and it works.

'this will offer a save as to make a new cust copy
VBA Code:
Option Explicit
Sub saveFile()
Application.DisplayAlerts = False
Dim folder As FileDialog
Dim fItem As String
Dim xlfileName
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
With folder
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    If .Show <> -1 Then
        MsgBox "folder not selected. try again"
        Exit Sub
            Else
        fItem = .SelectedItems(1)
        xlfileName = Application.GetSaveAsFilename(InitialFileName:=fItem, FileFilter:="Excel Files (*.xlsm), *.xlsx")
        If xlfileName <> False Then
            ActiveWorkbook.SaveAs Filename:=xlfileName, FileFormat:=52
                Else
            MsgBox "Please enter file name and try again"
            Exit Sub
        End If
    End If
End With
Application.DisplayAlerts = True
End Sub
 
Last edited by a moderator:
Upvote 0
Solution
@rltidd27
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,455
Members
449,161
Latest member
NHOJ

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top