Private Sub Workbook_Open() ' macro called when workbook has loaded Dim sh2 As Worksheet Set sh2 = ThisWorkbook.Sheets("sheet2") shLR = sh2.Cells(Rows.Count, 1).End(xlUp).Row + 1 sh2.Cells(shLR, 1) = Format(Date, "mm-dd-yyyy") sh2.Cells(shLR, 2) = Format(Time, "hh:mm:ss") End Sub
Private Sub Workbook_Activate() ' macro called when workbook is activated ... End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) answer = MsgBox("Are you sure you want to save?", vbYesNo) If answer = vbNo Then Cancel = False End If End Sub
Private Sub Workbook_AfterSave(ByVal Success As Boolean) msg = "Successfully saved" If Success = False Then msg = "Failed to save" MsgBox msg End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean) If ActiveSheet.Range("C1") <> "hello" Then answer = MsgBox("Are you certain you want to close? Cell C1 doesn't say 'hello'", vbYesNo) If answer = vbNo Then Cancel = True End If End Sub
Private Sub Workbook_BeforePrint(Cancel As Boolean) answer = MsgBox("Are you certain you want to print this", vbYesNo) If answer = vbNo Then Cancel = True End Sub
Private Sub Workbook_Deactivate() MsgBox "Workbook deactivated" End Sub
Private Sub Workbook_NewChart(ByVal Ch As Chart) Ch.ChartTitle.Text = "My Custom Chart" End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object) Sh.Cells.Font.Bold = True Sh.Cells.Font.Size = 14 End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object() If Sh.Name = "Sheet3" Then MsgBox "This is Sheet3" End If End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object) If Sh.Name = "Sheet3" Then MsgBox "This is Sheet3" End If End Sub Private Sub Workbook_SheetBeforeDelete(ByVal Sh As Object) Stop End Sub Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean) If Target.Address(0, 0) = "A2" Then MsgBox "You got A2" End If End Sub Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean) If Target.Address(0, 0) = "A2" Then MsgBox "You got A2" End If End Sub Private Sub Workbook_SheetCalculate(ByVal Sh As Object) ' runs any time any sheet does a calculation End Sub Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) ' runs any time the selected range changes End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object) ' can prevent deactivation by activating the sheet End Sub
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink) ' Stop End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) ' fired any time any cell selection is changed End Sub
Private Sub Workbook_WindowActivate(ByVal Wn As Window) ' only affects workbook being activated End Sub
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window) ' only affects workbook being deactivated End Sub
Private Sub Workbook_WindowResize(ByVal Wn As Window) ' as named End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object) sheetName = Sh.Name MsgBox "No new sheets may be created." Application.DisplayAlerts = False Sheets(sheetName).Delete Application.DisplayAlerts = True End Sub