Results 1 to 4 of 4

Thread: *Resolved* OnSheet_Selection?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Resolved *Resolved* OnSheet_Selection?

    Is there a VBA command (in Excel) that will run a procedure located in a sheet when that sheet is activated? I was thinking of something along the lines of the Selection_Change command but instead of having to change the focus on the sheet, I want the procedure to run when that sheet is selected.

    I was rather hoping that Excel would keep a track of all the procedures in a sheet so that if the conditions of the sheet change and one of the procedures says 'do something if a cell value = a certain number' then that change would automatically occur, but it seems that I have to call the procedure to make it work. I can get it to work by attaching it to a Forms button but I want it to run whenever that sheet is selected and not only if I change the focus or press a button!

    Any ideas?

    Cheers
    -Rob
    Last edited by TheRobster; Nov 16th, 2004 at 03:47 PM.

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    VB Code:
    1. Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    2.     If (Sh.Name = "Sheet1") Then
    3.         MsgBox "Hey, whaddayaknow Sheet1's been selected"
    4.     End If
    5. End Sub

    Place this code inside of the "ThisWorkbook" module & enjoy!!!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275
    That did the trick. Cheers Alex.

    -Rob

    *Edit* I would have thought that placing a subroutine within a sheet would mean that Excel either checks the subroutine to see if any conditions have been met that mean it should be run, or monitors them continously so that if any conditions change which match the criteria for a subroutine then it is automatically run.

    This doesn't seem to be the case though.........it seems you always have to call a subroutine, even when the conditions for it to be executed are met it doesn't run automatically. Is this correct?
    Last edited by TheRobster; Nov 16th, 2004 at 03:57 PM.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Place this in the ThisWorkbook class. It will give real-time detection,
    so to speak, not when the sheet changes from sheet1 to sheet2, etc.

    VB Code:
    1. Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    2.     MsgBox "Hey, whaddayaknow " & Sh.Name & "'s been selected"
    3. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width