How to Use a VBA Code for ‘Save Button’ in Excel (4 Methods)

How to Insert a Macro Button and Assign a Macro

Before creating a VBA-Enabled Save Button, users must insert a Command Button and assign a Macro to it.

Inserting a Command Button

  • Go to the Developer tab > Click on Insert (in the Controls section) > Click on a Command Button (under Active X Controls).

Command Button-VBA Code for Save Button in Excel

  • Right-click on the inserted Command Button. The Context Menu appears. From the Context Menu, select Properties.

Properties

  •  In the Properties window, edit necessary items as needed, as depicted in the below image.

Modified Properties


Assigning a Macro to the Command Button

  • After inserting the Command Button and modifying its properties, right-click on it. Select the View Code option from the Context Menu.

Assigning Macro-VBA Code for Save Button in Excel

  • Excel opens the Microsoft Visual Basic Window with the Command Button code space.

Microsoft Visual Basic

  • Enter the preferred macro in the offered space, and click the Save Button to run it.

Method 1 – VBA Code for File Saving, Then Quitting Excel

Steps:

  • Enter the following macro under the Command Button:
Private Sub CommandButton1_Click()
Application.Quit
ThisWorkbook.Save
End Sub

Macro-1-VBA Code for Save Button in Excel

  • In the code, start the macro procedure by declaring the Sub name. And it’s a Command Button.
  • Save the workbook content. Quit the application.
  • Click on the inserted Save Button.

Excel saves the modified content and then terminates the active Excel window.

Click on Save Button


Method 2 – Macro Code for a ‘Save As’ Button

Steps:

  • Enter the below code in the Command Button:
Private Sub CommandButton1_Click()
With Application.FileDialog(msoFileDialogSaveAs)
.Title = ("Provide a File Name then Select File Type & Location")
.ButtonName = "Save As"
.Show
End With
End Sub

Macro-2-VBA Code for Save Button in Excel

The code’s sections,

  • Initiate the macro procedure declaring the Sub name.
  • Open the Save As dialog box using the Application With statement.

 

  • Return to the worksheet then click on the Save Button.

Click on Save Button

 

  • Excel opens the Save As dialog box saying, “Provide a File Name then Select File Type & Location”.
  • Click Save As to save the file in the desired location with a preferred name.

Save As window


Method 3 – Code for Saving a File with a Custom Name in a Fixed Location

Steps:

  •  Enter the below macro under the Command Button:
Private Sub CommandButton1_Click()
Dim mFileName As String
mFileName = Range("B5").Value
ActiveWorkbook.SaveAs (mFileName)
End Sub

Macro-3-VBA Code for Save Button in Excel

From the above image, the code’s sections,

  • Set the Sub name.
  • Declare the variable as String.
  • Assign the variable name to the B5 cell entry.
  • Save the entire workbook.
  • Click on the Save Button. Excel changes the file name to the value of the B5 cell (i.e., Sales on 3rd June’22).

Click on Save Button

This macro automatically saves the Excel file in the Device’s Document directory. Go through the directory, and you find the saved file as shown below.

Saved directory


Method 4: Saving a File with a Given Name and Location Using a Button

Steps:

  •  Enter the below code in the Command Button:
Private Sub CommandButton1_Click()
ActiveWorkbook.SaveAs _
FileName:="C:\Users\maruf\Desktop\Softeko\New folder\Save Button"
End Sub

Macro-4-VBA Code for Save Button in Excel The above image has code sections,

  • Set the Sub name.
  • Save the Excel file in the given directory.
  • Click on the Save Button to execute the macro.

Click on Save Button

Execution of the macro saves the file in the specified directory, as depicted in the below screenshot.

Saved file

You can change the directory used in the macro to save wherever you wish to save the file.


Download the Excel Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Maruf Islam
Maruf Islam

MARUF ISLAM is an excellent marine engineer who loves working with Excel and diving into VBA programming. For him, programming is like a superhero tool that saves time when dealing with data, files, and the internet. His skills go beyond the basics, including ABACUS, AutoCAD, Rhinoceros, Maxsurf, and Hydromax. He got his B.Sc in Naval Architecture & Marine Engineering from BUET, and now he's switched gears, working as a content developer. In this role, he creates techy content... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo