Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default Comparative operation failing

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Comparative operation failing

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default Comparative operation failing

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
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
Comparative Histogram Jo Beth Charts and Charting in Excel 1 September 18th 07 03:53 AM
Change cut/paste operation to cut/insert operation Don Guillett Excel Programming 0 January 17th 07 03:23 PM
Comparative Formulas Michele Excel Worksheet Functions 3 December 8th 05 04:57 PM
Comparative spreadsheets Abe Excel Worksheet Functions 1 February 13th 05 07:59 AM
COMPARATIVE COUNTING Bob Phillips Excel Worksheet Functions 6 November 21st 04 05:22 PM


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