+ Reply to Thread
Results 1 to 6 of 6

Thread: Is there code that will prevent the workbook being closed.

  1. #1
    Registered User
    Join Date
    08-17-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Is there code that will prevent the workbook being closed.

    Hi all
    I'm trying to run a macro that will ensure some variables are met and won't allow the workbook to close if not. I know how to run the macro on close, but is there a code to stop the workbook from closing then?
    Thanks
    Alan
    Last edited by alanb1976; 01-23-2012 at 11:37 AM.

  2. #2
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Is there code that will prevent the workbook being closed.

    The before close Event has a cancel argument, you can set this to true to stop the workbook closing
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  3. #3
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,233

    Re: Is there code that will prevent the workbook being closed.

    Try "cancel", no quotes if your conditions are not met

  4. #4
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel 2007
    Posts
    825

    Re: Is there code that will prevent the workbook being closed.

    as an option
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    '''''''change here for yr condition
    If Range("A1") < 1000 Then
     MsgBox "The total in A1 must be at least 1000!" _
     & " Please complete worksheet before closing.", vbExclamation, "Warning!"
     Cancel = True
     End If
     End Sub
    Regards, John

  5. #5
    Registered User
    Join Date
    08-17-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Is there code that will prevent the workbook being closed.

    Thanks. That worked

  6. #6
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel 2007
    Posts
    825

    Re: Is there code that will prevent the workbook being closed.

    you're welcome!

    If your problem is solved, please say so clearly, and mark your thread as Solved:
    Click the Edit button on your first post in the thread,
    Click Go Advanced, select [SOLVED] from the Prefix dropdown,
    then click Save Changes.
    Regards, John

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0