Ask Your Question
3

How can I activate a macro when switching between Worksheets, rather than when modifying data within a Worksheet?

asked 2023-05-22 08:05:53 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-22 08:21:02 +0000

bukephalos gravatar image

You can use the WorksheetDeactivate() and WorksheetActivate() events to activate a macro when switching between worksheets.

  1. Open the VBA Editor by pressing Alt + F11.
  2. In the Project Explorer, double-click the worksheet where you want to activate the macro.
  3. In the code window that opens, select Worksheet from the left drop-down list, and then select Deactivate from the right drop-down list.
  4. Enter the following code to specify the macro you want to activate when you switch away from this worksheet:

Sub Worksheet_Deactivate() 'Code to be activated when switching away from this worksheet End Sub

  1. Now, select Activate from the right drop-down list.
  2. Enter the following code to specify the macro you want to activate when you switch to this worksheet:

Sub Worksheet_Activate() 'Code to be activated when switching to this worksheet End Sub

  1. Save your changes and close the VBA Editor.

Now, whenever you switch between worksheets, the corresponding activate or deactivate macro will be triggered.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-22 08:05:53 +0000

Seen: 8 times

Last updated: May 22 '23