Invoking macro when a tab is selected

  • Thread starter Thread starter MicroMain
  • Start date Start date
M

MicroMain

Mr. Novice again. . .

I have a macro to do some actions on data that I want to invoke when
they select the tab.

Also, I want to invoke some data clearing actions for when they leave
the tab and go somewhere else.

Can someone share the commands to start macros when entering or leaving
a sheet?

Thanks.
 
Hi Mr Novice,
to trigger code place it in appropriate event procedure in worksheet
module. To get to a module right click a sheet tab>View Code>change
left drop down list from General to Worksheet> select the appropriate
Event procedure from the right drop down list where you should see
Activate and Deactivate.
Ken Johnson
 
Hi Mr Novice,
Maybe I went a bit too fast there. I should have started by saying that
there are no commands for starting macros when entering (Activating) or
leaving (Deactivating) a sheet.
Instead, the code is triggered by either placing the code inside an
event procedure or calling your code from an event procedure. The start
and end lines for these event procedures are found in the worksheet
modules.
Say you already have a macro called macro1 and you want it to start
running when sheet1 is activated, then you could either type its name
between the ready supplied start and finish line:

Private Sub Worksheet_Activate()
macro1
End Sub

or paste the code there.

Either way, macro1 is then automatically run whenever sheet1 is
activated.

Similarly for the deactivate event sub.

Ken Johnson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top