+ Reply to Thread
Results 1 to 17 of 17

How to close the active workbook in Excel 2011?

  1. #1
    Registered User
    Join Date
    07-11-2009
    Location
    South Gippsland, Australia
    MS-Off Ver
    Excel 2011 (Mac)
    Posts
    31

    How to close the active workbook in Excel 2011?

    I'm using VBA in Excel 2011 on a Mac in macOS 10.13.4 (High Sierra). I can't find the right code to close the file in which the macro resides - I keep getting an error message, RunTime error 1004 based on the close command. I've tried:

    ActiveWorkbook.Close
    Workbook.Close
    ThisWorkbook.Close

    and they all fail. What should I be doing?

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: How to close the active workbook in Excel 2011?

    Hmm. The first and third should work, but I have never done this on a Mac. Could you attach the file so we can see if it works in Windows?

    Alternatively, try the attached. This works for me. If it causes an error for you then it may be that the Mac version will not allow you to close a file with a running macro.
    Attached Files Attached Files
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    09-26-2012
    Location
    Australia
    MS-Off Ver
    2016 All
    Posts
    1

    Re: How to close the active workbook in Excel 2011?

    You are leaving out an argument. You have to specifiy whether or not you want the book to Save
    So it should be
    ActiveWorkbook.Close False ' if you want to close without saving and

    ActiveWorkbook.Close True 'in the other case.

  4. #4
    Registered User
    Join Date
    07-11-2009
    Location
    South Gippsland, Australia
    MS-Off Ver
    Excel 2011 (Mac)
    Posts
    31

    Re: How to close the active workbook in Excel 2011?

    I assume I should click in the 'Close Me' box. Doing so just put handles on the box.

    'Application.Quit' works but it's not the solution I'd prefer.
    Last edited by audax; 11-28-2018 at 01:14 AM.

  5. #5
    Registered User
    Join Date
    07-11-2009
    Location
    South Gippsland, Australia
    MS-Off Ver
    Excel 2011 (Mac)
    Posts
    31

    Re: How to close the active workbook in Excel 2011?

    I tried the 'False' argument on each option - no difference.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,366

    Re: How to close the active workbook in Excel 2011?

    @DonDiego: strictly speaking, the argument is optional.

    See: https://docs.microsoft.com/en-us/off...workbook.close

    Syntax
    expression. Close( _SaveChanges_ , _Filename_ , _RouteWorkbook_ )
    expression A variable that represents a Workbook object.

    Parameters

    Name Required/Optional Data type Description
    SaveChanges Optional Variant If there are no changes to the workbook, this argument is ignored. If there are changes to the workbook and the workbook appears in other open windows, this argument is ignored. If there are changes to the workbook but the workbook doesn't appear in any other open windows, this argument specifies whether changes should be saved. If set to True , changes are saved to the workbook. If there is not yet a file name associated with the workbook, then FileName is used. If Filename is omitted, the user is asked to supply a file name.

    Filename Optional Variant Save changes under this file name.
    Last edited by TMS; 11-28-2018 at 10:27 AM. Reason: Thanks to 6SJ for encouraging me to complete my sentences ...
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  7. #7
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: How to close the active workbook in Excel 2011?

    Is there any code in the workbook_beforeclose event in the ThisWorkbook module?
    Rory

  8. #8
    Registered User
    Join Date
    07-11-2009
    Location
    South Gippsland, Australia
    MS-Off Ver
    Excel 2011 (Mac)
    Posts
    31

    Re: How to close the active workbook in Excel 2011?

    Yes:
    Please Login or Register  to view this content.
    Last edited by 6StringJazzer; 11-28-2018 at 10:10 AM.

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: How to close the active workbook in Excel 2011?

    Quote Originally Posted by DonDiego View Post
    You are leaving out an argument. You have to specifiy whether or not you want the book to Save
    Quote Originally Posted by TMS View Post
    @DonDiego: strictly speaking, the
    I believe TMS intended to say that strictly speaking the argument is optional.

  10. #10
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: How to close the active workbook in Excel 2011?

    Moderator's note: audax, please take the time to review our rules. There aren't many, and they are all important. Rule #2 requires code tags. I have added them for you this time because you don't have many posts, even though you have been a member for 9 years. --6StringJazzer

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: How to close the active workbook in Excel 2011?

    Quote Originally Posted by rorya View Post
    Is there any code in the workbook_beforeclose event in the ThisWorkbook module?
    Quote Originally Posted by audax View Post
    Yes:
    audax, the question was specifically about the workbook_beforeclose event, which is not shown in your code. Do you have the following in the ThisWorkbook module:

    Please Login or Register  to view this content.

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,366

    Re: How to close the active workbook in Excel 2011?

    @6SJ: exactly so Thank you for the spot.
    Last edited by TMS; 11-28-2018 at 10:29 AM.

  13. #13
    Registered User
    Join Date
    07-11-2009
    Location
    South Gippsland, Australia
    MS-Off Ver
    Excel 2011 (Mac)
    Posts
    31

    Re: How to close the active workbook in Excel 2011?

    Quote Originally Posted by 6StringJazzer View Post
    audax, the question was specifically about the workbook_beforeclose event, which is not shown in your code. Do you have the following in the ThisWorkbook module:

    Please Login or Register  to view this content.
    No - I have never previously encountered the 'Workbook_BeforeClose' structure. Where should it be located and what does it do?

  14. #14
    Registered User
    Join Date
    07-11-2009
    Location
    South Gippsland, Australia
    MS-Off Ver
    Excel 2011 (Mac)
    Posts
    31

    Re: How to close the active workbook in Excel 2011?

    Quote Originally Posted by 6StringJazzer View Post
    Moderator's note: audax, please take the time to review our rules. There aren't many, and they are all important. Rule #2 requires code tags. I have added them for you this time because you don't have many posts, even though you have been a member for 9 years. --6StringJazzer
    My humblest apologies - I hadn't realised they were obligatory. And, although I've been a member for nine years, I've written no VBA code in that time which is why I'm now exhibiting great confusion and asking very naive questions.

  15. #15
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: How to close the active workbook in Excel 2011?

    Quote Originally Posted by audax View Post
    No - I have never previously encountered the 'Workbook_BeforeClose' structure. Where should it be located and what does it do?
    If you don't already have it, you don't need it. It's code that executes just before the file closes. If you did have it, it might mean that the error was happening in there someplace.

    Did you try running my sample file? Click the button and the file will close. If that works with no error, then we can dig farther into your code.

    Is the sub you showed us running on the workbook that contains it, or some other workbook? That is, when this code runs what is ActiveWorkbook?

  16. #16
    Registered User
    Join Date
    07-11-2009
    Location
    South Gippsland, Australia
    MS-Off Ver
    Excel 2011 (Mac)
    Posts
    31

    Re: How to close the active workbook in Excel 2011?

    All solved thank you. One of the purposes of the exercise was to use a .xlsm file to open a new .xlxs file, import a text file and do a bit of fiddling with it. I discovered that I can close the .xlsm file if I have another file open so all is well.

    It appears certain to me that it is impossible to close the last file standing in VBA on a Mac running High Sierra.

    Mark this issue as solved.

  17. #17
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: How to close the active workbook in Excel 2011?

    If your question has been answered please mark your thread as "Solved" so that members will know by looking at the thread title that your problem is solved. Go to the menu immediately above your first post to the thread and click on Thread Tools. From the dropdown menu select "Mark this thread as solved..."

    If a member helped you solve your problem, consider adding to their reputation by clicking on the star icon below their name.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro to close all workbooks except active workbook and one other named workbook
    By picton2000 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-01-2017, 06:03 PM
  2. [SOLVED] Close Active workbook to different folder
    By Charles in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-28-2016, 01:55 PM
  3. VBA Code to close non-active workbook crashes excel
    By mwgates81 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-08-2015, 06:32 PM
  4. [SOLVED] Edit code to close all workbooks except active workbook and other specific workbook
    By rocksan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2012, 09:29 PM
  5. Will not auto Close down workbook when screensaver active
    By jeffwest2 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-11-2009, 10:40 AM
  6. [SOLVED] Delete (kill) active workbook on close: Possible?
    By BEEJAY in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-17-2006, 02:15 PM
  7. Replies: 0
    Last Post: 06-10-2005, 01:05 PM

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.6.0 RC 1