I have created an Excel spreadsheet, sample5a.xlsm with an Worksheet_Open subroutine. This subroutine opens another Excel file.The macro does open the 2nd file, sample5b.xlsm. I would like to close the first file without saving it. I cannot formulate a macro to close this file. Both files stay open.I have tried the macros shown below. I have identified the excel files and macros below.
SAMPLE5A MACROS
ThisWorkbook - 1
Sub Workbook_Open()
Callsecondfile
' Workbooks(1).Close didn't work
' Windows("sample5a.xlsm").Activate
' Windows("sample5b.xlsm").Select
' Windows("sample5a.xlsm").Close didn't work
' Application.Workbooks(1).Close
' ActiveWorkbook.Close
End Sub
Module1 - 1
Sub Callsecondfile()
Dim secondfilename As String
secondfilename = "c:\users\keyca\onedrive\documents\Microsoft Excel\sample5b.xlsm"
MsgBox secondfilename
Workbooks.Open secondfilename
MsgBox "if program displays this message, this macro is still working after opening sample5b"
' Application.Workbooks(1).Close
trialmacro
End Sub
Module2 - 1
Sub trialmacro()
'
' trialmacro Macro
'
'
Windows("SAMPLE5B.xlsm").Activate
Windows("SAMPLE5A.xlsm").Activate
Windows("SAMPLE5B.xlsm").Activate
' ActiveWorkbook.Close
Workbooks("sample5a.xlsm").Close
End Sub
SAMPLE5B MACROS
ThisWorkbook - 1
Sub Workbook_Open()
workonsample5a
MsgBox "workonsample5a should be complete"
End Sub
Module1 - 1
Sub workonsample5a()
Application.Workbooks(1).Activate
Application.Workbooks(1).Close
MsgBox "if sample5b macro gets to here, sample5a workbook should close"
End Sub
SAMPLE5A MACROS
ThisWorkbook - 1
Sub Workbook_Open()
Callsecondfile
' Workbooks(1).Close didn't work
' Windows("sample5a.xlsm").Activate
' Windows("sample5b.xlsm").Select
' Windows("sample5a.xlsm").Close didn't work
' Application.Workbooks(1).Close
' ActiveWorkbook.Close
End Sub
Module1 - 1
Sub Callsecondfile()
Dim secondfilename As String
secondfilename = "c:\users\keyca\onedrive\documents\Microsoft Excel\sample5b.xlsm"
MsgBox secondfilename
Workbooks.Open secondfilename
MsgBox "if program displays this message, this macro is still working after opening sample5b"
' Application.Workbooks(1).Close
trialmacro
End Sub
Module2 - 1
Sub trialmacro()
'
' trialmacro Macro
'
'
Windows("SAMPLE5B.xlsm").Activate
Windows("SAMPLE5A.xlsm").Activate
Windows("SAMPLE5B.xlsm").Activate
' ActiveWorkbook.Close
Workbooks("sample5a.xlsm").Close
End Sub
SAMPLE5B MACROS
ThisWorkbook - 1
Sub Workbook_Open()
workonsample5a
MsgBox "workonsample5a should be complete"
End Sub
Module1 - 1
Sub workonsample5a()
Application.Workbooks(1).Activate
Application.Workbooks(1).Close
MsgBox "if sample5b macro gets to here, sample5a workbook should close"
End Sub