PC Review


Reply
Thread Tools Rate Thread

Error handler question in Worksheet_Change event

 
 
Sam Kuo
Guest
Posts: n/a
 
      10th Mar 2008
Hi

My code below clears a cell's value then prompts an error message, if the
input value is outside the specified limit.
But I'd prefer to reverse the sequence (i.e. prompts the error message
first, then clear the input upon accpeting the error message) but don't know
how...

Any help is appreciated.

Sam


Private Sub Worksheet_Change(ByVal Target As Range)

Dim MyWkSht As Worksheet
Dim ARIRange As Range

Set MyWkSht = ThisWorkbook.Worksheets("Sheet1")
Set ARIRange = MyWkSht.Range("N13")

MyWkSht.Unprotect (1)

' Other codes

If ARIRange.Value < 50 Or ARIRange.Value > 130 Then
ARIRange.Value = ""
MyWkSht.Protect (1)
GoTo ErrorHandler
Exit Sub

ErrorHandler:
MsgBox "Please enter a value between 50mm and 130mm.", vbOKOnly,
"Warning"
End

Else
End If

MyWkSht.Protect (1)

End Sub
 
Reply With Quote
 
 
 
 
Sam Kuo
Guest
Posts: n/a
 
      10th Mar 2008
Also, is there a better place (other than Worksheet_Change event) to put this
code under, so it doesn't alway trigger the error message when the cell (N13
in this case) is blank?
 
Reply With Quote
 
 
 
 
RadarEye
Guest
Posts: n/a
 
      10th Mar 2008
Hi Sam,

Try this:

' Begin of code ------------------
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$N$13" Then
If Not IsEmpty(Target) Then
If IsNumeric(Target.Value) Then
If Target.Value < 50 Or Target.Value > 130 Then
MsgBox "Please enter a value between 50mm and
130mm.", vbOKOnly + vbInformation
Target.ClearContents
End If
Else
MsgBox "Please enter a value between 50mm and 130mm.",
vbOKOnly + vbInformation
Target.ClearContents
End If
End If
End If
End Sub
' End of code ------------------------

HTH,

Execurot
 
Reply With Quote
 
Sam Kuo
Guest
Posts: n/a
 
      12th Mar 2008
Thanks RedarEye. It works great!
I've also made the following change - because the input cell is a merged
cell (i.e. N13:O13), but Excel doesn't seem to allow ClearContents in a
merged cell when a cell (i.e. O13) is empty.

change:
Tartget.ClearContents

to:
ThisWorkbook.Worksheets("Sheet1").Range("N13").ClearContents


"Target.ClearContents"

"RadarEye" wrote:

> Hi Sam,
>
> Try this:
>
> ' Begin of code ------------------
> Private Sub Worksheet_Change(ByVal Target As Range)
> If Target.Address = "$N$13" Then
> If Not IsEmpty(Target) Then
> If IsNumeric(Target.Value) Then
> If Target.Value < 50 Or Target.Value > 130 Then
> MsgBox "Please enter a value between 50mm and
> 130mm.", vbOKOnly + vbInformation
> Target.ClearContents
> End If
> Else
> MsgBox "Please enter a value between 50mm and 130mm.",
> vbOKOnly + vbInformation
> Target.ClearContents
> End If
> End If
> End If
> End Sub
> ' End of code ------------------------
>
> HTH,
>
> Execurot
>

 
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
Is it any point to make these two event handler into one event handler Tony Johansson Microsoft C# .NET 1 22nd Dec 2012 01:02 AM
Worksheet_change event handler error gen Microsoft Excel Misc 0 18th Jan 2008 05:55 AM
Event Handler that creates adds another event handler kaczmar2@gmail.com Microsoft ASP .NET 1 22nd Feb 2007 08:37 AM
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:33 PM.