Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 92
Default Problem w/ worksheet change event

Hi all. I have the following inside a worksheet change event:
'Lock/Unlock cells based on FT/PT status
If .Column = 5 And .Row 11 And .Row < 312 Then
Application.EnableEvents = False
If .Value = "PT" Then
Cells(.Row, 6).Value = 20
Else
Cells(.Row, 6).Value = 40
End If
Application.EnableEvents = True
End If

I the user highlights and clears contents of column 5 and 6 at the same time
(ie E5:F5), I get a type mismatch error on the line If .Value = "PT" Then,
and the change event won't fire unless I completely clode out of excel and
re-open. Any ideas on how to prevent that from happening?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Problem w/ worksheet change event

First lets address the "Events stop firing" problem. Any time you modify the
applicatin level settings you really NEED to use an error handler. Something
like this...

On Error Goto ErrorHandler
If .Column = 5 And .Row 11 And .Row < 312 Then
Application.EnableEvents = False
If .Value = "PT" Then
Cells(.Row, 6).Value = 20
Else
Cells(.Row, 6).Value = 40
End If
End If

ErrorHandler:
Application.EnableEvents = True
End Sub

Now if an error happens the code will go to the error handler and turn the
events back on. Careful when debugging this kind of code also becuase if you
halt the exectuion prior to the application setting being reset you will run
into the same problem. When I am working on code similar to this I will
sometimes have a procedure which I will run to reset things back to normal.
Something like this...

Sub ResetApplication()
application.enableevents = true
application.screenupdating = true
application.calculation = xlautomatic
application.statusbar = false
application.displayalerts = true
End Sub

Instead of rebooting excel just run this and your world will be back to its
happy place.
--
HTH...

Jim Thomlinson


"Steph" wrote:

Hi all. I have the following inside a worksheet change event:
'Lock/Unlock cells based on FT/PT status
If .Column = 5 And .Row 11 And .Row < 312 Then
Application.EnableEvents = False
If .Value = "PT" Then
Cells(.Row, 6).Value = 20
Else
Cells(.Row, 6).Value = 40
End If
Application.EnableEvents = True
End If

I the user highlights and clears contents of column 5 and 6 at the same time
(ie E5:F5), I get a type mismatch error on the line If .Value = "PT" Then,
and the change event won't fire unless I completely clode out of excel and
re-open. Any ideas on how to prevent that from happening?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Problem w/ worksheet change event

If they select the entire column, then .Row would not be between 11 and 312.
Nontheless, if you only want the changes made if a single cell has been
changed


'Lock/Unlock cells based on FT/PT status
On Error goto ErrHandler
if Target.count 1 then exit sub
If .Column = 5 And .Row 11 And .Row < 312 Then
Application.EnableEvents = False
If .Value = "PT" Then
Cells(.Row, 6).Value = 20
Else
Cells(.Row, 6).Value = 40
End If
End If

ErrHandler:
Application.EnableEvents = True


--
Regards,
Tom Ogilvy

"Steph" wrote in message
...
Hi all. I have the following inside a worksheet change event:
'Lock/Unlock cells based on FT/PT status
If .Column = 5 And .Row 11 And .Row < 312 Then
Application.EnableEvents = False
If .Value = "PT" Then
Cells(.Row, 6).Value = 20
Else
Cells(.Row, 6).Value = 40
End If
Application.EnableEvents = True
End If

I the user highlights and clears contents of column 5 and 6 at the same

time
(ie E5:F5), I get a type mismatch error on the line If .Value = "PT" Then,
and the change event won't fire unless I completely clode out of excel and
re-open. Any ideas on how to prevent that from happening?




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Problem w/ worksheet change event

As for the .Value throwing an error. I am assuming that you have "With
Target"... The problem is that target is a range, not a single cell. What you
can do is to traverse through all of the cells in the target and check if it
is within the range you have defined and then do the voodo you want to do.
Since I have seen your coding and I know you have a reasonable ability to do
this kind of thing I will give you some general instructions. What you want
to do is to check the intersection of the target with the the range set out
in your if statement. I recomend creating a range object for this

Dim rng as range
dim rngCurrent as range

On error resume next
set rng = intersect(Target, range("E12", "E311"))
on error goto errorhandler
if not rng is nothing then
for each rngCurrent in rng

next rngCurrent
--
HTH...

Jim Thomlinson


"Steph" wrote:

Hi all. I have the following inside a worksheet change event:
'Lock/Unlock cells based on FT/PT status
If .Column = 5 And .Row 11 And .Row < 312 Then
Application.EnableEvents = False
If .Value = "PT" Then
Cells(.Row, 6).Value = 20
Else
Cells(.Row, 6).Value = 40
End If
Application.EnableEvents = True
End If

I the user highlights and clears contents of column 5 and 6 at the same time
(ie E5:F5), I get a type mismatch error on the line If .Value = "PT" Then,
and the change event won't fire unless I completely clode out of excel and
re-open. Any ideas on how to prevent that from happening?



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 92
Default Problem w/ worksheet change event

Thanks Jim. I added the error handler that you mentioned in your first
post, and that took care of both problems. The code no longer throws an
error when both cells are cleared, which in turn (I suppose) does not cause
the enableevents to go to false. Thanks so much for your help!

"Jim Thomlinson" wrote in message
...
As for the .Value throwing an error. I am assuming that you have "With
Target"... The problem is that target is a range, not a single cell. What

you
can do is to traverse through all of the cells in the target and check if

it
is within the range you have defined and then do the voodo you want to do.
Since I have seen your coding and I know you have a reasonable ability to

do
this kind of thing I will give you some general instructions. What you

want
to do is to check the intersection of the target with the the range set

out
in your if statement. I recomend creating a range object for this

Dim rng as range
dim rngCurrent as range

On error resume next
set rng = intersect(Target, range("E12", "E311"))
on error goto errorhandler
if not rng is nothing then
for each rngCurrent in rng

next rngCurrent
--
HTH...

Jim Thomlinson


"Steph" wrote:

Hi all. I have the following inside a worksheet change event:
'Lock/Unlock cells based on FT/PT status
If .Column = 5 And .Row 11 And .Row < 312 Then
Application.EnableEvents = False
If .Value = "PT" Then
Cells(.Row, 6).Value = 20
Else
Cells(.Row, 6).Value = 40
End If
Application.EnableEvents = True
End If

I the user highlights and clears contents of column 5 and 6 at the same

time
(ie E5:F5), I get a type mismatch error on the line If .Value = "PT"

Then,
and the change event won't fire unless I completely clode out of excel

and
re-open. Any ideas on how to prevent that from happening?





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 Problem tim Excel Programming 9 March 28th 05 08:37 AM
Worksheet Row Change event crazybass2 Excel Discussion (Misc queries) 4 December 8th 04 05:29 PM
Worksheet Change event Libby Excel Programming 2 October 26th 04 12:02 AM
Change Cell from Validated List Not Firing Worksheet Change Event [email protected] Excel Programming 3 October 4th 04 03:00 AM


All times are GMT +1. The time now is 07:36 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"