Home » excel » Run excel VBA function when sheet is clicked

Run excel VBA function when sheet is clicked

Posted by: admin March 9, 2020 Leave a comment

Questions:

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?

How to&Answers:

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