PC Review


Reply
Thread Tools Rate Thread

Cancel on Worksheet_Change

 
 
Cavy
Guest
Posts: n/a
 
      25th Feb 2008
Hi there,

I am trying to include a Cancel option in a Worksheet_Change event, but I am
having this error message when including the Cancel in the routine:

“Compile Error. Procedure declaration does not match description of even or
procedure having the same name”

My code is as follows:

Private Sub Worksheet_Change(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("XInput")) Is Nothing Then
Else
ReConfigure = MsgBox("You have selected to change X Input" & Chr(10)
& "Pricing needs to be re - calculated" & Chr(10) & "Do you want to
proceed?", vbYesNo)
If ReConfigure = vbNo Then Cancel = True
End If
End sub

Could you please advice?

Thanks

 
Reply With Quote
 
 
 
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      25th Feb 2008
While events are shown declared as subroutines, they are really something
slightly different. You cannot modify the argument list of an event... the
argument list is fixed for each event.

You can do the following to simulate the Cancel functionality. Delete your
existing Worksheet_Change event and replace it with the following...

Dim OriginalValue As Variant

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("XInput")) Is Nothing Then
ReConfigure = MsgBox("You have selected to change X Input" & _
Chr(10) & "Pricing needs to be re - calculated" & _
Chr(10) & "Do you want to proceed?", vbYesNo)
If ReConfigure = vbNo Then
Application.EnableEvents = False
Target.Value = OriginalValue
Application.EnableEvents = True
End If
End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
OriginalValue = Target.Value
End Sub

Make sure the Dim OriginalValue statement is located at the top of your
sheet's code window (in the General-Declarations section).

Rick



"Cavy" <(E-Mail Removed)> wrote in message
news:12C89A44-AFA2-4080-B153-(E-Mail Removed)...
> Hi there,
>
> I am trying to include a Cancel option in a Worksheet_Change event, but I
> am
> having this error message when including the Cancel in the routine:
>
> “Compile Error. Procedure declaration does not match description of even
> or
> procedure having the same name”
>
> My code is as follows:
>
> Private Sub Worksheet_Change(ByVal Target As Range, Cancel As Boolean)
> If Intersect(Target, Range("XInput")) Is Nothing Then
> Else
> ReConfigure = MsgBox("You have selected to change X Input" &
> Chr(10)
> & "Pricing needs to be re - calculated" & Chr(10) & "Do you want to
> proceed?", vbYesNo)
> If ReConfigure = vbNo Then Cancel = True
> End If
> End sub
>
> Could you please advice?
>
> Thanks
>


 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      25th Feb 2008
Maybe this

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("XInput")) Is Nothing Then
ReConfigure = MsgBox("You have selected to change X Input" _
& Chr(10) & "Pricing needs to be re - calculated" & Chr(10) & _
"Do you want to proceed?", vbYesNo)
End If
If ReConfigure = vbNo Then
MsgBox "You pressed cancel"
'do nothing
Else
MsgBox "You pressed ok"
'do something
End If
End Sub

Mike

"Cavy" wrote:

> Hi there,
>
> I am trying to include a Cancel option in a Worksheet_Change event, but I am
> having this error message when including the Cancel in the routine:
>
> “Compile Error. Procedure declaration does not match description of even or
> procedure having the same name”
>
> My code is as follows:
>
> Private Sub Worksheet_Change(ByVal Target As Range, Cancel As Boolean)
> If Intersect(Target, Range("XInput")) Is Nothing Then
> Else
> ReConfigure = MsgBox("You have selected to change X Input" & Chr(10)
> & "Pricing needs to be re - calculated" & Chr(10) & "Do you want to
> proceed?", vbYesNo)
> If ReConfigure = vbNo Then Cancel = True
> End If
> End sub
>
> Could you please advice?
>
> Thanks
>

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      25th Feb 2008
By the way, after you set the Target value back to the OriginalValue, you
should put an Exit Sub statement AFTER the Application.EnableEvents=True
statement so any subsequent code you might have in the event procedure won't
operate on the value that was previously there.

Rick


"Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> While events are shown declared as subroutines, they are really something
> slightly different. You cannot modify the argument list of an event... the
> argument list is fixed for each event.
>
> You can do the following to simulate the Cancel functionality. Delete your
> existing Worksheet_Change event and replace it with the following...
>
> Dim OriginalValue As Variant
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> If Not Intersect(Target, Range("XInput")) Is Nothing Then
> ReConfigure = MsgBox("You have selected to change X Input" & _
> Chr(10) & "Pricing needs to be re - calculated" & _
> Chr(10) & "Do you want to proceed?", vbYesNo)
> If ReConfigure = vbNo Then
> Application.EnableEvents = False
> Target.Value = OriginalValue
> Application.EnableEvents = True
> End If
> End If
> End Sub
>
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> OriginalValue = Target.Value
> End Sub
>
> Make sure the Dim OriginalValue statement is located at the top of your
> sheet's code window (in the General-Declarations section).
>
> Rick
>
>
>
> "Cavy" <(E-Mail Removed)> wrote in message
> news:12C89A44-AFA2-4080-B153-(E-Mail Removed)...
>> Hi there,
>>
>> I am trying to include a Cancel option in a Worksheet_Change event, but I
>> am
>> having this error message when including the Cancel in the routine:
>>
>> “Compile Error. Procedure declaration does not match description of even
>> or
>> procedure having the same name”
>>
>> My code is as follows:
>>
>> Private Sub Worksheet_Change(ByVal Target As Range, Cancel As Boolean)
>> If Intersect(Target, Range("XInput")) Is Nothing Then
>> Else
>> ReConfigure = MsgBox("You have selected to change X Input" &
>> Chr(10)
>> & "Pricing needs to be re - calculated" & Chr(10) & "Do you want to
>> proceed?", vbYesNo)
>> If ReConfigure = vbNo Then Cancel = True
>> End If
>> End sub
>>
>> Could you please advice?
>>
>> Thanks
>>

>


 
Reply With Quote
 
Cavy
Guest
Posts: n/a
 
      25th Feb 2008
It worked great, thanks!

"Rick Rothstein (MVP - VB)" wrote:

> While events are shown declared as subroutines, they are really something
> slightly different. You cannot modify the argument list of an event... the
> argument list is fixed for each event.
>
> You can do the following to simulate the Cancel functionality. Delete your
> existing Worksheet_Change event and replace it with the following...
>
> Dim OriginalValue As Variant
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> If Not Intersect(Target, Range("XInput")) Is Nothing Then
> ReConfigure = MsgBox("You have selected to change X Input" & _
> Chr(10) & "Pricing needs to be re - calculated" & _
> Chr(10) & "Do you want to proceed?", vbYesNo)
> If ReConfigure = vbNo Then
> Application.EnableEvents = False
> Target.Value = OriginalValue
> Application.EnableEvents = True
> End If
> End If
> End Sub
>
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
> OriginalValue = Target.Value
> End Sub
>
> Make sure the Dim OriginalValue statement is located at the top of your
> sheet's code window (in the General-Declarations section).
>
> Rick
>
>
>
> "Cavy" <(E-Mail Removed)> wrote in message
> news:12C89A44-AFA2-4080-B153-(E-Mail Removed)...
> > Hi there,
> >
> > I am trying to include a Cancel option in a Worksheet_Change event, but I
> > am
> > having this error message when including the Cancel in the routine:
> >
> > “Compile Error. Procedure declaration does not match description of even
> > or
> > procedure having the same name”
> >
> > My code is as follows:
> >
> > Private Sub Worksheet_Change(ByVal Target As Range, Cancel As Boolean)
> > If Intersect(Target, Range("XInput")) Is Nothing Then
> > Else
> > ReConfigure = MsgBox("You have selected to change X Input" &
> > Chr(10)
> > & "Pricing needs to be re - calculated" & Chr(10) & "Do you want to
> > proceed?", vbYesNo)
> > If ReConfigure = vbNo Then Cancel = True
> > End If
> > End sub
> >
> > Could you please advice?
> >
> > Thanks
> >

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
CancelEventArgs.Cancel - How Event Raising Code Know if Client Set .Cancel=true Smithers Microsoft C# .NET 3 24th Sep 2007 02:30 AM
Combine 2 codes from WorkSheet_Change & WorkSheet _SelectionChange to ONLY WorkSheet_Change Corey Microsoft Excel Programming 2 17th Dec 2006 09:59 PM
Cancel Macro is user selects 'cancel' at save menu =?Utf-8?B?TWFyaw==?= Microsoft Excel Programming 1 6th Apr 2005 05:45 PM
Re: worksheet_change vs. calculate, and worksheet_change not running Tom Ogilvy Microsoft Excel Programming 1 14th Jul 2003 02:51 AM
worksheet_change vs. calculate, and worksheet_change not running Ross Microsoft Excel Programming 0 13th Jul 2003 04:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:46 AM.