+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    01-20-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Close Workbooks in Other Instances of Excel

    I have one main workbook that, when opened, opens three other workbooks each in its own instance of Excel. I'd like to know how to close all three of these upon closing this main workbook.

    Code:
    Private Sub Workbook_Open()
        Dim wb As String
        Dim wbpath As String
        Dim NameA As String
        Dim NameB As String
        Dim NameC As String
        
        Application.WindowState = xlMaximized
        
        NameA = "Name_of_My_First_Workbook"
        NameB = "Name_of_My_Second_Workbook"
        NameC = "Name_of_My_Third_Workbook"
        
        wb = ActiveWorkbook.name
        wbpath = Workbooks(wb).path
        Dim xls As New Excel.Application
        
        xls.Workbooks.Open (wbpath & "\" & NameA  & ".xlsm")
        xls.Visible = True
        ActiveWindow.WindowState = xlNormal
    
        Dim xls2 As New Excel.Application
        xls2.Workbooks.Open (wbpath & "\" & NameB & ".xlsm")
        xls2.Visible = True
        ActiveWindow.WindowState = xlNormal
        
        Dim xls3 As New Excel.Application
        xls3.Workbooks.Open (wbpath & "\" & NameC  & ".xlsm")
        xls3.Visible = True
        ActiveWindow.WindowState = xlNormal
    End Sub
    Last edited by c_leven3; 02-05-2010 at 10:47 AM.

  2. #2
    Forum Moderator romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Alibi
    MS-Off Ver
    All
    Posts
    8,262

    Re: Close Workbooks in Other Instances of Excel

    Use variables to keep track of them:
    Code:
    Option Explicit
    Dim xls(1 To 3) As Excel.Application
    Dim wbk(1 To 3) As Excel.Workbook
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Dim blnSaveChanges As Boolean
        Dim n As Long
        ' amend to True if you want to save before closing
        blnSaveChanges = False
        
        For n = 1 To 3
            wbk(n).Close blnSaveChanges
            xls(n).Quit
        Next n
    End Sub
    
    Private Sub Workbook_Open()
        Dim wb As String
        Dim wbpath As String
        Dim NameA As String
        Dim NameB As String
        Dim NameC As String
        
        Application.WindowState = xlMaximized
        
        NameA = "Name_of_My_First_Workbook"
        NameB = "Name_of_My_Second_Workbook"
        NameC = "Name_of_My_Third_Workbook"
        
        wb = ActiveWorkbook.Name
        wbpath = Workbooks(wb).Path
        Set xls(1) = New Excel.Application
        
        Set wbk(1) = xls(1).Workbooks.Open(wbpath & "\" & NameA & ".xlsm")
        xls(1).Visible = True
        ActiveWindow.WindowState = xlNormal
    
        Set xls(2) = New Excel.Application
        Set wbk(2) = xls(2).Workbooks.Open(wbpath & "\" & NameB & ".xlsm")
        xls(2).Visible = True
        ActiveWindow.WindowState = xlNormal
        
        Set xls(3) = New Excel.Application
        Set wbk(3) = xls(3).Workbooks.Open(wbpath & "\" & NameC & ".xlsm")
        xls(3).Visible = True
        ActiveWindow.WindowState = xlNormal
    End Sub
    So long, and thanks for all the fish.

  3. #3
    Registered User
    Join Date
    01-20-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Close Workbooks in Other Instances of Excel

    Worked perfectly! Thanks!

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