automatically close second workbook

  • Thread starter Thread starter swain.s
  • Start date Start date
S

swain.s

i have put the following code in to workbook 'A' to close workbook 'B' but it
does not close book 'B' there is no error message

Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Workbooks("B.XLS").Close SaveChanges:=False
ActiveWorkbook.Close SaveChanges:=True
End Sub
 
Several of us have tested your code..........both workbook_open which opens
B.xls when A.xls opens and the workbook_beforeclose code.

Works fine for me.

All I can think of is that you are opening B.xls in a separate instance of
Excel.

If that's the case, the beforeclose code in A.xls will not trigger B.xls to
close.

Try this code in A.xls

Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Workbooks("B.XLS").Close SaveChanges:=False
ActiveWorkbook.Close SaveChanges:=True
Application.Quit
End Sub

Do you still have workbook B.xls open in an instance of Excel?


Gord Dibben MS Excel MVP
 
Gord,

The last time this poster had problems like this, it was because the extension
of the file wasn't .xls. It was .xlsm or .xlsx or .xlsb or who knows what.
 
I seem to remember something like that but OP did state at that time B.xls
did open when he opened A.xls

So..........maybe the extension was correct in the open code but not in the
beforeclose code?


Gord
 
Back
Top