Using Excel VBA to Save a Workbook as a New File in the Same Folder – 3 Examples

 

Excel VBA to Save Workbook as New File in Same Folder: 3 Ideal Examples

The sample Excel workbook contains 3 worksheets: ‘VBA-1’, ‘VBA-2’ and ‘VBA-3’. Each worksheet contains the same dataset (B4:D8).

vba save workbook as new file in same folder

To save a new file in this same folder using Excel VBA:


Example 1 – Using  Excel VBA to Save the Active Workbook as a New File in the Same Folder

Steps:

  • Go to the Developer tab.
  • In Code, click Visual Basic.

Excel VBA to Save Active Workbook as New File in Same Folder

  • In the Microsoft Visual Basic for Applications window, click Insert and select Module.

  • Enter the VBA code below.
Sub saving_new_file()
Dim thisfile As Workbook
Set thisfile = ActiveWorkbook
Workbooks.Add
ActiveWorkbook.SaveAs Filename:=thisfile.Path & "\new workbook1.xlsx"
ActiveWorkbook.Close savechanges:=False
End Sub
  • Click Save.

  • In the Microsoft Excel window, click Yes to save the file as a macro-free workbook.

  • Keep the cursor inside the code and click Run.

  • A new ‘.xlsx’ file is saved in the same folder as the original one.

Excel VBA to Save Active Workbook as New File in Same Folder

  • It will not contain any data from the original book. You will get a blank workbook.

Read More: Excel VBA to Save Workbook in Specific Folder with Date


Example 2 – Save a Copy of a Workbook in the Same Folder as the Original Using VBA

Steps:

  • Open the VBA code window by following the steps described in the previous method.
  • Enter the VBA code below in the code window.
Sub saving_new_file()
Dim path As String
Dim workbookname2 As String
path = ThisWorkbook.path & "\"
workbookname2 = Range("Z40").Text
Application.DisplayAlerts = False
ActiveWorkbook.SaveCopyAs Filename:=path & workbookname2 & "new workbook2.xlsx"
Application.DisplayAlerts = True
End Sub
  • Click Save.

Save a Copy of Workbook to Same Folder as Original Using VBA

  • Click Yes in the Microsoft Excel window to save the file as ‘.xlsx’.

  • Place the cursor inside the code and click Run.

  • A new ‘.xlsx’ file is saved in the same folder as the original one.

  • It is a copy of the workbook.

Read More: How to Save a Copy as XLSX Using Excel VBA


Example 3 – Using Excel VBA to Save a Workbook in the Same Folder in the Save As Dialog Box

Steps:

  • Save the Excel Workbook (.xlsx) as an Excel Macro-Enabled Workbook (.xlsm):  open the Excel Workbook and go to the File tab.

Excel VBA Save Workbook in Same Folder with Save As Dialog Box

  • Click Save As.

  • Go to Save as type > Excel Macro-Enabled Workbook >Save.

  • Save the workbook in a folder.

  • Save a copy of the workbook in the same folder: open the VBA code window as described in the previous examples.
  • Enter the VBA code below in the code window.
Sub SavingNewFile()
Dim y1 As String
Dim y2 As Variant
y1 = "new workbook3"
y2 = Application.GetSaveAsFilename(InitialFileName:=y1, _
FileFilter:="File Type (*.xlsm), *.xlsm")
If y2 <> False Then
ActiveWorkbook.SaveAs y2
End If
End Sub
  • Click Save > place the cursor inside the code > click Run.

  • In the Save As dialog box, you can change the File name.
  • Click Save.

  • The workbook was saved in the same folder as the original.

Excel VBA Save Workbook in Same Folder with Save As Dialog Box

  • The new workbook contains all the data of the original workbook.

Read More: Excel VBA: Save Workbook in Specific Folder


Download Practice Workbook

Download the practice workbook here.


 

Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Sagufta Tarannum
Sagufta Tarannum

Sagufta Tarannum, holding a BSc in Naval Architecture & Engineering from Bangladesh University of Engineering and Technology, contributes significantly as an Excel & VBA Content Developer at ExcelDemy. Fueled by a deep interest in research and innovation, she actively engages with Excel. In her role, Sagufta not only skillfully addresses challenging issues but also demonstrates enthusiasm and expertise in gracefully navigating intricate situations, underscoring her unwavering commitment to consistently delivering exceptional content. Her interests are Advanced... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo