Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 219
Default Sheet change event not triggering macro

I have this macro in ThisWorkbook:

Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
'Activated by a change in any worksheet in the workbook - gets password
from user
' verifies password, and unprotects worksheet
MsgBox "Sheet Change"


Dim vResponse As Variant

Set wsPwrdNames = ThisWorkbook.Sheets("sheet1")
Set rShNames = wsPwrdNames.Range("ShNames")
Set rPwrdEnt = wsPwrdNames.Range("bPwrd")
If rPwrdEnt.Value = "True" Then Exit Sub 'EXIT

Set rFoundShName = rShNames.Find(ActiveSheet.Name, _
LookIn:=xlValues, _
LookAt:=xlWhole)

If rFoundShName Is Nothing Then Exit Sub 'EXIT

wsPwrdNames.Visible = True

PwrdForm:

ufPwrdEntry.Show

If sPwrd = rFoundShName.Offset(0, 1).Value Then
bPwrdEntrd = True
Application.EnableEvents = False
rPwrdEnt.Value = bPwrdEntrd
Application.EnableEvents = True
Else
vResponse = MsgBox("Incorrect Password! Click OK to try again, Cancel
to exit", _
vbOKCancel)
If vResponse = vbCancel Then
ufPwrdEntry.Hide
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
wsPwrdNames.Visible = False
End 'EXIT
Else
GoTo PwrdForm
End If
End If

wsPwrdNames.Visible = False
End 'EXIT
wsPwrdNames.Visible = False
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub 'EXIT

Private Sub Workbook_SheetDeactivate(ByVal ws As Object)
'when active worksheet is deactivated, reset variables and reinstate
passord protection

Application.ScreenUpdating = False

Set wsPwrdNames = ThisWorkbook.Sheets("sheet1")
Set rPwrdEnt = wsPwrdNames.Range("bPwrd")
bPwrdEntrd = False
Application.EnableEvents = False
rPwrdEnt.Value = bPwrdEntrd
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

The MsgBox at the beginning should be popping up every time I make a sheet
change, but it's not showing at all. I've checked in the Immediate Window
to make sure Events are turned on. Any ideas as to what else could cause
this code to be inoperative?
Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 219
Default Sheet change event not triggering macro

On Tue, 10 Feb 2009 15:59:28 -0700, salgud wrote:

I have this macro in ThisWorkbook:

Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
'Activated by a change in any worksheet in the workbook - gets password
from user
' verifies password, and unprotects worksheet
MsgBox "Sheet Change"


Dim vResponse As Variant

Set wsPwrdNames = ThisWorkbook.Sheets("sheet1")
Set rShNames = wsPwrdNames.Range("ShNames")
Set rPwrdEnt = wsPwrdNames.Range("bPwrd")
If rPwrdEnt.Value = "True" Then Exit Sub 'EXIT

Set rFoundShName = rShNames.Find(ActiveSheet.Name, _
LookIn:=xlValues, _
LookAt:=xlWhole)

If rFoundShName Is Nothing Then Exit Sub 'EXIT

wsPwrdNames.Visible = True

PwrdForm:

ufPwrdEntry.Show

If sPwrd = rFoundShName.Offset(0, 1).Value Then
bPwrdEntrd = True
Application.EnableEvents = False
rPwrdEnt.Value = bPwrdEntrd
Application.EnableEvents = True
Else
vResponse = MsgBox("Incorrect Password! Click OK to try again, Cancel
to exit", _
vbOKCancel)
If vResponse = vbCancel Then
ufPwrdEntry.Hide
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
wsPwrdNames.Visible = False
End 'EXIT
Else
GoTo PwrdForm
End If
End If

wsPwrdNames.Visible = False
End 'EXIT
wsPwrdNames.Visible = False
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub 'EXIT

Private Sub Workbook_SheetDeactivate(ByVal ws As Object)
'when active worksheet is deactivated, reset variables and reinstate
passord protection

Application.ScreenUpdating = False

Set wsPwrdNames = ThisWorkbook.Sheets("sheet1")
Set rPwrdEnt = wsPwrdNames.Range("bPwrd")
bPwrdEntrd = False
Application.EnableEvents = False
rPwrdEnt.Value = bPwrdEntrd
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

The MsgBox at the beginning should be popping up every time I make a sheet
change, but it's not showing at all. I've checked in the Immediate Window
to make sure Events are turned on. Any ideas as to what else could cause
this code to be inoperative?
Thanks.


