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.
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.
Worked perfectly! Thanks!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks