+ Reply to Thread
Results 1 to 2 of 2

Close other workbooks on open

Hybrid View

  1. #1
    Registered User
    Join Date
    03-26-2014
    Location
    Washington
    MS-Off Ver
    Excel 2010
    Posts
    61

    Close other workbooks on open

    This may be a simple question, but I can't figure out the code for it. When a user opens the workbook I am building I want it to see if other excel files are open and prompt a warning that no other excel files can be open while accessing my workbook. If they click continue then all the files will be closed and the active one will be the only file open. If they choose to not continue then only my tool closes. Also, if possible I would like the warnings to pop up with the application not visible and then regardless of the choice the application is visible again.

    As I said this may be something simple. Thanks for any help provided.

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

    Re: Close other workbooks on open

    Hi there,

    See if the attached workbook does what you need.

    It uses the following code which should be inserted into the "ThisWorkbook" VBA CodeModule of your workbook:


    
    
    Option Explicit
    
    
    Private Sub Workbook_Open()
    
        Dim iYesNo  As Integer
        Dim wbk     As Workbook
    
        Windows(Me.Name).Visible = False
    
        If Workbooks.Count > 1 Then
    
              iYesNo = MsgBox("No other workbooks may be open while workbook """ & _
                               Me.Name & """ is open" & _
                               vbLf & vbLf & _
                              "Do you want to close ALL open workbooks?" & _
                               vbLf & _
                              "(Any changes to open workbooks will be saved " & _
                              "automatically before the workbooks are closed)", _
                               vbYesNo + vbQuestion, "Other workbooks are open")
    
              If iYesNo = vbYes Then
    
                    For Each wbk In Workbooks
    
                        If Not wbk Is Me Then
                            wbk.Close SaveChanges:=True
                        End If
    
                    Next wbk
    
                    Windows(Me.Name).Visible = True
                    Me.Saved = True
    
              Else: MsgBox "The workbook """ & Me.Name & """ will now be closed", _
                            vbInformation, "Application not available"
    
                    Me.Close SaveChanges:=False
    
              End If
    
        Else: Windows(Me.Name).Visible = True
              Me.Saved = True
    
        End If
    
    End Sub

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

    Regards,

    Greg M
    Attached Files Attached Files

+ 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] Open workbooks update the main one and close the other workbooks
    By stojko89 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2013, 07:12 AM
  2. [SOLVED] Macro to close all open workbooks except one
    By aetedford in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-26-2013, 10:21 AM
  3. [SOLVED] Open Multiple Workbooks, Record names, Copy paste to Active Workbook, Close the Workbooks
    By vba_madness in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-24-2013, 06:09 AM
  4. How to close open workbooks from an application get open filename call ?
    By leanne2011 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-10-2011, 10:41 PM
  5. Open close workbooks and Excel
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-02-2011, 03:52 PM
  6. close all open workbooks except the active one
    By Dav in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-05-2006, 08:30 PM
  7. [SOLVED] Open Close workbooks
    By bbc1 in forum Excel General
    Replies: 2
    Last Post: 08-28-2005, 07:05 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