Consulting

Results 1 to 6 of 6

Thread: Solved: Detecting when a different sheet is made active without using Worksheet_Activate

  1. #1

    Solved: Detecting when a different sheet is made active without using Worksheet_Activate

    I have a form that loads a different picture depending on what sheet is active. I'm using the Worksheet_Activate event to switch the pictures, and this works fine if I change sheets by clicking on the Sheet Tab. However it doesn't work if I change sheets by just clicking on an inactive sheet to bring it to the front, as this apparently doesn't trigger the Activate Event.

    Is there any way I can detect when the active sheet changes without depending on the Activate Event?

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,728
    Location
    What do you mean by "clicking on an inactive sheet to bring it to the front"? Do you mean switching to a different workbook? If so, you need the workbook_activate event or, more likely, an application-level application_workbookactivate event
    Be as you wish to seem

  3. #3
    All the sheets are in the same workbook, with multiple sheets visible in different Windows. When I click on a sheet in the background, it's Window comes to the front and it becomes active, except there was no Sheet_Activate Event issued. I need to change sheets by clicking on the Sheet Tab before I get an Activate Event.

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,728
    Location
    Sounds like a workbook_windowactivate event then.
    Be as you wish to seem

  5. #5
    Exactly so! That fixed it. Thank-you.

  6. #6
    Just to follow up on this, it turns out that the Workbook_WindowActivate Event doesn't issue when the active sheet is changed by a click on the Sheet Tabs, because then the new sheet is coming up in the same window. So in order to make sure my code responds to both methods of changing sheets, I have to account for both events. These 2 subs illustrate the way sheet activation events work.

    [vba]Private Sub Workbook_WindowActivate(ByVal Wn As Window)
    Debug.Print "Active Sheet name = " & ActiveSheet.Name & " after click on inactive window"
    End Sub[/vba]
    No Worksheet_Activate Event happens when the sheet changes in different windows.

    [vba]Private Sub Worksheet_Activate()
    Debug.Print "Active Sheet name = " & ActiveSheet.Name & " after click on Sheet Tab"
    End Sub[/vba] No Workbook_WindowActivate Event happens when the sheet changes in the same window.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •