Example The Workbook BeforeClose Event Handler

Private Sub Workbook BeforeClose(Cancel As Boolean) ' Delete custom menu and close all add-ins

Dim r As Integer Dim ws As Worksheet Dim sName As String

' In case we try to close a workbook that is not open On Error Resume Next


Set ws = ThisWorkbook.Worksheets("DataSheet")

For r = 2 To Application.WorksheetFunction.CountA(ws.Range("A:A")) sName = ws.Cells(r, InWorkbook Col).Value If sName <> "" And sName <> "ThisWorkbook" Then

Workbooks(sName).Close End If Next r End Sub

The DeleteCustomMenus procedure is shown in Example 12-17.

0 0

Post a comment