+ Reply to Thread
Results 1 to 7 of 7

Close a second workbook from the Wrokbook BeforeClose

  1. #1
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Close a second workbook from the Wrokbook BeforeClose

    I cannot make this work in XL2010.

    I am opening a second workbook in ReadOnly in the Workbook Open and Closing it in the Workbook BeforeClose.
    I don't EVER want to save changes on either workbook.
    I need to leave excel open as the macro created a 3rd workbook which the user will need.
    TAI



    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Close a second workbook from the Wrokbook BeforeClose

    What error are you getting?
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Close a second workbook from the Wrokbook BeforeClose

    The Public variable (Public dbWb As Workbook) should be declared in a Standard code module (e.g. Module1) and not in a worksheet or workbook code module.

    This should work.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Re: Close a second workbook from the Wrokbook BeforeClose

    Public variable IS in standard code module

    Please Login or Register  to view this content.
    does not close the workbook. It should but it doesn't!


    I don't get any error message, it closes the workbook containing the code and it leaves the wbDb open


    dbWb.Activate this does NOT activate the dbWb
    dbWb.Close This ACTIVATES IT BUT DOES NOT CLOSE IT

  5. #5
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Re: Close a second workbook from the Wrokbook BeforeClose

    It has to be a setting that I've made elsewhere in the code because when I put just these routines in a blank workbook it works fine.

    But what could I have done to make it ignore the code? The BeforeClose Event IS firing when I step thru



    Even in the original workbook, the CloseDb works WHEN NOT CALLED FROM THE Workbook BeforeClose
    Last edited by carsto; 05-27-2014 at 01:32 PM.

  6. #6
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Re: Close a second workbook from the Wrokbook BeforeClose

    The BeforeClose Event will close BOTH workbooks if I have not run any other code.
    When I run the following code, to force an error to GoTo QuitNow and close the closing of the 2nd workbook fails.

    Please Login or Register  to view this content.
    How can I close both workbooks upon an error but NOT close Excel, as I have a 3rd workbook that the user needs.

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Close a second workbook from the Wrokbook BeforeClose

    Maybe throw in an Err.Clear command. Just a guess.

    Please Login or Register  to view this content.

+ 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. Disable close workbook but still force users to close thru command button
    By rathig in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2013, 07:29 AM
  2. Macro not run in Share Wrokbook
    By Abdul Haneef in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-21-2012, 02:04 PM
  3. Workbook BeforeClose Question
    By mik-ee in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-21-2011, 10:29 AM
  4. Cancel Close outside BeforeClose Event
    By yawnzzzz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-01-2011, 11:43 AM
  5. [SOLVED] Workbook Open/BeforeClose Problem
    By Ozzy in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-01-2005, 12:25 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