How to Create a Macro Button That Saves & Closes an MS Excel Workbook
An Excel Worksheet With a Button
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
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
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
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.
After the code is entered be sure to click on the save button.
Visual Basic Structure
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
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.
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