I want to run a macro every time an excel files opens and before it closes. I've tried using the below script but it didn't work.

Private Sub Worksheet_Activate()
'my macro
End Sub
  • 1
    How do you know it didn't work? Your script doesn't actually do anything. – DavidPostill Oct 12 '15 at 9:07
  • Put MsgBox("Hello World!") in, as David mentioned. Marked as "unclear what you're asking" – Leo Chapiro Oct 12 '15 at 9:15
  • @DavidPostill I've written MsgBox("Hello World!") but nothing appears when I open the workbook – Tak Oct 12 '15 at 9:24

How do I run a macro every time a workbook opens or closes?


Private Sub Workbook_Open()
' dosomething
End Sub 


  • Use Sub Auto_Open() in a module


Private Sub Workbook_BeforeClose(Cancel As Boolean)
' dosomething
End Sub 


  • Use Sub Auto_Close() in a module


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.