How to use Worbook.SaveAs

The Worbook SaveAs method is used to save changes to the workbook in a different file. In the below example the user selects the folder and gives the filename using the GetSaveAsFilename dialog.


  Dim varFile As Variant
  Dim strFile As String
  varFile = Application.GetSaveAsFilename(InitialFileName:="c:\temp\test.xlsx", FileFilter:="Excel Files, *.xlsx")
  If varFile <> False Then
      strFile = varFile
      Dim wb As Workbook: Set wb = ActiveWorkbook
      wb.SaveAs Filename:=strFile, FileFormat:=xlOpenXMLWorkbook
  End If
workbook saveas code builder

On this page we show the different options available when saving the workbook using the SaveAs arguments Filename FileFormat, Password, ReadOnlyRecommended, CreateBackup, AddToMru and Local.

The image on the right shows the Code VBA (download) builder that explains what the different options mean and allows you to select required values.

Optional arguments

Filename (String) - A string that indicates the name of the file to be saved. You can include a full path; if you don't, Microsoft Excel saves the file in the current folder.

FileFormat (XlFileFormat) - The file format to use when you save the file. For a list of valid choices, see the XlFileFormat enumeration. For an existing file, the default format is the last file format specified; for a new file, the default is the format of the version of Excel being used.


Here you can find possible values for

XlFileFormat

workbook saveas password Password (String) - A case-sensitive string (no more than 15 characters) that indicates the protection password to be given to the file.

WriteResPassword - A string that indicates the write-reservation password for this file. If a file is saved with the password and the password isn't supplied when the file is opened, the file is opened as read-only

ReadOnlyRecommended (Boolean) - True to display a message when the file is opened, recommending that the file be opened as read-only.

CreateBackup (Boolean) - True to create a backup file.

ribbon set share AccessMode (XlSaveAsAccessMode) - The access mode for the workbook.

Possible return values are xlExclusive - Exclusive mode, xlNoChange - Default (does not change the access mode), xlShared - Share list.

ConflictResolution (XlSaveConflictResolution) - An XlSaveConflictResolution value that determines how the method resolves a conflict while saving the workbook. If set to xlUserResolution, the conflict-resolution dialog box is displayed. If set to xlLocalSessionChanges, the local user's changes are automatically accepted. If set to xlOtherSessionChanges, the changes from other sessions are automatically accepted instead of the local user's changes. If this argument is omitted, the conflict-resolution dialog box is displayed.

Possible return values are xlLocalSessionChanges - The local user's changes are always accepted, xlOtherSessionChanges - The local user's changes are always rejected, xlUserResolution - A dialog box asks the user to resolve the conflict.

AddToMru (Boolean) - True to add this workbook to the list of recently used files. The default value is False.

TextCodepage - Ignored for all languages in Microsoft Excel. NOTE: When Excel saves a workbook to one of the CSV or text formats, which are specified by using the FileFormat parameter, it uses the code page that corresponds to the language for the system locale in use on the current computer. This system setting is available in the Control Panel > Region and Language > Location tab under Current location

TextVisualLayout - Ignored for all languages in Microsoft Excel. NOTE: When Excel saves a workbook to one of the CSV or text formats, which are specified by using the FileFormat parameter, it saves these formats in logical layout. If left-to-right (LTR) text is embedded within right-to-left (RTL) text in the file, or vice versa, logical layout saves the contents of the file in the correct reading order for all languages in the file without regard to direction. When an application opens the file, each run of LTR or RTL characters are rendered in the correct direction according to the character value ranges within the code page (unless an application that is designed to display the exact memory layout of the file, such as a debugger or editor, is used to open the file)

Local - True saves files against the language of Microsoft Excel (including control panel settings). False (default) saves files against the language of Visual Basic for Applications (VBA) (which is typically US English unless the VBA project where Workbooks.Open is run from is an old internationalized XL5/95 VBA project)