Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Saving a Workbook in a Macro.

Saving a Workbook in a Macro

by Allen Wyatt
(last updated June 5, 2019)

3

If you want to save a workbook under control of your macro, you can use the Save method. This is the same as choosing Save from the ribbon options, so it will display the Save As dialog box if the document you are saving has not been previously saved. The syntax is as follows:

ActiveWorkbook.Save

If you want to save the workbook to a file with a new name, use the following basic syntax:

ActiveWorkbook.SaveAs FileName:="filename"

where "filename" is the full name (including a path), in quotes, that you want used for the file.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10769) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Saving a Workbook in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Inserting Dashes between Letters in Words

Sometimes typing isn't straight typing. Sometimes you need to perform special tasks, such as putting dashes between ...

Discover More

Adding Columns to Your Page Layout

Most documents are created using a single column of text. Word, however, allows you to use many, many columns in your ...

Discover More

Updating a Table of Contents

For longer documents, a table of contents is almost a necessity. Google Docs allows you to add a TOC to a document, but ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Comparing Workbooks

Do you need to compare two workbooks to each other? While you can use specialized third-party software to do the ...

Discover More

Opening a Workbook to a Specific Worksheet

When you open a workbook, Excel displays the worksheet that was visible when the workbook was last saved. You may want, ...

Discover More

Sudden Increases in Workbook File Size

Workbooks can get rather large rather quickly. If you think your workbook has gotten too big too fast, here are some ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 5?

2020-11-22 05:03:26

Alan Elston

As an example:
The following macro saves a file with the name Book1.xls on my desktop
It build the file name from the first three cells in the active workbook, as shown in the pic below

Sub Macro1()
Dim ThisFile As String, Ws1 As Worksheet
Set Ws1 = ActiveWorkbook.Worksheets.Item(1) ' First tab worksheet in active workbook
Let ThisFile = Ws1.Range("A1").Value & Ws1.Range("B1").Value & Ws1.Range("C1").Value
ActiveWorkbook.SaveAs Filename:="C:\Users\Elston\Desktop\" & ThisFile & ".xls"
End Sub


(see Figure 1 below)

Figure 1. 


2020-11-22 04:50:02

Alan Elston

Hello Bob
There are probably a few issues, but a few things are not clear form the info that you gave. So we can only give general info.

_ The main issue is probably related to what Allen Wyatt said above….
….”…. filename is the full name (including a path)…..”
I don’t know what you have in cells A1 or B1 or C1 , but the final string you end up with must have a form of like :
Path & "\" & Name & Extension
Path will look something like C:\Users\Elston\Desktop
Name would be something like Book1
Extension might be something like .xlsx

_ You need to be clear about where the macro is, and where the ranges are containing the string information. It is usually best to qualify the ranges fully to give their workbook and worksheet. Just one of many possibilities would be like
ActiveWorkbook.Worksheets.Item(1).Range("A1").Value
That last line example is referring to the first cell in the first worksheet of the active workbook. But I don’t know if that is what you are wanting

Without knowing the exact details of what you are doing, or wanting to do, it is not possible to know exactly what the problem might be

Alan Elston


2020-11-21 10:22:58

Bob Jamison

I would like to have a macro that saves a file according to the values in A1&B1&C1. I found this in a search:

Sub SaveAsA1()
ThisFile = Range("A1").Value & Range("B1").Value & Range("C1").Value
ActiveWorkbook.SaveAs Filename:=ThisFile
End Sub


It does not seem to work. I use a PC, Windows 10, Office 365.
Does anyone have a suggestion?
Thank you.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.