Workbook activation events

Excel also recognizes the event that occurs when you activate or deactivate a particular workbook. The following code, which is contained in the Code window for the ThisWorkbook object, is executed whenever the workbook is activated. The procedure simply maximizes the workbook's window.

Private Sub Workbook_Activate()

ActiveWindow.WindowState = xlMaximized End Sub

The Workbook_Deactivate code, shown next, is executed when a workbook is deactivated. This procedure minimizes the workbook's window:

Private Sub Workbook_Deactivate()

ThisWorkbook.Windows(1).WindowState = xlMinimized End Sub

Notice that I didn't use ActiveWindow in this code. That's because the workbook is no longer the active window when it's deactivated. Therefore, I used ThisWorkbook, which refers to the workbook that contains the code.

0 0

Post a comment