Consulting

Results 1 to 4 of 4

Thread: Command Button to save & close workbook

  1. #1
    VBAX Newbie
    Joined
    Aug 2005
    Location
    In a house.
    Posts
    2
    Location

    Command Button to save & close workbook

    Hello everyone -
    Hope you are all doing well on this fine day. I am sure this is posted somewhere - however I am in a rush.
    I would like to create a command button that will save & close a workbook. Nothing major - my brain is malfunctioning today. Yesterday was my birthday - so I have an excuse - a senior moment.

  2. #2
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    in its simplest form

    ThisWorkbook.save
    Application.Quit

  3. #3
    VBAX Newbie
    Joined
    Aug 2005
    Location
    In a house.
    Posts
    2
    Location
    Thanks very much - that was quite simple. However, I realized that this would not be enough. I am not very VBA-savvy. Here is what I really need done. I need to disable any other method of closing the workbook - except this one.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try the following
    In the Workbook module


    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Cancel = Not CheckClose
    End Sub

    In a Standard module


    Public CheckClose As Boolean
    Sub DoClose()
    CheckClose = True
    ThisWorkbook.Save
    ThisWorkbook.Close
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •