On Sheet select Macro

  • Thread starter Thread starter Colin G Eastwood
  • Start date Start date
C

Colin G Eastwood

Hi there

I need a macro to run, but only when a certain 'WorkSheet' is selected
from within a Workbook containing many worksheets.

Can anyone help me with this?
 
Use the "SheetActivate" event of the Workbook object.

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "Sheet2" Then
MsgBox "You selected Sheet2."
End If
End Sub

HTH.
-----Original Message-----
Hi there

I need a macro to run, but only when a
certain 'WorkSheet' is selected
 
If you want to run the macro automatic when you select the sheet you can
use this event in the sheet module

Private Sub Worksheet_Activate()
MsgBox "Hi"
End Sub

Or use this line in your macro to exit the sub if the sheet you want
is not the active one

Sub test()
If ActiveSheet.Name <> "yourname" Then Exit Sub
MsgBox "Your code"
End Sub
 
Back
Top