PC Review


Reply
Thread Tools Rate Thread

Comparative operation failing

 
 
Ken Warthen
Guest
Posts: n/a
 
      15th Jun 2009
I'm using the following code to verify a user entered value calculates to a
minimum margin specified on an Excel worksheet. The code works fine unless
the calculated margin is equal to the specified minimum margin in which case
the else event should be triggered but it is not. In a test example when the
user enters values that produce a margin of 25% (0.25) and the specified
minimum margin is 25% (0.25), the message box prompts the user to re-enter a
new value - not the result I was looking for. Any advice on what I might be
doing wrong, or a better way to validate user entries would be very much
appreciated.

Failing code snippet:

If Target.Value <> "" And Target.Offset(-1, 0) <> "" Then
dblActualMargin = Target.Offset(1, 0)
'Minimum Margin
If CDbl(dblActualMargin) < CDbl(dblMinimumMargin) Then
Debug.Print "The actual margin is " & dblActualMargin
Debug.Print "The minimum margin is " & dblMinimumMargin
'Entered retail price does not produces required margin
MsgBox "The entered unit retail does not meet minimum "
& strMessageMargin & "% margin." & vbCrLf & "Please re-enter unit retail
pricing that meets minimum margin.", vbOKOnly + vbExclamation, "Invalid
Retail Price"
Cells(Target.Row, Target.Column) = ""
Cells(Target.Row, Target.Column).Activate
Exit Sub
Else
'Entered retail price meets required margin
Cells((Target.Row + 2), Target.Column).Activate
End If
Else
'Exit routine
Exit Sub
End If

 
Reply With Quote
 
 
 
 
Jim Thomlinson
Guest
Posts: n/a
 
      15th Jun 2009
The issue is most likely one of precision.
0.25
might actually be
0.25000000001

Computers do that kind of thing when working with doubles. Try rounding the
two numbers

If round(CDbl(dblActualMargin), 4) < round(CDbl(dblMinimumMargin), 4) Then
--
HTH...

Jim Thomlinson


"Ken Warthen" wrote:

> I'm using the following code to verify a user entered value calculates to a
> minimum margin specified on an Excel worksheet. The code works fine unless
> the calculated margin is equal to the specified minimum margin in which case
> the else event should be triggered but it is not. In a test example when the
> user enters values that produce a margin of 25% (0.25) and the specified
> minimum margin is 25% (0.25), the message box prompts the user to re-enter a
> new value - not the result I was looking for. Any advice on what I might be
> doing wrong, or a better way to validate user entries would be very much
> appreciated.
>
> Failing code snippet:
>
> If Target.Value <> "" And Target.Offset(-1, 0) <> "" Then
> dblActualMargin = Target.Offset(1, 0)
> 'Minimum Margin
> If CDbl(dblActualMargin) < CDbl(dblMinimumMargin) Then
> Debug.Print "The actual margin is " & dblActualMargin
> Debug.Print "The minimum margin is " & dblMinimumMargin
> 'Entered retail price does not produces required margin
> MsgBox "The entered unit retail does not meet minimum "
> & strMessageMargin & "% margin." & vbCrLf & "Please re-enter unit retail
> pricing that meets minimum margin.", vbOKOnly + vbExclamation, "Invalid
> Retail Price"
> Cells(Target.Row, Target.Column) = ""
> Cells(Target.Row, Target.Column).Activate
> Exit Sub
> Else
> 'Entered retail price meets required margin
> Cells((Target.Row + 2), Target.Column).Activate
> End If
> Else
> 'Exit routine
> Exit Sub
> End If
>

 
Reply With Quote
 
Ken Warthen
Guest
Posts: n/a
 
      15th Jun 2009
Thanks Jim. That did the trick.

Ken

"Jim Thomlinson" wrote:

> The issue is most likely one of precision.
> 0.25
> might actually be
> 0.25000000001
>
> Computers do that kind of thing when working with doubles. Try rounding the
> two numbers
>
> If round(CDbl(dblActualMargin), 4) < round(CDbl(dblMinimumMargin), 4) Then
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Ken Warthen" wrote:
>
> > I'm using the following code to verify a user entered value calculates to a
> > minimum margin specified on an Excel worksheet. The code works fine unless
> > the calculated margin is equal to the specified minimum margin in which case
> > the else event should be triggered but it is not. In a test example when the
> > user enters values that produce a margin of 25% (0.25) and the specified
> > minimum margin is 25% (0.25), the message box prompts the user to re-enter a
> > new value - not the result I was looking for. Any advice on what I might be
> > doing wrong, or a better way to validate user entries would be very much
> > appreciated.
> >
> > Failing code snippet:
> >
> > If Target.Value <> "" And Target.Offset(-1, 0) <> "" Then
> > dblActualMargin = Target.Offset(1, 0)
> > 'Minimum Margin
> > If CDbl(dblActualMargin) < CDbl(dblMinimumMargin) Then
> > Debug.Print "The actual margin is " & dblActualMargin
> > Debug.Print "The minimum margin is " & dblMinimumMargin
> > 'Entered retail price does not produces required margin
> > MsgBox "The entered unit retail does not meet minimum "
> > & strMessageMargin & "% margin." & vbCrLf & "Please re-enter unit retail
> > pricing that meets minimum margin.", vbOKOnly + vbExclamation, "Invalid
> > Retail Price"
> > Cells(Target.Row, Target.Column) = ""
> > Cells(Target.Row, Target.Column).Activate
> > Exit Sub
> > Else
> > 'Entered retail price meets required margin
> > Cells((Target.Row + 2), Target.Column).Activate
> > End If
> > Else
> > 'Exit routine
> > Exit Sub
> > End If
> >

 
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
Comparative Histogram =?Utf-8?B?Sm8gQmV0aA==?= Microsoft Excel Charting 1 18th Sep 2007 03:53 AM
COMPARATIVE COUNTING Iain Halder Microsoft Excel Worksheet Functions 9 21st Nov 2004 05:22 PM
Comparative charts =?Utf-8?B?S2VsbGk=?= Microsoft Excel Charting 1 27th Oct 2004 02:08 AM
Comparative AV-test SFB Anti-Virus 3 1st Mar 2004 04:14 PM
Comparative Chart =?Utf-8?B?Um9u?= Microsoft Excel Charting 1 8th Nov 2003 05:15 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:59 AM.