events are global to the workbook
right click on the Workbook object in the VBA editor
from the context menu select View Code
change from a General macro to a Workbook macro
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
Top
Index
Private Sub Workbook_Activate()
' macro called when workbook is activated
...
End Sub
Top
Index
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
Top
Index
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
msg = "Successfully saved"
If Success = False Then msg = "Failed to save"
MsgBox msg
End Sub
Top
Index
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
Top
Index
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
Top
Index
Private Sub Workbook_Deactivate()
MsgBox "Workbook deactivated"
End Sub
Top
Index
select cells with data for chart
Private Sub Workbook_NewChart(ByVal Ch As Chart)
Ch.ChartTitle.Text = "My Custom Chart"
End Sub
Top
Index
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Sh.Cells.Font.Bold = True
Sh.Cells.Font.Size = 14
End Sub
Top
Index
Private Sub Workbook_SheetActivate(ByVal Sh As Object()
If Sh.Name = "Sheet3" Then
MsgBox "This is Sheet3"
End If
End Sub
Top
Index
Delete, DoubleClick, RightClick, Calculate, Change
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
Top
Index
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
' can prevent deactivation by activating the sheet
End Sub
Top
Index
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
' Stop
End Sub
Top
Index
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
' fired any time any cell selection is changed
End Sub
Top
Index
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
' only affects workbook being activated
End Sub
Top
Index
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
' only affects workbook being deactivated
End Sub
Top
Index
Private Sub Workbook_WindowResize(ByVal Wn As Window)
' as named
End Sub
Top
Index
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
Top
Index