Welcome to the Excel Forum

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Please Register to Remove these Ads

Please Register to Remove these Ads



Reply
  #1  
Old 02-05-2010, 09:13 AM
c_leven3 c_leven3 is offline
Registered User
 
Join Date: 20 Jan 2010
Location: Atlanta, GA
MS Office Version:Excel 2007
Posts: 18
c_leven3 is becoming part of the community
Close Workbooks in Other Instances of Excel

Please Register to Remove these Ads

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 09:47 AM.
Reply With Quote
  #2  
Old 02-05-2010, 09:43 AM
romperstomper's Avatar
romperstomper romperstomper is offline
Valued Forum Contributor
 
Join Date: 04 Nov 2008
Location: The inner wardrobes of my mind
MS Office Version:95 onward (except 97)
Posts: 2,078
romperstomper Has a higher level of understanding romperstomper Has a higher level of understanding romperstomper Has a higher level of understanding romperstomper Has a higher level of understanding romperstomper Has a higher level of understanding romperstomper Has a higher level of understanding romperstomper Has a higher level of understanding
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
__________________
HTH,
R.

Bad decisions make good stories.
Reply With Quote
  #3  
Old 02-05-2010, 09:47 AM
c_leven3 c_leven3 is offline
Registered User
 
Join Date: 20 Jan 2010
Location: Atlanta, GA
MS Office Version:Excel 2007
Posts: 18
c_leven3 is becoming part of the community
Re: Close Workbooks in Other Instances of Excel

Worked perfectly! Thanks!
Reply With Quote


Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump