|
-
Nov 15th, 2004, 11:03 PM
#1
Thread Starter
Hyperactive Member
*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.
-
Nov 16th, 2004, 01:05 PM
#2
VB Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If (Sh.Name = "Sheet1") Then
MsgBox "Hey, whaddayaknow Sheet1's been selected"
End If
End Sub
Place this code inside of the "ThisWorkbook" module & enjoy!!!
-
Nov 16th, 2004, 03:48 PM
#3
Thread Starter
Hyperactive Member
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.
-
Nov 16th, 2004, 09:45 PM
#4
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:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
MsgBox "Hey, whaddayaknow " & Sh.Name & "'s been selected"
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|