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:
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.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Have you ever been working with data in Excel and experienced a "freeze" where the program stops responding? This can be ...
Discover MoreIf you are using Excel as a repository for data used in your business, you may want to figure out a way to make that ...
Discover MoreWorkbooks can contain many worksheets. If you want to pull a workbook apart and create a whole series of workbooks based ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-11-22 05:03:26
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
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.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments