ChingLu Tay
ChingLu Tay

Reputation: 49

Auto Close Workbook

I have 2 workbooks. Workbook A & B.

When workbook A opens, workbook B opens automatically. Auto open workbook macro is working nicely. But when I want to close workbook A, the workbook B should close together automatically. But with the auto close macro below, it doesn't seems to work. Please advise where went wrong :

Private Sub Workbook_Close()
If Workbooks.Close("A.xlxm") Then
ThisWorkbook.Close
End If
End Sub

The code above is pasted in workbook B's ThisWorkbook.

Upvotes: 0

Views: 267

Answers (1)

QHarr
QHarr

Reputation: 84465

In workbook A in the ThisWorkbook code pane.

Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    On Error Resume Next
    Application.Workbooks.Item("B.xlsm").Close True
    On Error GoTo 0
End Sub

Upvotes: 0

Related Questions