Weird. I played with this problem for over an hour before I posted here.
Right after I posted, I noticed a "new" icon (just had XL 11 installed) at
the top of the Explorer Window in the VBE. I clicked on the "View Object"
icon which, of course, selected whatever sheet I had currently selected.
And the "Sheet Change" dialog box appeared!

So the question is, why would using the "View Object" icon trigger the
SheetChange event when clicking on the actual sheet tabs didn't? Does this
make any sense at all? Anyone else seen this?
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 219
Default Sheet change event not triggering macro

On Tue, 10 Feb 2009 18:41:52 -0500, JLGWhiz wrote:

The SheetChange event is for changes to the sheet, not changing selection of
sheets. Clicking the name tab does not change the sheet. You would need to
change a cell content on the sheet to trigger the event. If you want to
trigger the macro by changing the active sheet, then use the SheetActvate or
SheetDeactivate event.



"salgud" wrote in message
...
On Tue, 10 Feb 2009 15:59:28 -0700, salgud wrote:

I have this macro in ThisWorkbook:

Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As
Range)
'Activated by a change in any worksheet in the workbook - gets password
from user
' verifies password, and unprotects worksheet
MsgBox "Sheet Change"


Dim vResponse As Variant

Set wsPwrdNames = ThisWorkbook.Sheets("sheet1")
Set rShNames = wsPwrdNames.Range("ShNames")
Set rPwrdEnt = wsPwrdNames.Range("bPwrd")
If rPwrdEnt.Value = "True" Then Exit Sub 'EXIT

Set rFoundShName = rShNames.Find(ActiveSheet.Name, _
LookIn:=xlValues, _
LookAt:=xlWhole)

If rFoundShName Is Nothing Then Exit Sub 'EXIT

wsPwrdNames.Visible = True

PwrdForm:

ufPwrdEntry.Show

If sPwrd = rFoundShName.Offset(0, 1).Value Then
bPwrdEntrd = True
Application.EnableEvents = False
rPwrdEnt.Value = bPwrdEntrd
Application.EnableEvents = True
Else
vResponse = MsgBox("Incorrect Password! Click OK to try again, Cancel
to exit", _
vbOKCancel)
If vResponse = vbCancel Then
ufPwrdEntry.Hide
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
wsPwrdNames.Visible = False
End 'EXIT
Else
GoTo PwrdForm
End If
End If

wsPwrdNames.Visible = False
End 'EXIT
wsPwrdNames.Visible = False
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub 'EXIT

Private Sub Workbook_SheetDeactivate(ByVal ws As Object)
'when active worksheet is deactivated, reset variables and reinstate
passord protection

Application.ScreenUpdating = False

Set wsPwrdNames = ThisWorkbook.Sheets("sheet1")
Set rPwrdEnt = wsPwrdNames.Range("bPwrd")
bPwrdEntrd = False
Application.EnableEvents = False
rPwrdEnt.Value = bPwrdEntrd
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

The MsgBox at the beginning should be popping up every time I make a
sheet
change, but it's not showing at all. I've checked in the Immediate Window
to make sure Events are turned on. Any ideas as to what else could cause
this code to be inoperative?
Thanks.


Weird. I played with this problem for over an hour before I posted here.
Right after I posted, I noticed a "new" icon (just had XL 11 installed) at
the top of the Explorer Window in the VBE. I clicked on the "View Object"
icon which, of course, selected whatever sheet I had currently selected.
And the "Sheet Change" dialog box appeared!

So the question is, why would using the "View Object" icon trigger the
SheetChange event when clicking on the actual sheet tabs didn't? Does this
make any sense at all? Anyone else seen this?


Thank you, thank you, thank you. I had completely forgotten that when I
first wrote this code, part of it was supposed to run when the user tried
to make a change to the data in the sheet, and the other part (not shown in
my post) runs when the sheet is deactivated. So the code is correct, but my
interpretation of what it was doing was wrong. Duh!
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
Worksheet Change Event Triggering Multiple Times PosseJohn Excel Programming 2 November 9th 08 12:38 PM
RTD value changes not triggering worksheet change event DTM[_4_] Excel Programming 2 June 7th 06 05:01 PM
Validation not triggering Change event in Excel XP Jeffrey[_8_] Excel Programming 3 March 13th 06 03:33 AM
Validation not triggering Change event in Excel XP Jeffrey[_8_] Excel Programming 1 March 8th 06 01:59 AM
auto-filter change not triggering worksheet_change event mark Excel Programming 1 September 19th 03 03:01 PM


All times are GMT +1. The time now is 05:16 PM.

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"