Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 192
Default Foolproof way to trap Worksheet Change?

Is there any reliable way to detect when user switches worksheets?

My VBA needs to react when a user switches worksheets, and I have a set of
event catchers (Workbook_SheetSelectionChange, Worksheet_SelectionChange,
WorkSheet_Activate, WorkSheet_Deactivate) which I thought was foolproof.
Whether user switched sheets via my hyperlinks, used sheet tabs, or used the
Web toolbar, I got one of these events to trigger.

But user can start on sheet1, use Edit/GoTo to get to Sheet2, click an
option button, use Edit/GoTo to get back to sheet1, click a button all
without triggerring an event. I can trap more events with the option
buttons, but isn't there an easier way?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Foolproof way to trap Worksheet Change?

The problem with the hyperlink not firing the worksheet_activate was fixed in a
later version of excel (broken in xl2k, but works in xl2002, IIRC).

I'm not sure if that affected the web toolbar, though.



Nick wrote:

Is there any reliable way to detect when user switches worksheets?

My VBA needs to react when a user switches worksheets, and I have a set of
event catchers (Workbook_SheetSelectionChange, Worksheet_SelectionChange,
WorkSheet_Activate, WorkSheet_Deactivate) which I thought was foolproof.
Whether user switched sheets via my hyperlinks, used sheet tabs, or used the
Web toolbar, I got one of these events to trigger.

But user can start on sheet1, use Edit/GoTo to get to Sheet2, click an
option button, use Edit/GoTo to get back to sheet1, click a button all
without triggerring an event. I can trap more events with the option
buttons, but isn't there an easier way?


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Foolproof way to trap Worksheet Change?

Using OnSheetActivate and OnSheetDeactivate works in XL2000.
(XL5 code)
In your workbook open event place something like this,
for each desired sheet ...

Sub FixItUp()
Me.Worksheets("Sheet3").OnSheetActivate = "DoThis"
Me.Worksheets("Sheet3").OnSheetDeactivate = "DoThat"
End Sub

In your workbook close event use code like this for every
sheet listed above ...
Sub UndoFix()
Me.Worksheets("Sheet3").OnSheetActivate = ""
Me.Worksheets("Sheet3").OnSheetDeactivate = ""
End Sub

DoThis and DoThat are the subs that run when the applicable
sheet is activated/deactivated. Place those subs in a general module.
Any of the sub names can be names you choose.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Nick"
wrote in message
Is there any reliable way to detect when user switches worksheets?

My VBA needs to react when a user switches worksheets, and I have a set of
event catchers (Workbook_SheetSelectionChange, Worksheet_SelectionChange,
WorkSheet_Activate, WorkSheet_Deactivate) which I thought was foolproof.
Whether user switched sheets via my hyperlinks, used sheet tabs, or used the
Web toolbar, I got one of these events to trigger.

But user can start on sheet1, use Edit/GoTo to get to Sheet2, click an
option button, use Edit/GoTo to get back to sheet1, click a button all
without triggerring an event. I can trap more events with the option
buttons, but isn't there an easier way?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 192
Default Foolproof way to trap Worksheet Change?

Wow! This works with everything I've thrown at it -- changing sheets via
tabs, hyperlinks, Goto, web toolbar.

It's interesting that your solution works even when Workbook_SheetActivate
does not. I would have guessed they would respond to the same events. (I'm
testing under XL2000, since I don't know which versions my users have, so it
may have been fixed in XL2002.)


Thanks, much appreciated!

Nick


"Jim Cone" wrote:

Using OnSheetActivate and OnSheetDeactivate works in XL2000.
(XL5 code)
In your workbook open event place something like this,
for each desired sheet ...

Sub FixItUp()
Me.Worksheets("Sheet3").OnSheetActivate = "DoThis"
Me.Worksheets("Sheet3").OnSheetDeactivate = "DoThat"
End Sub

In your workbook close event use code like this for every
sheet listed above ...
Sub UndoFix()
Me.Worksheets("Sheet3").OnSheetActivate = ""
Me.Worksheets("Sheet3").OnSheetDeactivate = ""
End Sub

DoThis and DoThat are the subs that run when the applicable
sheet is activated/deactivated. Place those subs in a general module.
Any of the sub names can be names you choose.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Foolproof way to trap Worksheet Change?

Nick,
Appreciate the feedback.
As Microsoft "improves" each version problems emerge.
There are lots of improvements in the forthcoming xl 2007.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


"Nick"
wrote in message
Wow! This works with everything I've thrown at it -- changing sheets via
tabs, hyperlinks, Goto, web toolbar.

It's interesting that your solution works even when Workbook_SheetActivate
does not. I would have guessed they would respond to the same events. (I'm
testing under XL2000, since I don't know which versions my users have, so it
may have been fixed in XL2002.)
Thanks, much appreciated!
Nick


"Jim Cone" wrote:
Using OnSheetActivate and OnSheetDeactivate works in XL2000.
(XL5 code)
In your workbook open event place something like this,
for each desired sheet ...

Sub FixItUp()
Me.Worksheets("Sheet3").OnSheetActivate = "DoThis"
Me.Worksheets("Sheet3").OnSheetDeactivate = "DoThat"
End Sub

In your workbook close event use code like this for every
sheet listed above ...
Sub UndoFix()
Me.Worksheets("Sheet3").OnSheetActivate = ""
Me.Worksheets("Sheet3").OnSheetDeactivate = ""
End Sub

DoThis and DoThat are the subs that run when the applicable
sheet is activated/deactivated. Place those subs in a general module.
Any of the sub names can be names you choose.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 192
Default Foolproof way to trap Worksheet Change?

Jim,

It's quite interesting to compare using the "OnSheetActivate" property that
you suggested with the Worksheet_Activate and Workbook_SheetActivate events.

As I wrote before, OnSheetActivate seems to trap the event no matter which
method the user employs to switch worksheets, even when neither
Worksheet_Activate nor Workbook_SheetActivate is raised.

Using your solution, I was able to eliminate several routines I had
previously used. In the process, I did note that "OnSheetActivate" does not
trap the worksheet change if the VB code itself issues a
Worksheet("Sheet2").Activate. However, in that case, the Worksheet_Activate
event is raised. Using OnSheetActivate, I no longer need to activate via
code, but it was a head-scratcher before I figured it out.

Do you know if the "OnSheetActivate" property remains valid in XL2007? It
seems like it's been removed from the documentation going back to XL2000.

Thanks again for your help.

Nick


"Jim Cone" wrote:

Nick,
Appreciate the feedback.
As Microsoft "improves" each version problems emerge.
There are lots of improvements in the forthcoming xl 2007.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I trap the change event (Tab Press key) of these controls Padam Excel Programming 2 August 11th 06 09:41 AM
how can I trap the change event (Tab Press key) of these controls Padam Excel Programming 2 August 11th 06 09:40 AM
how can I trap the change event (Tab Press key) of these controls Padam Excel Worksheet Functions 1 August 10th 06 03:59 PM
how can I trap the change event (Tab Press key) of these controls Padam Excel Worksheet Functions 0 August 10th 06 02:41 PM
Trap Sheet Name Change Otto Moehrbach[_6_] Excel Programming 6 October 25th 04 11:58 PM


All times are GMT +1. The time now is 02:17 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"