How to Create a Macro Button That Saves & Closes an MS Excel Workbook - TurboFuture - Technology
Updated date:

How to Create a Macro Button That Saves & Closes an MS Excel Workbook

James likes to learn about technology and share what he learns through his articles.

An Excel Worksheet With a Button

Utilizing a macro button can save you a great deal of time for repetitive tasks in Microsoft Excel. These buttons are easy to program by a beginner Excel user and can be created for various operations.

Utilizing a macro button can save you a great deal of time for repetitive tasks in Microsoft Excel. These buttons are easy to program by a beginner Excel user and can be created for various operations.

Introduction

Before attempting this tutorial, please be sure the developer tab is visible in Microsoft Excel. If you don’t know how to add the tab find the instructions here. The illustration below will give you an idea of where you can find this tab. Understand that the developer tab is by default not displayed in Excel. Adding this functionality allows you to work with macros, add-ins and XML related tasks.

After creating a useful Excel workbook saving and closing a workbook is inevitable. If you create a workbook to be used for a long period of time, why not have a button that will save your work? This would save you a step and keep that annoying pop up window that asks if you want to save from appearing.

The Developer Tab Location

It only takes a few minutes to activate the developer tab. Even if you do not plan on doing much developer related work, this tab is good to have available so you can continue to improve your Microsoft Excel knowledge base.

It only takes a few minutes to activate the developer tab. Even if you do not plan on doing much developer related work, this tab is good to have available so you can continue to improve your Microsoft Excel knowledge base.

Inserting a Macro Button Into a Worksheet

Step one to creating a save and close button is finding and clicking on the developer tab. After doing so, you will find a section in the ribbon called the controls sections. Click on the insert button in this controls section. A lot of icons will become available for selection. We are interested in the form control button, so in the form controls section I want to select it. This is located in the first row of the first column.

Selecting the Form Control Button

The form control and ActiveX control command buttons have some similarities in their appearance and function.

The form control and ActiveX control command buttons have some similarities in their appearance and function.

Create the Button

Now that the button is selected, with the cursor, drag a rectangle or square somewhere on the Excel worksheet by holding down the left mouse button. After you stop dragging, an assign macro window will appear. Here the macro can be renamed for better reference. Click on new and another window will appear. In this window you need to write the code that tells the button to save the workbook before it closes.

Size and Name the Button

create-a-macro-button-that-saves-closes-a-ms-excel-workbook

Adding Visual Basic Code

Here is your chance to practice writing visual basic code. The text below can be pasted into the module window exactly as it appears below. Before doing so, make sure to delete any existing code in the module. The first and last line should already exist in the module widow.

Sub Button2_Click()
Application.Quit
ThisWorkbook.Save
End Sub

After the code is entered be sure to click on the save button.


Visual Basic Structure

Line one the of code "Sub Button2_Click()" is intended to initiate the task. After the button gets clicked the second line "Application.Quit" avoids a prompt because the line "ThisWorkbook.Save" saves the workbook. The module ends with "End Sub"

Line one the of code "Sub Button2_Click()" is intended to initiate the task. After the button gets clicked the second line "Application.Quit" avoids a prompt because the line "ThisWorkbook.Save" saves the workbook. The module ends with "End Sub"

Saving the Workbook With a Macro

If the workbook was not saved before the previous step, you will be prompted to save the workbook. Keep in mind that in order for macros to operate the workbook must be saved as an Excel Macro-Enabled Workbook.

Macro-Enabled Workbook Option

Saving the workbook as Macro-Enabled is the only way a macro button will work in a workbook. Understand that you will be prompted to enable the macro whenever the workbook is reopened.

Saving the workbook as Macro-Enabled is the only way a macro button will work in a workbook. Understand that you will be prompted to enable the macro whenever the workbook is reopened.

Testing the Macro Button

At this point, button can be tested. A save test can be made to ensure that the button is working properly. Before testing the button, make sure the button is not selected. This can be completed by clicking in a cell. Now to test the button add to text to any cell without saving, then click on the save and close button. The workbook should close without any prompts whatsoever. When the workbook is opened again make sure that the text that you entered saved.

Designing the Macro Button

The button design can be modified by right clicking on it and clicking on format control. You will be given the option to change several properties of the button such as the text style, color, and size. You also have the option to change the appearance as well.

Editing Text

create-a-macro-button-that-saves-closes-a-ms-excel-workbook

Additional Resource

If you need additional reference on how to create a save and close button in Microsoft Excel try following along to the video below.

How to Add a Macro Button That Saves and Closes an Excel Workbook

This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.

© 2019 James Smith