VBA Save File – 20 Easy Examples

Associated Files Download Links


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.

Save the active workbook.

Note: This is the current active workbook from with in the VBA code.

Saved the workbook where the code is stored.

Save all open workbooks

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

Save a workbook defined by 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.

Save a workbook based on a cell value

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

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

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.

Create Save As Dialog Box with Default File Name Provided

Create Save As Dialog Box with Default File Name Provided

Create & Save New Workbook

Disable Save Alerts

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

and to re-able alerts:

 

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!


<<Return to VBA Examples

Did you find this VBA tutorial useful? Then share it with your friends and colleagues: