Run excel VBA function when sheet is clicked

calendar_today Asked Oct 24, 2011
thumb_up 7 upvotes
history Updated April 16, 2026

Question posted 2011 · +5 upvotes

I was wondering if there was a way to run a VBA script when I open a sheet in the workbook.

For example, I have a workbook called “Inventory” and I want to run an “InitiateInventoryValues” Function when the “View Inventory” sheet is opened.

Can anybody please help me on this?

Accepted answer +7 upvotes

Double click the “Workbook” icon in VBE and use this event. It will trigger everytime you activate a different sheet by clicking its tab. If the tab is the one named “View Inventory”, your code will run (once) when the sheet is activated:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)

If Sh.Name = "View Inventory" Then
    'Do your code
End If

End Sub

VBA Core objects referenced (4)

  • Sh.Name — Refer to Named Ranges
  • Sh.Name — Invalid procedure name error
  • Workbook — Workbooks and Worksheets
  • Workbook — Add a Table of Contents to a Workbook

Top vba Q&A (6)

+7 upvotes ranks this answer #56 out of 81 vba solutions on this site .
vba