Auto Open and Close Excel VBA Procedures
There are many occasions when you need to perform VBA logic at a Worksheet startup or close. Just create a public module and give the procedures the names as shown in the example below. Place the code in the "General Modules" section of the VBA interface.
Program Code
Public Sub Auto_Open()
' *****************************************
' Put Program Code Here For Auto_Open
' *****************************************
MsgBox ("AutoOpen")
End Sub
Public Sub Auto_Close()
' *****************************************
' Put Program Code Here For Auto_Close
' *****************************************
MsgBox ("AutoClose")
End Sub
Workbook Open and Close Events
Another method to automatically execute code when a workbook opens or closes is to use the Workbook_Open() and Workbook_BeforeClose() procedures. These procedures will execute even if the workbook is opened from VBA, whereas the Auto_Open will not execute if opened from VBA. Make sure the code is added to the "ThisWorkbook" section of the VBA interface and not the "General Modules" section.
Program Code
Option Explicit
Private Sub Workbook_Open()
MsgBox ("Workbook Open")
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
MsgBox ("Workbook About To Close")
End Sub