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

Updated on December 4, 2019
THEAMAZINGJAMES profile image

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

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

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

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, turbofuture.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://maven.io/company/pages/privacy

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)