Problem comparing numeric values

rizwindu

New Member
Joined
Oct 20, 2010
Messages
27
Hi all,

I have a system where a user enters values in a form, and these are checked to make sure they are within a range specified in a series of cells.

The comparison is performed like this:

Code:
Dim ctl as Control
...
If ctl.value < .Cells(row, 2) Then
    Debug.Print (ctl.Name & " = " & ctl.value & " and is lower than " & .Cells(row, 2))
End If
If ctl.value > .Cells(row, 3) Then
    Debug.Print (ctl.Name & " = " & ctl.value & " and is higher than " & .Cells(row, 3))
End If

I have found that using the code exactly as above does not perform the comparisons correctly, always indicating the value is out of range.

If I cast the values to CSng or CDbl then it mostly works as expected, however there are still some strange errors. For example, one of the inputs has a lower acceptable limit of 0.015. If an input of 0.6 gives an error, but 0.60 does not.

With 0.6 entered in the input box I get:
Code:
sampleVolume = 0.6 is lower than 0.5
When 0.60 is entered, no error occurs.

What am I missing here?

Many thanks,

Ben.
 
That's interesting. If I type:

?IsDate(0.6)

in the VBE Immediate window, False is returned, as it is with 0.60. But:

?IsDate("0.6")

returns True, and False for "0.60".

So it seems that your problem did arise from not applying the Val function to the control's Value property.
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,224,606
Messages
6,179,862
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top