+ Reply to Thread
Results 1 to 20 of 20

Macro to close two open windows of the same workbook ?

  1. #1
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Macro to close two open windows of the same workbook ?

    I have a problem which I can't solve.

    The condition is :
    A. Maybe there is only one window open
    B. Maybe there are two windows open (of the same workboook, but different sheet).

    What I want is, when the user click X mark on the most top right of Excel application,
    It will close the workbook (closing right away whether there are two opened windows or one open window).

    What I've tried so far is :
    Please Login or Register  to view this content.
    But it doesn't work (on two windows open).
    After I click the X mark,
    it will close the active window first, then activate the other window.
    The code on the Private Sub above is not run yet.

    I need to click again the X mark, in order to close the workbook.
    (then the code above is redundant).

    So, my question is :
    what is the code to achieve one click of the X mark,
    then those two opened windows are close, closing the workbook ?

    Any kind of respond would be greatly appreciated.
    Thank you in advanced.
    Last edited by karmapala; 08-15-2019 at 12:26 AM.

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Macro to close two open windows of the same workbook ?

    .
    https://analysistabs.com/vba-code/workbook/m/close/

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

  3. #3
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Macro to close two open windows of the same workbook ?

    Thank you for the link, Logit.

    But I am sorry, none of both codes (below) from the link you gave, work on me.

    Please Login or Register  to view this content.
    I still have to click the X mark (on the most top-right, not the top-right of the opened windows) two times.

    Please Login or Register  to view this content.
    I still have to click the X mark (on the most top-right, not the top-right of the opened windows) two times.

    Thanks.

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Macro to close two open windows of the same workbook ?

    .
    Two thoughts ...

    There is more code in your project that is contributing to this issue. Without seeing the remainder of the code you are using it would be difficult to trouble shoot.

    Excel has become corrupted on your computer and will require a complete reinstallation of the software.

  5. #5
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Macro to close two open windows of the same workbook ?

    @Logit,

    I am sorry, do you mean that actually Excel will directly close the app
    no matter how many windows (of the same workbook) open ?

    I've tried the following :
    1. Make a new workbook (so there is no macro), Book1.
    2. Click "VIEW" tab on the menu
    3. Click "NEW WINDOW" three times.
    4. Click "ARRANGE ALL" and choose "vertical".
    4. Now there are 4 windows (of the same workbook) open :
    Book1:1, Book1:2, Book1:3 and Book1:4

    5. Click the X mark at most top-right of the Excel app.
    But it doesn't close the app directly.

    My first X-mark click causing Excel close the Book1:4
    My second X-mark click causing Excel close the Book1:3
    My third X-mark click causing Excel close the Book1:2
    Then finaly on my fourth X-mark click, Excel close the app including the workbook.

    There is more code in your project that is contributing to this issue
    From what I did above, I guess it's not because there are more codes in my workbook.

    Excel has become corrupted on your computer and will require a complete reinstallation of the software
    So I think this is the culprit which causing Excel doesn't close the app (and the workbook) directly
    when there is more than one window (of the same workbook) opened.

    FYI, if there is only one window of the workbook,
    Excel does close the app (and the workbook) directly
    soon after I click the X mark on top-right of the app.

    Thank you for your feedback, Logit.
    Last edited by karmapala; 08-24-2019 at 01:59 AM.

  6. #6
    Valued Forum Contributor
    Join Date
    12-14-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    439

    Re: Macro to close two open windows of the same workbook ?

    Hi karmapala,
    Your code didn't work because when you close a window of few workbook's windows, the workbook didn't close and the Workbook_BeforeClose event wasn't fired.
    Maybe the Workbook_WindowDeactivate event will be helpful in this case but I'm busy and I haven't time to help you now.

  7. #7
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Macro to close two open windows of the same workbook ?

    Hi huuthang_bd,

    Quote Originally Posted by huuthang_bd View Post
    Hi karmapala,
    Your code didn't work because when you close a window of few workbook's windows, the workbook didn't close
    and the Workbook_BeforeClose event wasn't fired.
    That's what I've found.

    If (for example) there are two windows (of the same workbook in my case),
    the Workbook_BeforeClose event isn't fired on the first click, but the second click of the X mark of Excel App.

    If (for example) there are four windows (of the same workbook in my case),
    the Workbook_BeforeClose event isn't fired on the 1st to 3rd click, but the 4th click of the X mark of Excel App.


    Maybe the Workbook_WindowDeactivate event will be helpful in this case but I'm busy and I haven't time to help you now.
    Just now I've tried to change the BeforeClose to WindowDeactivate, but still the same problem.
    Anyway, thank you for your explanation, huuthang_bd.
    Please take your time, no need to hurry .

  8. #8
    Valued Forum Contributor
    Join Date
    12-14-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    439

    Re: Macro to close two open windows of the same workbook ?

    Hi karmapala,
    Now i have free time and return to help you solve this thread.
    You can try this code in ThisWorkbook module.
    Please Login or Register  to view this content.

  9. #9
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Macro to close two open windows of the same workbook ?

    Hi huuthang_bd ,

    I'm sorry the code still doesn't work with me.

    Say, there are 3 windows of the same workbook open.
    In the "ThisWorkbook" module,
    I put all your codes there.
    And I put a "red stop" on the "Workbook_WindowDeactivate" line.

    So I click the X-mark of the Excel app.
    It takes me to the module, where the yellow bar stop on my "red stop".
    I push f8 to step-by-step running the code :
    Please Login or Register  to view this content.
    Soon after the yellow reach "End If" it goes to the "End Sub" then close whatever window which is active.
    Again I click the X-mark of the Excell app, push f8 to step-by-step running the code, after the end sub, it close the active window.

    It never reach to go to the below codes :

    Please Login or Register  to view this content.
    The same thing if I don't put the "red stopper" at all on the codes,
    I still have to click the X mark n times depending on how many n of the windows open.

    Actually I'm in the option whether to do :
    A. open two windows of the same workbook, but showing different sheet
    or
    B. show a Userform (listview, as in my other thread).

    But anyway, thank you once again huuthang_bd.
    Last edited by karmapala; 08-25-2019 at 11:49 PM.

  10. #10
    Valued Forum Contributor
    Join Date
    12-14-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    439

    Re: Macro to close two open windows of the same workbook ?

    Please donn't use red stop. Just simply click X mark.

  11. #11
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Macro to close two open windows of the same workbook ?

    You could put this in the ThisWorkbook code module.
    If you close any window of that workbook will close, no matter how many are open.

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  12. #12
    Valued Forum Contributor
    Join Date
    12-14-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    439

    Re: Macro to close two open windows of the same workbook ?

    Hi mikerickson,

    What happen if I only switch between windows?

  13. #13
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Macro to close two open windows of the same workbook ?

    Quote Originally Posted by huuthang_bd View Post
    Please donn't use red stop. Just simply click X mark.
    The same thing if I don't put the "red stopper" at all on the codes,
    I still have to click the X mark n times depending on how many n of the windows open, huuthang_bd.

    ANyway, thank you for your help, huuthang_bd
    Last edited by karmapala; 08-27-2019 at 02:32 PM.

  14. #14
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Macro to close two open windows of the same workbook ?

    Quote Originally Posted by mikerickson View Post
    You could put this in the ThisWorkbook code module.
    If you close any window of that workbook will close, no matter how many are open.

    Please Login or Register  to view this content.
    It throw me error 1004 --->

    First condition, there are 3 windows (of the same workbook open).
    Then I paste your code to the workbook module.
    I click the X-mark
    it ask "Do you want to save blablabla ?"
    I click the SAVE button.
    The save dialog box open
    I fill the name, then click OK
    Then it throw me an error 1004 ---> "close method of the workbook class failed"
    I click END
    Then it close one of the window. (two windows left).
    I click the X-mark again
    It throw me the same error.
    I click END.
    Then it close one of the window. (one window left).
    I click the X-mark again.
    It throw me the same error.
    I click END.
    Finally the Excel app (and the workbook) close.

    If I activate any of the opened window, it throw me the same error.
    When the code already in the module, if I click "New Window" from the Excel VIEW menu,
    it also throw me the same error.

    Anyway, thank you for your respond, mike.

  15. #15
    Registered User
    Join Date
    07-15-2019
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2016
    Posts
    42

    Re: Macro to close two open windows of the same workbook ?

    Hi, i am a noob to excel but i am good at googling! see if this link helps out at all:

    HTML Code: 

  16. #16
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Macro to close two open windows of the same workbook ?

    Hi there,

    See if the attached workbook does what you need.


    It uses the following code in a VBA Class Module called "C01_WindowEvent":

    Please Login or Register  to view this content.

    and the following code in the "ThisWorkbook" VBA CodeModule:

    Please Login or Register  to view this content.

    I opened the workbook and created several views of it in new windows. The various windows could be activated/deactivated as required, but clicking once on the "X" button on the Excel TitleBar caused all associated windows to close.


    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  17. #17
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Macro to close two open windows of the same workbook ?

    Hi Greg M,
    Thank you, thank you very much indeed, Greg.
    Your code and your sample workbook work like a dream.
    Really appreciate it.

  18. #18
    Valued Forum Contributor
    Join Date
    11-04-2018
    Location
    Denpasar
    MS-Off Ver
    Excel 2010
    Posts
    777

    Re: Macro to close two open windows of the same workbook ?

    Quote Originally Posted by dylanmoran View Post
    Hi, i am a noob to excel but i am good at googling! see if this link helps out at all:
    You are very right

    Tip #1: Shift + Close Window Button
    HTML Code: 
    Thank you very much, dylanmoran.

  19. #19
    Registered User
    Join Date
    07-15-2019
    Location
    Rochester, NY
    MS-Off Ver
    Excel 2016
    Posts
    42

    Re: Macro to close two open windows of the same workbook ?

    Hope it worked/helped! Haha, you're welcome!

  20. #20
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Macro to close two open windows of the same workbook ?

    Hi again,

    Many thanks for your feedback and also for the Reputation increase - much appreciated!

    You're welcome - glad I was able to help.

    Best regards,

    Greg M

+ 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. Windows task scheduler to automatically open(then close) an excel workbook
    By aprildu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-15-2018, 11:49 PM
  2. Replies: 2
    Last Post: 11-05-2017, 01:18 PM
  3. [SOLVED] Issue with Open, copy, close Macro - when trying to close workbook Clipboard error
    By seanpcorbett1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-13-2016, 05:14 PM
  4. [SOLVED] Macro to open/close/save workbook
    By JonathanB2 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-14-2014, 10:51 AM
  5. How to open,save and close another workbook from the current workbook using macro?
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-15-2012, 10:42 AM
  6. Macro to close workbook open in IE?
    By martin.mcgarry in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-05-2010, 02:13 PM
  7. Macro to detect if workbook is open Getting close!
    By LAWDAWG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-20-2009, 12:26 AM

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