Return to VBA Code Examples

VBA Save File – 20 Easy Examples

This VBA Tutorial covers how to save a file using the Save and Save As commands in VBA.

Save Workbook – VBA

The VBA Save command saves an Excel file similarly to clicking the Save icon or using the Save Shortcut (CTRL + S).

Save a Specified Workbook

To save a workbook, reference the workbook object and use the Save command.

Save the Active Workbook

Note: This is the current active workbook from with in the VBA code, which is different from ThisWorkbook which contains the running code.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

vba save as

 

Learn More!

Save the Workbook Where the Code is Stored

Save all Open Workbooks

This will loop through all open workbooks, saving each one.

Tired of Searching for VBA Code Examples? Try AutoMacro!

Save all open workbooks that were not opened ReadOnly

Note: opening a workbook in ReadOnly mode prevents the file from being saved.
To save the file you will need to use Save As and save the file with a different name.

Save a workbook defined by a variable

This will save a workbook that was assigned to a workbook object variable.

Save a workbook defined by a string variable

This will save a workbook that’s name was saved to a string variable.

Save a workbook defined by the order it was opened.

Note: The first workbook opened would have 1, the second 2, etc.

Great Product. AutoMacro doesn't just write your code, it teaches as you go!" - Tony, UK

Learn more

Read our 900+ Reviews

Save a workbook based on a cell value

This will save a workbook that’s name is found in a cell value.

VBA Programming | Code Generator does work for you!

Save As – VBA

The VBA Save As command saves an Excel file as a new file, similar to clicking the Save As icon or using the Save As Shortcut (Alt > F > A).
Above, we identified all the ways to specify which workbook to save. You can use those exact same methods to identify workbooks when using Save As.

Save As behaves similarly to Save, except you also need to specify the name of the new file.
In fact, Save As has many potential variables to define:

SaveAs Syntax:

A full description of all of the SaveAs arguments is included below. For now we will focus on the most common examples.
Note: These arguments can be entered as string with parenthesis or as defined variables.

Save As Syntax Examples:

Workbook Save As – Same Directory

or

or

VBA Programming | Code Generator does work for you!

Workbook Save As – New Directory

or

Workbook Save As – New Directory, Specify File Extension

or

Workbook Save As – New Directory, Specify File Extension – Alt Method

You can also specify the file format in it’s own argument.

Workbook Save As – Add Password to Open File

VBA Programming | Code Generator does work for you!

Workbook Save As – Add Password for Write Privileges

If correct password is not supplied then workbook opens as Read-Only

Workbook Save As – Read-Only Recommended

TRUE to display a message box, recommending that the file is opened read-only.

Other Save As Examples

Create Save As Dialog Box

This Generates the Save As Dialog Box, prompting the user to Save the file.
Keep in mind that this simple code may not be appropriate in all cases.

VBA Programming | Code Generator does work for you!

Create Save As Dialog Box with Default File Name Provided

Create Save As Dialog Box with Default File Name Provided

Create & Save New Workbook

This will create a new workbook and immediately save it.

Disable Save Alerts

As you work with saving in VBA, you may come across various Save Warnings or Prompts. To disable warnings, add this line of code:

and to re-able alerts: