Excel VBA Save as File Format (12 Suitable Examples)

To illustrate how to Save as in various ways using VBA, we’ll use the following dataset containing the Salesman, Product, and Net Sales data of a company.

Excel VBA Save as File Format


Example 1 – Save as Excel File

Let’s start with a simple VBA Code to save an Excel file.

STEPS:

  • Go to the Developer tab.
  • Select Visual Basic.

VBA to Save as an Excel File

As a result, the VBA window will pop out.

  • Click Insert.
  • Select the Module.

VBA to Save as an Excel File

The Module window will appear.

  • Copy the following code and paste it into the box:
Sub Example_1()
ActiveWorkbook.SaveAs
End Sub

  • Run the code by pressing F5.
  • Input the file name, format, and other info as requested.

The file will be saved in your specified location.

Read More: Excel VBA to Save as File Using Path from Cell


Example 2 – Specify the File Extension

In the previous example, we had to manually specify the File Format after pressing the run command, but we can also specify the file extension after the file name in our code.

Insert the below code in the Module window:

Sub Example_2()
Dim location As String
location = "D:\SOFTEKO\excel vba save as file format.xlsm"
ActiveWorkbook.SaveAs Filename:=location
End Sub

Specify File Extension with Excel VBA

Run the code to save the file in the desired format and location.

To save the file in the xlsx Format, type xlsx instead of xlsm.

Read More: How to Use Macro to Save Excel File with New Name


Example 3 – Use File Format Code

We can input the File Format Code Number instead of specifying the file extension. Some useful codes are: .xlsx = 51, .xlsm = 52, .xlsb = 50, .xls = 56.

Copy the following code and paste it into the Module box:

Sub Example_3()
Dim location As String
location = "D:\SOFTEKO\excel vba save as file format"
ActiveWorkbook.SaveAs Filename:=location, FileFormat:=52
End Sub

Excel VBA to Use File Format Code

Read More: How to Save Excel Macro Files as Filename from Cell Value 


Example 4 – Save in the Same Directory

It’s a simple process to Save into the Same Directory as the existing file.

Insert the following code in the Module window:

Sub Example_4()
ActiveWorkbook.SaveAs Filename:="excel vba save as file format"
End Sub

Save in Same Directory with VBA


Example 5 – Save to a New Directory

Enter the following code in the Module box and run it.

Sub Example_5()
Dim location As String
location = "D:\SOFTEKO\excel vba save as file format"
ActiveWorkbook.SaveAs Filename:=location
End Sub

VBA to Store in New Directory


Example 6 – Ask for Password to Open Excel File

We can ask for a Password to Open Excel File.

Insert and run the following code in the Module window:

Sub Example_6()
ActiveWorkbook.SaveAs _
Filename:="D:\SOFTEKO\excel vba save as file format.xlsm", Password:="one"
End Sub


Example 7 – Add a Password for Editing

Moreover, you can ask for a Password for Editing when opening the saved file in Excel. Without the password, it’ll only open in read-only format.

Copy the following code and paste it in the Module window, then run the code:

Sub Example_7()
ActiveWorkbook.SaveAs _
Filename:="D:\SOFTEKO\excel vba save as file format.xlsm", WriteRes:="one"
End Sub


Example 8 – Open in Read-only Format

Enter the following code in the Module window and press F5 to run it:

Sub Example_8()
ActiveWorkbook.SaveAs _
Filename:="D:\SOFTEKO\excel vba save as file format.xlsm", _
ReadOnlyRecommended:=True
End Sub


Example 9 – Generate a ‘Save As’ Dialog Box

Insert the following code in the Module window and run it:

Sub Example_9()
Application.GetSaveAsFilename
End Sub


Example 10 – Create & Save a New Workbook

In addition to saving a file, we can also Create & Save New Workbook with VBA code.

Enter the following code in the Module window and press F5 to run it:

Sub Example_10()
Dim book As Workbook
Set book = Workbooks.Add
Application.DisplayAlerts = False
book.SaveAs Filename:="D:\SOFTEKO\excel vba save as file format.xlsm"
Application.DisplayAlerts = True
End Sub

VBA to Create & Save New Workbook


Example 11 – Save the Active Workbook

Likewise, we can save the active workbook where it’s already stored.

Simply insert the following very simple code in the Module window and press F5 to run it:

Sub Example_11()
ActiveWorkbook.Save
End Sub


Example 12 – Save as PDF Format

We can use the PDF file extension to save in PDF Format.

Enter the following code in the Module window, and run it by pressing F5:

Sub Example_12()
ActiveSheet.SaveAs Filename:="excel vba save as file format.pdf"
End Sub

VBA to Save as PDF Format in Excel

Read More: Excel Macro to Save as PDF


Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Aung Shine
Aung Shine

Aung Shine completed his bachelor’s in Electrical and Electronics Engineering from Bangladesh University of Engineering and Technology. It has been almost 2 years since he joined SOFTEKO and actively working on the ExcelDemy project. Currently he works as a Team Leader where he guides his team members to create technical content. He has published 150+ articles and reviewed 50+ articles. He has also solved various user problems before. He has interests in Data Analysis, Power Query, Advanced Excel,... Read Full Bio

1 Comment
  1. Sorry for my limited knowledge. You should show the result for each VBA….Thanks.
    Right now, I have some problem on excel in window 11.
    ‘Text to column’ is necessary after Copy & Paste.
    I do not want to do ‘text to column’ manually.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo