automatically close second workbook

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
 
G

Gord Dibben

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
 
D

Dave Peterson

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.
 
G

Gord Dibben

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
 
D

Dave Peterson

Or he didn't share the details of the changes that made it work.

(Just my bet.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top