VBA Save As Workbook in Specific Folder Excel Macro

Home/VBA/VBA Save As Workbook in Specific Folder Excel Macro

VBA code to save as Workbook to specific folder example code will help us to save as the excel workbook in desired location. We can use SaveAs method of Workbook to save the file. In this example we will see how to save as the Excel workbook using VBA. And this code should work for all the version of Microsoft Excel 2003, Excel 2007, Excel 2010, and Excel 2013.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


VBA code to save as Workbook


Here is the Example VBA syntax and Example VBA code to save as Workbook. This will help you to how to save Excel workbook in Specific directory using VBA.

VBA Save as Workbook: Syntax


Following is the VBA Syntax and sample VBA code to save as Workbook using VBA. We are using the SaveAs method of the Excel Workbook object. This will take full path to save the workbook.


ACTIVEWORKBOOK.SAVEAS (“C:\TEMP\TEST1.XLSX”)

Here workbook can be ActiveWorkbook, ThisWorkbook or a workbook assigned to an object.
ActiveWorkbook.SaveAs will Save the currently active Excel Workbook. And ThisWorkbook.SaveAs will save the workbook which we have written our VBA code. And we can set the workbook to any object and we can save it using WorkbookObject.SaveAs method as shown below:

Dim aWb as Workbook
Set aWb=ActiveWorkbook
aWb.SaveAs “C:\temp\test1.xlsx”

Here is explained VBA code:
‘Declaring the wb object as Workbook, in the next statement we will assign the active workbook to this object.
Dim awb As Workbook

‘Assigning Thisworkbook to workbook object i.e.; awb.
Set awb = ActiveWorkbook

We are saving the workbook using workbook object which we have created above and SaveAs method of the workbook.
awb.SaveAs “C:\temp\test1.xlsx”

Like this we can assign any workbook to an object. And then we can save the Excel Workbook using Object in VBA.

Save as Workbook using VBA: Examples


The following VBA code is to Save the Excel File in desired location. This code will save as the Active Workbook into location mentioned in the code.

Sub sbVBS_To_SaveAs_ActiveWorkbook()
ActiveWorkbook.SaveAs "C:\temp\test1.xlsx"
End Sub

Instructions to run the VBA code to save as Excel Workbook into a specific folder


Please follow the below steps to execute the VBA code to save as the excel file.
Step 1: Open any existing Excel workbook
Step 2: Press Alt+F11 – This will open the VBA Editor
Step 3: Insert a code module from then insert menu
Step 4: Copy the above code and paste in the code module which have inserted in the above step
Step 5: Activate any workbook
Step 6: Now press F5 to execute the code

Now you can observe that your Active Excel workbook is saved in the specific location with the changes.

Explained VBA Code to SaveAs the Excel File :

Starting procedure to write VBA code to SaveAs Excel Workbook using VBA.
Sub sbVBS_To_SaveAs_ActiveWorkbook _C()

‘To save ActiveWorkbook, i.e the workbook which we currently activated (this may or may not contain the code module and this procedure). We are using SaveAs method of the workbook to save the excel file.

ActiveWorkbook.SaveAs “C:\temp\test1.xlsx”

End Sub
Ending sub procedure.

Here ActiveWorkbook object represent the workbook which we currently activated. And SaveAs is the Workbook method to Save the Excel File in the required path. You can find the example code to save the Workbook in different situations in other topics.

LIMITED TIME OFFER
By |April 22nd, 2014|VBA|2 Comments

About the Author:

PNRao is a passionate business analyst and having close to 10 years of experience in Data Mining, Data Analysis and Application Development. This blog is his passion to learn new skills and share his knowledge to make you expertise in Data Analysis (Excel, VBA, SQL, SAS, Statistical Methods, Market Research Methodologies and Data Analysis Techniques).

2 Comments

  1. Gajanan September 7, 2015 at 1:02 PM - Reply

    Thanks Sir,

    But when done second time then I got message of replace earlier workbook. But I dont want to replace

    Can you help me
    I

    • PNRao September 7, 2015 at 10:06 PM - Reply

      Hi,
      ‘SaveAs’ method will always save a file with another name. When you use the same name second time, it will through the error as the file with same name is already exist.
      If you want to use Save the changes, you can just use ‘Save’ method. i.e; ActiveWorkbook.Save ‘this will save the file changes to the same file.
      If you want to save the file with different name use the SaveAs method. Example; ActiveWorkbook.SaveAs “C:\temp\test1.xlsx” ‘New filepath and file name”.

      Hope this clarifies your questions.
      Thanks-PNRao!

Leave A Comment


Related pages


how to unhide all rows in excelclose excel workbook vbapaste vba excelif statements in excel 2010remove duplicates from excel listexcel vba getopenfilenameworksheet vba excelusing sql in excel vbahow to determine duplicates in excelvba merge excel filesvba countvlookup usagemicrosoft excel autofitworksheet in excel definitioncreate calculator in excelsas find duplicatesnew adodb.connectionunlock worksheetcountif function in excelexcel vba report examplehow to program vbavlookup between two valuesdml ddl sqlexcel vba reference cellawesome excel tricksexcel remove duplicate columnsmacros in databaseif iserror functionfile exist vbafso.opentextfilecombobox templatehow to open excel vbaexcel vba loop untilsort alphabetically excelusing vba functions in excelexcel duplicate sheetexcel project portfolio dashboardvba read cellleft excel vbacolumn width excel 2013excel vba programming examples pdfvba excel range selectdynamic pivot table in sql server 2008open excel file using vbahow to format rows in excelvba passwordexcel subtract two datesunhide columns in excel 2013excel adodb connectionexcel project management dashboardinsert combo box excelvlookup example excelvba excel remove duplicatesunprotect excel macroexcel macro merge cellsexcel count rows in rangedbms interview questions and answers pdf free downloadvba excel delete columnsinsert a new worksheetpastespecial xlpastevaluesdo while loop vba excelvlookup code in vbavbscript sql server connection stringinterview questions & answers for fresherstask excel templatewriting a macro in excelexcel worksheet mergehow to collapse rows in excelvba sql insertsql in vba excelvba for excel bookexcel how to unhidevba ms access tutorialhow to do vlookup in excel 2007 step by step