LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default Select statement or me?

On 5 May, 07:17, Zeq wrote:
tony;715598 Wrote:

Can someone explain the bizzare behaviour I am getting with the





following select statement?


Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("mysheet").Select
ActiveSheet.Unprotect ("mypassword")
Select Case Target.Address
Case $x$n
Range("$y$n").Select
Range("$y$n").Locked = False
'perform some action to cell $y$n
Range($y$n").Locked = True
End Select
ActiveSheet.Protect("mypassword")
End Sub


You would expect that the sub would unprotect the worksheet, select
the cell, unlock the cell, perform the action ,lock the cell and
protect the worksheet.


What appears to happen is this:
The sub unprotects the worksheet, selects the cell, unlocks the cell,
performs the action , protects the worksheet and then attempts to lock
the cell (which it can't because the worksheet is now protected).


I've managed to work around this by putting the protect bit in the
case statement but surely this isn't right is it?


Just in case your wondering I'm not referencing merged cells - I
thought that was the problem but is isn't.


A lot of good comments to this post already, but I suppose the original
"bizarre execution order" has not been fully discussed yet. To me this
sounds like the changes you do after unlocking Range("$y$n") result in
this sub running again before the current sub gets to locking this
Range.

This could happen e.g. if you have this Worksheet_Change sub in sheet
"mysheet". What then would happen is that the second instance of this
sub, activated by your changes and running in the middle of the first
instance, protects the sheet before the first instance continues and
tries to lock the range. I realise the above is not as clear as it could
be, hope you get my point....

Should this be the case - I suggest testing by setting breakpoints in
your code - you might want to have Application.EnableEvents=False at the
beginning of your sub and Application.EnableEvents=True at the end to
prevent it running again if the sub itself makes changes.

--
Zeq
------------------------------------------------------------------------
Zeq's Profile:http://www.thecodecage.com/forumz/member.php?u=1626
View this thread:http://www.thecodecage.com/forumz/sh...d.php?t=200317

http://www.thecodecage.com/forumz- Hide quoted text -

- Show quoted text -


Hi Zeq,

That's what I suspected so I did use the Application.EnableEvents and
still no joy, but I think it has to be a case of the sub running twice
- actually it's the only explanation.

Anyway thanks to everyone for their contributions, I did eventually
get my code to do what I wanted even though it became a bit long
winded. I just have to test it out on my work mates now and see if
they can break it!

They usually do!
 
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
Select statement failing DesertCyclist Excel Programming 8 October 16th 07 06:41 AM
Trouble with select statement James W.[_2_] Excel Programming 0 May 19th 05 06:04 PM
SELECT statement Tom Ogilvy Excel Programming 2 July 23rd 04 06:24 PM
SQL Select statement with Date as Pepe[_2_] Excel Programming 3 February 4th 04 06:50 PM
Adding SQL Select Statement Pepe[_2_] Excel Programming 1 January 29th 04 02:19 AM


All times are GMT +1. The time now is 12:30 PM.

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

About Us

"It's about Microsoft Excel"